[英]Making routes with 2 params at the same time in Rails
I'm really stuck with this. 我真的很坚持。 I'm new to rails.
我是新手。
I want to make a double parameter route to filter photos by 2 attributes . 我想做一个双参数路由来按2个属性过滤照片。 If I'm visualizing photos by category 1 and click on zone 1, it should show photos with category 1 and zone 1 at the same time.
如果我按类别1可视化照片并单击区域1,则它应同时显示类别1和区域1的照片。
Example: I want to see photos that have category attribute : 1 and zone attribute : 1 at the same time. 示例:我想同时查看具有类别属性:1和区域属性:1的照片。 I've done:
我弄完了:
`def type
@photos = Photo.by_category(params[:category_id]).paginate(:page => params[:page])
render :index
end
def zone
@photos = Photo.by_zone(params[:zone_id]).paginate(:page => params[:page])
render :index
end
def search
@photos = Photo.with_user_avatar.all
@photos = @photos.where('category = ?', params[:category_id]) if params[:category_id]
@photos = @photos.where('zone = ?', params[:zone_id]) if params[:zone_id]
@photos = @photos.paginate(:page => params[:page])
end`
get 'spots/:category_id', to: "photos#type", as: :spots_category get 'spots/zonas/:zone_id', to: "photos#zone", as: :spots_zone get 'spots/:category_id/:zone_id', to: "photos#search", as: :spots_category_zone get 'spots/zonas/:zone_id/:category_id', to: "photos#search", as: :spots_zone_category
`<section class="row semi_padding_top">
<aside id="sidemenu" class="large-3 columns">
<a href="#" data-dropdown="drop1" data-options="align:right" class="button dropdown"></i> Quiero ver spots de...</a><br>
<ul id="drop1" data-dropdown-content class="f-dropdown" data-options="align:right">
<li><%= link_to "Baños", spots_zone_path(1) %></li>
<li><%= link_to "Cocinas", spots_zone_path(2) %></li>
<li><%= link_to "Cuartos de estar", spots_zone_path(3) %></li>
<li><%= link_to "Dormitorios", spots_zone_path(4) %></li>
<li><%= link_to "Exteriores", spots_zone_path(5) %></li>
<li><%= link_to "Hostelería", spots_zone_path(6) %></li>
<li><%= link_to "Infantil", spots_zone_path(7) %></li>
<li><%= link_to "Oficinas", spots_zone_path(8) %></li>
<li><%= link_to "Salones", spots_zone_path(9) %></li>
</ul>
<h5 class="bold uppercase lightgrey">Estilos
<span class="green"><%= (@zone ? "para #{@zone}": "") %></h5></span>
<ul class="listnone bold lightgrey">
<li><%= link_to "Moderno", spots_category_path(1, zone: @zone) %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Clásico", spots_category_path(2) %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Industrial & Loft", spots_category_path(3), zone: @zone %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Minimalista", spots_category_path(4), zone: @zone %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Rústico", spots_category_path(5) %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Contemporáneo", spots_category_path(6) %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Étnico", spots_category_path(7) %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Art Deco", spots_category_path(8) %>
<span class="spot_count small right"><%= @photos.count %></span></li>
<li><%= link_to "Ecléctico", spots_category_path(9) %>
<span class="spot_count small right"><%= @photos.count %></span></li>
</ul>
</aside>
<%= render 'spotlist' %>
` `
I don't know how to configure my view to show a double parameter. 我不知道如何配置视图以显示双精度参数。
I've tried with <%= link_to "Moderno", spots_category_path(1, zone: @zone) %> , but didn't work. 我试过<%= link_to“ Moderno”,spot_category_path(1,zone:@zone)%> ,但没有用。
Thanks!!! 谢谢!!!
Rails will match parameters in the order they are specified in your route. Rails将按照在路线中指定的顺序匹配参数。 For example:
例如:
get 'spots/:category_id/:zone_id', to: "photos#search", as: :spots_category_zone
<%= link_to "Moderno", spots_category_zone_path(1, 'Moderno') %>
...would output a link to http://localhost:3000/spots/1/Moderno
. ...将输出到
http://localhost:3000/spots/1/Moderno
。 You can also specify them explicitly: 您还可以明确指定它们:
<%= link_to "Moderno", spots_category_zone_path(category_id: 1, zone_id: 'Moderno') %>
Or you can give it parameters not specified in the route, in which case it will append them as normal ?get=
values. 或者,您可以给它提供路由中未指定的参数,在这种情况下,它将把它们附加为普通的
?get=
值。
get 'spots/:category_id/:zone_id', to: "photos#search", as: :spots_category_zone
<%= link_to "Moderno", spots_category_zone_path(1, 'Moderno', foo: 'bar') %>
...would give you http://localhost:3000/spots/1/Moderno?foo=bar
. ...将为您提供
http://localhost:3000/spots/1/Moderno?foo=bar
。
Specifying them explicitly is helpful if you want to list them out of order for some reason. 如果您出于某些原因要不按顺序列出它们,则明确指定它们将很有帮助。
<%= link_to "Moderno", spots_category_zone_path(zone_id: 'Moderno', foo: 'bar', category_id: 1) %>
...still gives you http://localhost:3000/spots/1/Moderno?foo=bar
. ...仍然为您提供
http://localhost:3000/spots/1/Moderno?foo=bar
。
In your case, if @zone == 'foo'
, then <%= link_to "Moderno", spots_category_path(1, zone: @zone) %>
returns http://localhost:3000/spots/1?zone=foo
. 在您的情况下,如果
@zone == 'foo'
,则<%= link_to "Moderno", spots_category_path(1, zone: @zone) %>
返回http://localhost:3000/spots/1?zone=foo
。 You want to use spots_category_zone_path
as demonstrated in my first example.. 如我的第一个示例所示,您想使用
spots_category_zone_path
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.