mercredi 22 juin 2016

Adding the data from html.erb file into database

Doing Rails (new to Rails).... and would like to add the fields into the database (which my code is not doing). I have 2 models:

# == 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

and

# == 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 is a single item that contains lp_number and item_title. 'Claim' represents a storage for claim_items and has field status.

The problem is a user can select (checkbox) a multiple checkboxes, and after clicking the 'Submit' button, the lp_number and title should be stored in the database after method create gets executed. But I'm pretty sure I did my html.erb file wrong:

<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>

Method create

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

    end

Aucun commentaire:

Enregistrer un commentaire