Passing multiple table IDs between views

I am having some difficulty conceiving how to best address a parameter passing problem I am having with rails. I am sure that this has been encountered and solved by others so perhaps someone can illuminate my ignorance.

I have the following models:

Entity   has_many :locations   has_many :site_locations, :include => :sites   has_many :sites, :through => :site_locations

Site   has_many :locations

Location   belongs_to :entity   belongs_to :site

Typically a site is created together with a location. The call from entities/index looks like this:

      <% if entity.locations.empty? -%>             <%= link_to 'Add Locations', new_entity_location_path(entity) -%>       <% else -%>

which passes this params hash to the locations_controller action new:

entity_id: "3" action: new controller: locations

What I require is for the value {:entity_id => '3' } to be retained and passed back to the controller for the subsequent locations/show view. I tried doing this in the locations/new view:

  <%=       hidden_field_tag :entity_id, params[:entity_id].to_s   -%>

but that did not work. How do I accomplish this?

save it to the session maybe?

Roger Pack wrote:

save it to the session maybe?

On Mon, Apr 28, 2008 at 12:27 PM, James Byrne

I am not sure that that would be the correct thing to do. This is not a session variable but a request variable. What I have is the situation where location.id = 'x' and location.entity_id = 'y'. Now, in the locations_controller I need to lookup entities via entity_id.

What I desire is to pass entity_id, which is in params already when the locations/new view is presented, from this view back to the locations_controller via params. I cannot recall how do do this in the present instance. Perhaps I have to pass it as an optional parameter in the submit action. I had thought that initializing a hidden field would accomplish this, but evidently I was wrong.

double check your hidden field settings http://betterlogic.com/roger/?p=204