将html.erb文件中的数据添加到数据库中

时间:2016-06-23 00:35:13

标签: html ruby-on-rails ruby ruby-on-rails-3

做Rails(Rails的新手)....并希望将字段添加到数据库中(我的代码没有这样做)。我有2个型号:

# == Schema Information
#
# Table name: claims
#
#  id         :integer          not null, primary key
#  status     :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Claim < ActiveRecord::Base
    has_many :claim_items
end

# == Schema Information
#
# Table name: claim_items
#
#  id         :integer          not null, primary key
#  item_title :string
#  lp_number  :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class ClaimItem < ActiveRecord::Base
    belongs_to :claim
end

ClaimItem是包含lp_numberitem_title的单个项目。 &#39;根据权利要求&#39;表示claim_items的存储空间,其字段为status

问题是,用户可以选择(复选框) multiple checkboxes ,然后点击“提交”按钮。按钮,lp_numbertitle应在方法create执行后存储在数据库中。但我很确定我的html.erb文件错了:

<body>
        <%= form_for @claim, :url => { :action => "create" } do |f| %>
            <h3>Please check the items <br></h3>
            <% @lp_title.each do |lp_number, title| %>
                    <%= check_box_tag "check_box", lp_number, checked = false %> 
                    <%= lp_number + ": "%> <%= link_to title, @lp_image[lp_number]%>
                    <br>
                    <%= select_tag(:user_id,    '<option value="0">---Select a reason---</option>
                                                 <option value="1">Bought by mistake</option>
                                                 <option value="2">Missing parts or accessories</option>
                                                 <option value="3">Didn\'t approve purchase</option>'.html_safe)%>

                    <br>
                        <%= text_area :issue_description, :cols => "100", :rows => "100" %>
                    <br>
                    <br>

            <% end %>
            <%= f.submit "Submit" %>
        <% end %>
</body>

方法 create new

def new
        @claim = Claim.new

        @lp_title = {}
        @lp_image = {}
        lp = products.each do |product|
                        product.units.each do |unit|     
                            @lp_title[unit.lp] = product.title #stores lp numbers and titles
                            @lp_image[unit.lp] = product.img #stores lp numbers and images
                        end  
                    end
    end

def create 
        @claim = Claim.new(params[:post])
        if @claim.save
            redirect_to :action => 'show'
        else 
            redirect_to :action => 'new'
        end

    end

0 个答案:

没有答案