简体   繁体   中英

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 . 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.

Example: I want to see photos that have category attribute : 1 and zone attribute : 1 at the same time. I've done:

Photos Controller

`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`

Routes.rb

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

In my view

`<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.

Thanks!!!

Rails will match parameters in the order they are specified in your route. 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 . 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 '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 .

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 .

In your case, if @zone == 'foo' , then <%= link_to "Moderno", spots_category_path(1, zone: @zone) %> returns http://localhost:3000/spots/1?zone=foo . You want to use spots_category_zone_path as demonstrated in my first example..

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