在form_tag中传递多个参数 [英] Passing Multiple Parameters in a form_tag

查看:116
本文介绍了在form_tag中传递多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails和jqPlot在网页上显示一些数据.该图需要按日期灵活(例如,用户可以选择一个时间范围,并且该图会自动更新).到目前为止,这是我的代码(在我的视图中):

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是我的控制器,event_trends是一个动作. (路径为localhost:3000/home/event_trends.3102030)

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

这很好.但是,我还需要将另一个参数传递给我的控制器.此网页已经有一个传递给它的参数params[:format](这是URL中的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]))

还有其他

如何让控制器从视图中查看多个参数?

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

推荐答案

没有什么与Idan的答案相抵触的,但这就是我最终所做的(为将来的Google员工...)

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

在我看来:

<%= 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

最后是模型:

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

这似乎最适合我.我决定允许为GET请求更改URL,然后立即将issue参数存储在搜索框中,以备后用.

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!

这篇关于在form_tag中传递多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆