Search code examples
ruby-on-railsformsruby-on-rails-4nested-attributeslink-to

how to pass params to action#new (in a nested has_many association) via link_to


I have a cinema system where you can select a session and buy a ticket for that so I have a link_to:

<td><%= link_to 'Buy', new_offer_path(:session_id => session.id) %></td>

I have a model called offer and a model ticket, when the user press Buy it generates a Offer. this offer has a nested Ticket and the info that i need is the session_ID to create a ticket id in the offer.

Here is my offer controller #new action:

def new
    @offer = Offer.new
    @offer.ticket = params[:session_id]

    respond_with(@offer)
end

but it wont work, i need the params i have passed by link to fill the ticket fields in the offer how i do that?

other maybe usefull

Offer model

class Offer < ActiveRecord::Base
     belongs_to :ticket
end

Ticket model

class Ticket < ActiveRecord::Base
  belongs_to :session
  belongs_to :seat

  has_many :offers
end

Solution

  • Add the following in your form:

    <%= f.hidden_field :session_id, :value => params[:session_id] %>
    

    So that the session_id persists till the create action.