2017-05-16 5 views
-1

複数のパラメータをビューからコントローラに渡すにはどうすればよいですか?私は、例えばのような要求を実行する必要があります。複数のパラメータをレールでlink_toで渡す

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

各パラメータには、独自のlink_toの選択によって生成され、私は私のモデルを照会するために一緒にパラメータを使用したいと思います。

#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 

編集:私はそうのように行うに助言する答えに遭遇しました:。

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

しかし、私の場合、私は2つの異なるリンク(例えばfooへの対応する単一のパラメータを持つものを持っているチェックボックスを)、もう一つはbaz(例えばドロップダウン)のための単一のパラメータです。課題は、異なるリンクからパラメータを引き出し、それらをまとめていく方法です。

+4

すでにこのhttp://stackoverflow.com/questions/10773695/rails-passing-parameters-in-link-to?rq=1上のかなりの数の質問がありますが読む

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

Iceman

+1

既存の質問に加えて、既に複数のパラメータを持つリンクを使用する回答があります。まだ答えが見つかりませんでしたか?たぶんあなたの質問には詳細や説明が必要かもしれません。 – Gerry

+0

@gerry私は1つのリンクの例で複数のパラメータを見ていました。 EDIT – amo

答えて

0

コントローラー、アクションなどを使用する代わりに、単純に渡したいパラメーターでroute_helper_methodを使用することができます。およそlink_toここ

+0

私は投票の理由を知っているかもしれませんか? –

関連する問題