AirWick219
AirWick219

Reputation: 944

Rails pass object from view to controller possible?

I understander that normally with the model, you can just use the xx_path(id) to pass the id to the controller then you can use the id to get all the information needed; however, we have a REST backend layer, so we don't have the luxury to just pass the id. Also, I don't think is ideal to make another REST call just to query out the info when I can just pass the object from view after selected a particular one.

fruits.html.haml
%thread
  %tr
    %th= 'fruit'
    %th

$tbody
    - if @fruits.any ?
      - @fruits.each do |fruit|
        %tr
          %td= fruit.name
          $td= linkt _to 'edit', edit_fruit_path(fruit)    <---------- is it possible to just pass the object 

Upvotes: 0

Views: 401

Answers (1)

DiegoSalazar
DiegoSalazar

Reputation: 13531

What you are essentially doing by using xx_path(id) is generating an HTML <a> within an HTML document and transmitting that to the client across the wire. When your user clicks on that <a> tag the request will go back to your server and it can pick up the request, and find any objects it needs with the id that came through in the request path. Since this communication is happening over HTTP, and since HTTP is stateless and requests can happen arbitrarily far apart from each other, it is not a good idea to serialize the object you're trying to work with as it is possible for it to get updated in between requests. Passing your object's id around and having your server re-query and re-instantiate your object is the ideal way to handle this. Furthermore, no. You can not pass the object from the view to the controller

Upvotes: 1

Related Questions