创建下拉菜单以显示rails 4

时间:2015-09-02 07:22:27

标签: ruby-on-rails ruby-on-rails-4 drop-down-menu menu

我正在开发一个食谱应用程序,但无法显示类别和子类别的下拉菜单。 我为类别创建了菜单并且显示正确,但是现在我想要点击类别时会显示属于他们的子类别,点击子类别后我希望它显示食谱。

这是我的代码:

控制器

def index
    if params[:category].blank?
        @recipe = Recipe.all.order("created_at DESC")
    elsif params[:category]
        @category_id = Category.find_by(name: params[:category]).id
        @recipe = Recipe.where(category_id: @category_id).order("created_at DESC")
    else params[:subcategory]    
        @subcategory_id = Subcategory.find(name: params[:subcategory]).id
        @recipe = Recipe.where(subcategory_id: @subcategory_id).order("created_at DESC")
    end
end



def show
end

def new
    @recipe = current_user.recipe.build
end

def create
    @recipe = current_user.recipe.build(recipe_params)

    if @recipe.save
        redirect_to @recipe, notice: "recipe sucessfully created"
    else
        render 'new'
    end
end

def edit
end

def update
    if @recipe.update(recipe_params)
        redirect_to @recipe
    else
        render 'edit'
    end
end

def destroy
    @recipe.destroy
    redirect_to root_path, notice: "sucessfully deleted"
end


private

def recipe_params
    params.require(:recipe).permit(:title, :description, :image, :category_id, :subcategory_id, ingredients_attributes: [:id, :name, :_destroy], directions_attributes: [:id, :step, :_destroy])
end


def find_recipe
    @recipe = Recipe.find(params[:id])
end
end

applicatio.html.haml

!!! 5
%html
%head
    %title Recipe App
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true
    = javascript_include_tag 'application', 'data-turbolinks-track' => true
    = csrf_meta_tags

%nav.navbar.navbar-default
    .container
        .navbar-brand= link_to "Recipe Box", root_path
        %ul.nav.navbar-nav
            - Category.all.each do |category|
                %li= link_to category.name, recipes_path(category: category.name)
                - category.subcategories.each do |subcategory| 
                    %li= link_to subcategory.name, recipes_path(subcategory: subcategory.name)

        = render 'layouts/header'
        - if user_signed_in?
            %ul.nav.navbar-nav.navbar-right
                %li= link_to "New Recipe", new_recipe_path
                %li= link_to "Sign Out", destroy_user_session_path, method: :delete
        - else
            %ul.nav.navbar-nav.navbar-right
                %li= link_to "Sign Up", new_user_registration_path
                %li= link_to "Sign In", new_user_session_path

.container
    - flash.each do |name, msg|
        = content_tag :div, msg, class: "alert"

    = yield

recipe.rb模型

class Recipe < ActiveRecord::Base
    searchkick
    belongs_to :user
    belongs_to :subcategory


    has_many :ingredients
    has_many :directions


    accepts_nested_attributes_for :ingredients, 
                                                reject_if: proc { |attributes| attributes ['name'].blank? },
                                                allow_destroy: true
    accepts_nested_attributes_for :directions, 
                                                reject_if: proc { |attributes| attributes ['step'].blank? },
                                                allow_destroy: true
    validates :title, :description, :image, presence: true
    has_attached_file :image, style: { :medium => "400x400#>" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

category.rb

class Category < ActiveRecord::Base


    has_many :subcategories
    has_many :recipes, :through => :subcategories
end

subcategory.rb

class Subcategory < ActiveRecord::Base

    belongs_to :category

    has_many :recipes


end

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您可以使用javascript或JQuery执行此操作。 category_name id上的呼叫更改功能,在此功能中更改子类别的选项值,依此类推。