简体   繁体   中英

rails 3 how to handle the receiving end of query strings

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?

I think you need to set up a route in routes.rb, such as

match "recommendation/:category", controller: 'recommendation', action: 'new', category: :category

Then, in your controller, you can find the category in params[:category] , and pass that to the view.

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;

<% case params[:category]
   when 'Brand' %>
  show brand stuff
<% when 'Business' %>
   show business stuff
<% else %>
   show default something else
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM