camdroid
camdroid

Reputation: 524

Rails Parse Form Input

I'm using a form to transfer some data from one part of a controller to another (new to create), but I'm having some trouble with it. When I try to get the data after submitting the form, it just gives me a nil value.

Here's the form code:

<%= f.hidden_field :owner_id, :value => @tool.user_id %>
<%= f.hidden_field :tool_id, :value => @tool.id %>
<%= f.hidden_field :borrower_id, :value => current_user.id %>

And this is the create action in the controller:

  def create
    render text: params[:rental_agreement].inspect
    @rental_agreement = RentalAgreement.create
    @rental_agreement.owner_id = params[:owner_id]
    # render text: @rental_agreement.inspect
  end

When I hit the "Submit" button on the form, I see this:

{"owner_id"=>"3", "tool_id"=>"1", "borrower_id"=>"4"}

That's fine, but when I change which inspection renders (comment out the top line, uncomment the bottom), all it displays is:

#

And if I look in the Rails console at this object, all of the fields in it are nil (except the id and created_at fields).

I'm just trying to figure out how to assign the variables from the form (owner_id, tool_id, and borrower_id) to the rental_agreement variable. Any help is much appreciated!

Upvotes: 2

Views: 879

Answers (1)

Jason Kim
Jason Kim

Reputation: 19031

Your create method seems wrong. Try this.

def create
  @rental_agreement = RentalAgreement.new(params[:rental_agreement])
  @rental_agreement.save
end

Upvotes: 2

Related Questions