0

I tried this:

th = link_to "ID", results_path(sort: "id", search_form: @search_form)

to return the instance @search_form to the controller "results", but that returns a nil variable, but I have to point out that the sort variable accessed on the controller in this way params[:sort] has the correct value, so the path works.

So now I have to return every single attribute by its own and this is the solution that works

th = link_to "ID",
                      :sort => "id",
                      :verweildauer => @search_form.verweildauer,
                      :datenjahr => @search_form.datenjahr,
                      :drgsystem => @search_form.drgsystem,
                      :greatersmallerequal => @search_form.greatersmallerequal,
                      :patients_per_page => @search_form.patients_per_page

then I have to rebuild the object on the controller accessing every single attribute.

How can I directly pass in the @search_form into the controller?

Mattia
  • 179
  • 15
  • Link_to likes to do a get call unless you tell it otherwise,. Check out http://stackoverflow.com/questions/13414663/using-rails-link-to-for-links-that-post – dbugger Mar 29 '17 at 00:43
  • @dbugger still not working I tried with: `th = link_to "ID", resultsPost_path( param1: "@search_form"), method: :post` on the view and `post '/results', to: 'results#results', :as => :resultsPost` on the `routes.rb` file. String parameters are passed in but the object itself is not passed. Is there a strange way to access it from the controller or can I access it like this `params[:param1]` because I actually don't even see a param1 parameter on the debugger... – Mattia Mar 29 '17 at 19:24

1 Answers1

0

If I correctly understand you want to pass many filter params to the get/link_to. It is a bad idea because of get request query size limit. You should use form tag here and move all your params to hidden fields. This is the right way.

AlexeyKuznetsov
  • 414
  • 4
  • 12
  • Can I move the entire instance `@search_form` instead of accessing separately every attribute? – Mattia Mar 30 '17 at 10:44
  • I want to pass in the whole instance not just the parameters – Mattia Mar 30 '17 at 13:40
  • `link_to 'ID', results_path(@search_form.merge({sort: 'id'}))` if your @search_form is an instance of Hash class. It will use all them keys/values but if it is not - you can't send ruby object data to URL, you should convert it into hash anyway. – AlexeyKuznetsov Mar 30 '17 at 16:18