문제

Need some help with this please.

I have a model Photos that has a column zone_id and category_id.

Photos with categories and Photos with views render the same index view (photos index).

I want to have a sidebar, depending on if you are in a Photo index by views or in a Photo index by category shows one dropdown or another.

So, if you click on Photo by Modern category, I want the sidebar to render a dropdown "Search by Zone"

In the other case, If you are in Photos index view by zone, I want my side bat to render a dropdown "Search by category".

I know this is with an if / unless, but don't know how to tell the conditional "Hey dude, Im in Photos by category, so you need to render Photos Zone dropdown".

Please help!

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

# I've got rid of Photo.with_user_avatar ...
def search
    @photos = Photo.all
    @photos = @photos.where('category_id = ?', params[:category_id]) if params[:category_id]
    @photos = @photos.where('zone_id = ?', params[:zone_id]) if params[:zone_id]
    @photos = @photos.paginate(:page => params[:page])
    render :index
end

Routes

 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

Photos Index View

<% if @photos.first.zone %>
        <h6 class="bold medium uppercase grey">Seleccionar estilo para:  </br>
            <span class="green big"><%= (@photos.first.zone.name ? "#{@photos.first.zone.name}": "") %></h6></span>

            <!-- One dropdown-->

                <ul class="listnone bold lightgrey">
                    <li><%= link_to "Moderno", spots_category_zone_path(1,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Clásico", spots_category_zone_path(2,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Industrial & Loft", spots_category_zone_path(3,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Minimalista", spots_category_zone_path(4,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Rústico", spots_category_zone_path(5,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Contemporáneo", spots_category_zone_path(6,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Étnico", spots_category_zone_path(7,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Art Deco", spots_category_zone_path(8,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                    <li><%= link_to "Ecléctico", spots_category_zone_path(9,@photos.first.zone) %>
                    <span class="spot_count small right"><%= @photos.count %></span></li>
                </ul>
                <% else %>

                <!-- The other dropdown -->

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

            <% end %>

Thanks a lot!

도움이 되었습니까?

해결책

Here we are again! Two easy ways.

You can set a variable equal to the action:

def type
     @photos = Photo.by_category(params[:category_id]).paginate(:page => params[:page])
     @action = 'type'
     render :index
end

def zone
    @photos = Photo.by_zone(params[:zone_id]).paginate(:page => params[:page])
    @action = 'zone'
    render :index
end

in your view

<% if @action == 'type' %>
  <%= render 'sidebar/zone_menu' %>
<% elsif @action == 'zone' %>
  <%= render 'sidebar/category_menu' %>
<% end %>

Or you can skip the variable and just call params[:action] in your view:

<% if params[:action] == 'type' %>
  <%= render 'sidebar/zone_menu' %>
<% elsif params[:action] == 'zone' %>
  <%= render 'sidebar/category_menu' %>
<% end %>

Personally, I'd go with the variable. It just feels like the kind of logic that belongs in the controller. Your mileage may vary.

I'd also put the menus into partials (make a "sidebar" folder, create _zone_menu.html.erb and _category_menu.html.erb respectively) and load them that way but you can certainly just put the full menus in the view instead. If you're going to do partials, I find it a best practice to keep instance variables out of partials by passing locals:

<%= render 'sidebar/category_menu', locals: { photo_count: @photos.count, current_zone: @photos.first.zone } %>

And then in the partial, replace @photos.count and @photos.first.zone with photo_count and current_zone.

다른 팁

As @subvertallchris pointed out you can access the params hash so I would do this in the controller.

before_filter :set_action

private
  def set_action
    @action = params[:action]
  end

then you can use the views stated in @subvertallchris answer

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top