0

How can I pass multiple parameters from the view to the controller? I need to perform a request like for example:

GET "/indicators/data?country_id=5&statistic_id=12&anotherparameter_id=anothervalue, ...."

Each parameter is generated by its own link_to selection and I would like to use the parameters together to query my model.

#app/views/indicators/index.html.erb
<!--statistic select button-->
<ul id = "statistics">
   <% Statistic.all.each do |stat| %>
   <li><%= link_to stat.indicator_name, :action => "data", :controller => "indicators", :statistic_id => stat.indicator_code, remote: true %></li>
   <% end %>
</ul>

<!--country select button-->
<ul id = "countries">
   <% Country.all.each do |c| %>
   <li><%= link_to c.name, :action => "data", :controller => "indicators", :country_id => c.id, remote: true %></li>
   <% end %>
</ul>

#config/routes.rb
get 'indicators/data' => 'indicators#data'

#app/controllers/indicators_controller.rb
  def data
    @country = Country.find(params[:country_id]).name
    @statistic = params[:statistic_id]
    @queryresult = Mymodel.where(country: @country, statistic: @statistic)
    respond_to do |format|
      format.js
    end
  end

EDIT: I had come across answers that advise to do like so:

link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux")
# => <a href="/searches?foo=bar&amp;baz=quux">Nonsense search</a>

But in my case I have two different links one with a single parameter corresponding to foo (eg. a checkbox) and the other a single parameter for baz (eg. a dropdown) so to say. The challenge is how to pull the parameters from different links and put them together.

amo
  • 3,030
  • 4
  • 25
  • 42
  • 4
    There are quite a few questions already on this http://stackoverflow.com/questions/10773695/rails-passing-parameters-in-link-to?rq=1 – Eyeslandic May 16 '17 at 12:09
  • 1
    Besides existing questions, there's already an answer to use a link with multiple parameters. Still not found an answer? Maybe your question needs more detail or clarification. – Gerry May 16 '17 at 14:34
  • @gerry I had seen the multiple parameters within one link example. Please see the clarification under the EDIT – amo May 16 '17 at 18:12
  • So, if i understand your code: You have multuple links with countries (one for each `Country`) and multiple links with statistics (one for each `Statistic`) all of them with different values (i.e. parameters). What parameters should be sent if the first country link is clicked? Which value from the statistics links should it consider? – Gerry May 16 '17 at 20:59
  • @gerry Yes, I have multiple links with countries (one for each Country) and multiple links with statistics (one for each Statistic) each with a different value. If the first country is clicked country_id=1 is sent. Which value from the statistics links should it consider? It does not automatically consider any statistic. It considers the statistic that the user selects. If statistic 5 is selected then statistic_id=5 is to be sent. But for the query to be complete I need to send "country_id=1&statistic_id=5" together. – amo May 17 '17 at 05:17
  • Note that the country and statistic widgets are separate. Eg. If I go to the country widget I can select country=>Singapore. Then I move to the statistic widget and select statistic=>Employment rate or statistic=>Fertility rate. So the statistic resource is not nested within the country resource but are independent. – amo May 17 '17 at 05:25

1 Answers1

1

Instead of using controller, action etc you can simply use route_helper_method with params that you want to pass simply.

<li><%= link_to stat.indicator_name, indicators_data_url(countryid: "value", another_id: "value", etc: "etc"), remote: true %></li>

Read about link_to here

Narasimha Reddy - Geeker
  • 3,510
  • 2
  • 18
  • 23