The following works great for carrying forward data from one page to another:
<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => session[:worequest_id] %>
How would I add a 2nd field? The following doesn’t work:
<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => session[:worequest_id] = @worequest.id, [:client_id] = @worequest.client_id %>
Thanks!
UPDATED
This is the code I’m using in the new work order form. It picks up the worequest_id field from the session
<% if session[:worequest_id] != nil %>
<%= f.hidden_field :worequest_id, :value => session[:worequest_id] %>
onclickdoesn’t really work this way – it’s an html attribute used to store JavaScript code to be executed when the element is clicked. While you can use it to evaluate Ruby code in the context of a Ruby method call (in this case as part of the options hash given tolink_to), it doesn’t really make sense to do so.In your first example, it doesn’t actually do anything. If you check your rendered html on the page where that link appears, I expect it evaluates to something like
<a href="..." onclick="6">New Work Order</a>. You can, however, store data insession(which is persistent for as long as the user remains logged in), which is why you’re seeing this data carrying forward from page to page.If you’re trying to fill in default values for the new workorder, you could pass them as params to the path method:
In your
workorders#newaction, your model instantiation would need to include the params:However, this might not be the best way to proceed. If there will always be a client or worequest associated with a workorder, you might want to look into nested routes.