Passing Multiple Parameters in a form_tag

久未见 提交于 2019-12-25 06:36:29

问题


I'm using Rails and jqPlot to display some data on a webpage. The graph needs to be flexible by date (as in, the user can select a time range, and the graph will update itself). Here's my code so far (in my View):

<%= form_tag home_event_trends_path(params[:timePeriod]), :method => 'post' do %>
  <p>
    <%= select_tag :timePeriod, options_for_select(["2 Hours Ago", "1 Week Ago", "3 Months Ago"], :selected => params[:timePeriod]) %>
    <%= submit_tag "Refresh", :name => nil %>
  </p>
<% end %>

Home is my controller, event_trends is an action, I suppose. (The path is localhost:3000/home/event_trends.3102030)

This works fine. However, I need to also pass another parameter to my controller. This webpage is already having a parameter passed to it, params[:format] (this is the 3102030 in the URL, which changes based upon what event I want to display), and I need this passed along as well. I've tried:

home_event_trends_path(params[:timePeriod][:format])
home_event_trends_path(params[:timePeriod],[:format])
home_event_trends_path(params([:timePeriod][:format]))

among others.

How do I get my controller to see multiple parameters from my View?


回答1:


I see you are passing the old timePeriod using GET and the new timePeriod using POST. Is this really what you want? Why does the controller need the old timePeriod?

Since you want to display a graph - that is, you want to retrieve data, not to cause side effects - I think you should send only the new timePeriod using GET:

<%= form_tag home_event_trends_path(), :method => 'get' do %>
    <p>
        <%= select_tag :timePeriod, options_for_select(["2 Hours Ago", "1 Week Ago", "3 Months Ago"], :selected => params[:timePeriod]) %>
        <%= select_tag :format, options_for_select([...], :selected => params[:format]) %>
        <%= submit_tag "Refresh", :name => nil %>
    </p>
<% end %>

Also, I agree with Anthony - you should try to change the format parameter - just look in the controller to see where it is used, and change the name.




回答2:


Nothing against Idan's answer, but this was what I ended up doing (for future Googlers...)

In my View:

<%= form_tag home_event_trends_path, :method => 'get' do %>
  <p>
    <%= select_tag :timePeriod, options_for_select(["2 Hours Ago", "1 Week Ago", "3 Months Ago"], :selected => params[:timePeriod]) %>
    <%= text_field_tag :issue, params[:issue] %>
    <%= submit_tag "Refresh", :name => nil %>
  </p>
<% end %>

And then in my Home Controller:

def event_trends
  @eventTrend = Home.eventTrend(params[:timePeriod], params[:issue])
end

And finally, the Model:

def self.eventTrend(date, id)
  # dirty work
end

This seems to work best for me. I decided to allow the URL to change for the GET request, and I'm immediately storing the issue parameter in the search box, for use later on.

Hope this helps anyone in the future!



来源:https://stackoverflow.com/questions/11762542/passing-multiple-parameters-in-a-form-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!