Rails 3如何处理查询字符串的接收端
我有两个简单的链接,
<h3><%= link_to "Brand Awards", new_recommendation_path(:category => "Brand" ) %></h3>
<h3><%= link_to "Business Awards", new_recommendation_path(:category => "Business" ) %></h3>
这些链接都转到相同的视图,但它们显然将两个单独的值传递给类别。
如何使用这些在我的 new_recommendation 视图中运行 if 语句。
我基本上需要做: 如果类别=“品牌”做abc,elsif类别=“商业”做xyz
我是否走在正确的道路上?
I have two simple links
<h3><%= link_to "Brand Awards", new_recommendation_path(:category => "Brand" ) %></h3>
<h3><%= link_to "Business Awards", new_recommendation_path(:category => "Business" ) %></h3>
these links both go to the same view but they obviously pass two separate values to category.
How do use these to run an if statement in my new_recommendation view.
I need to essentially do:
if category = "Brand" do abc, elsif category = "Business" do xyz
Am I even on the right track?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该路线(在另一个答案中提到)不是必需的(尽管很不错)。如果您不设置路线,它将仅显示为查询字符串参数,例如:
/recommendation?category=Brand
无论哪种方式,您都可以只引用参数,例如;
The route (mentioned in the other answer) isn't necessary (though a nice touch). if you don't set up the route, it will just appear as a query-string param eg:
/recommendation?category=Brand
Either way you can just refer to the params eg;
我认为你需要在routes.rb中设置一个路由,例如
然后,在你的控制器中,你可以在
params[:category]
中找到类别,并将其传递给视图。I think you need to set up a route in routes.rb, such as
Then, in your controller, you can find the category in
params[:category]
, and pass that to the view.