views:

99

answers:

3

I have one action which will include the .id for a vendor class through link_to.

That action which is a modification of the new() for a different controller for Reviews needs to pass both the value of the ID from the URL and the session[:user_id] into the database.

  • How can I grab the value of the vendor.id? Is it possible to not pass it through the URL and place it in the flash instead?

  • do I use a hidden value to pass the session[:user_id]?

+1  A: 

I assume you can access it as params[:id] from the controller action? If so, you should be able to access it as params[:id] in the View. Ditto for session[:user_id].

Edit: I think I may have misunderstood your question. Are you asking about posting the id back through the form? Or possibly not having the id in the url?

Matt Grande
yes, I did have the ID passed to the new but it wasn't being passed through the form. Should I just use a hidden value? Seems like that's how people are suggesting...although that's not particularly RESTful...
AFG
A: 

If you're passing parameters between controllers, you should usually pass it as a parameter.

<%= link_to "Link to other controller", other_path(@other, :some_param => param_value) %>

In the action you're linking to (here it's SHOW), you'll be able to access :some_param as params[:some_param].

From forms, go ahead and include a hidden field (as long as you're checking to see if it's valid later on, since a user could submit anything).

Either way, don't use the flash to store session state - the flash is for user messages only.

Raphomet
If there is a has_many relationship (e.g. vendor has_many reviews) can I still use review_path and then pass vendor_id => @vendor.id?
AFG
I have it link to controller new which takes them to a submit form which I guess then upon submission calls create, and I get an error:ActiveRecord::RecordNotFound (Couldn't find Vendor without an ID):It seems like it is losing the ID when going from new to create?
AFG
A: 

I don't believe people are understanding the question. You pass it through the link but what does the form_tag look like. If you try something like form_tag '/person/new/:id', the form will not submit properly. Is there a way to submit the parameter through the form tag or should you just use a hidden field tag? I believe that is the question being asked.

Todd