Reddirt
Reddirt

Reputation: 5953

Rails passing information from a view to a form

I have a Rails app that includes Requests and Workorders tables.

I have a New Workorder button on the Request show page. I need to pass information from the Request to the Workorder - for instance the Request.id.

I'm currently using flash to do this. Here is the button on the Request show page:

<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick =>  (flash[:request_id] = @request.id  %>

In the new Workorder form, I have:

  <% if flash[:request_id] != nil %>
     <%= f.hidden_field :request_id, :value => flash[:request_id] %> 

This works. But, not always. And I haven't been able to figure out why it fails sometimes.

Is there a better way to pass this data?

Thanks for the help!!

UDPDATE1

Sometimes I need to bring forward quite a few data fields. For example:

       <%= link_to 'Follow-up Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => ( flash[:workorder_id] = @workorder.id, flash[:client_id] = @workorder.client_id, flash[:contact_id] = @workorder.contact_id, flash[:location_id] = @workorder.location_id, flash[:type_id] = @workorder.type_id, flash[:woasset_id] = @workorder.woasset_id) %>

Upvotes: 1

Views: 317

Answers (1)

Lazarus Lazaridis
Lazarus Lazaridis

Reputation: 6029

You can try passing the parameter to the path of the link and then pass it to the form via your controller's action:

Link:

<%= link_to 'New Work Order', new_workorder_path(request_id: @request.id), :class => 'btn btn-primary' %>

Controller

def new
  @request_id = params[:request_id]
  ...
end

In your view:

<%= f.hidden_field :request_id, value: @request_id %>

Upvotes: 3

Related Questions