2017-04-05 45 views
0

我建立一个ToDo应用程序,因为我与Ruby的新,我希望用户可以喜欢的Todos列表。该应用程序有两种列表:公共和私人,所以如果用户没有登录,他将能够看到公共列表,但不会能够喜欢他们(显然)。Rails 5最喜爱的机制

我跟着本教程:Implement "Add to favorites" in Rails 3 & 4但我得到和错误在我的控制器说,当我尝试(联合国)收藏列表未初始化的常量User :: FavouriteList。林不知道我做错了,因为它似乎解决方案适用于每个人。这里是我的代码:

class ListsController < ApplicationController 

    before_action :set_list, except: [:index, :new, :create] 
    before_action :authenticate_user!, except: [:index, :show] 

    def index 
    @lists = List.all 
    @users = User.all 
    end 

    def show 

    end 

    def new 
    @list = List.new 
    @list.todos.build 
    end 

    def edit 
    end 

    def create 
    @list = current_user.lists.new list_params 
    @list.todos.each do |todo| 
     todo.user_id = current_user.id 
    end 

    if @list.save 

     redirect_to @list, notice: "List was successfuly created!" 
    else 
     render action: :new 
    end 
    end 

    def update 

    @list.close = true 
    @list.todos.each do |todo| 
     if !todo.close 
     @list.close = false 
     end 
    end 

    respond_to do |format| 
     if @list.update(list_params) 
     format.html { redirect_to @list, notice: "List was successfuly updated!" } 
     format.json {render :show, status: :ok, location: @list } 
     else 
     format.html {render :edit } 
     format.json {render json: @list.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @list.destroy 
    redirect_to lists_url, notice: "List was successfuly removed!" 
    end 

    def favourite 
    type = params[:type] 
    if type == "favourite" 
     current_user.favourites << @list 
     redirect_to :back 
    elsif type == "unfavourite" 
     current_user.favourites.delete(@list) 
     redirect_to :back 
    else 
     redirect_to :back, notice: "Nothing happened." 
    end 
    end 


    private 

    def set_list 
    @list = List.find(params[:id]) 
    end 

    def list_params 
    params.require(:list).permit(:title, :public, :close, todos_attributes: Todo.attribute_names.map(&:to_sym).push(:_destroy)) 
    end 

end 

class User < ApplicationRecord 

    has_many :lists, dependent: :destroy 
    has_many :todos, dependent: :destroy 
    has_many :favourite_lists 
    has_many :favourites, through: :favourite_lists, source: :list 


    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :lockable, :timeoutable, :confirmable 
end 

class List < ApplicationRecord 
    belongs_to :user, optional: true 
    has_many :favourite_lists 
    has_many :favourites, through: :favourite_lists, source: :user 
    has_many :todos, inverse_of: :list, dependent: :destroy 
    accepts_nested_attributes_for :todos, allow_destroy: true, reject_if: proc { |att| att['task'].blank? } 

    validates :title, presence: true 
    validates_associated :todos 
end 

class Favourite < ApplicationRecord 
    belongs_to :user_id 
    belongs_to :list_id 
end 

View及其部分

<h1>Public Lists</h1> 
    <div class="container"> 
     <table class="table"> 
      <thead> 
       <th>Favourites</th> 
       <th>Title</th> 
       <th>Author</th> 
       <th>Created at</th> 
      </thead> 
      <tbody> 
       <% @lists.each do |list| %> 
        <% if list.public %> 
         <tr> 
          <% if user_signed_in? %> 
           <td><%= render "favourites", object: list %></td> 
          <% end %> 
          <td><%= list.title %></td> 
          <td><%= @users.find(list.user_id).name %></td> 
          <td><%= list.created_at %></td> 
          <td><%= link_to "View", show_list_path(list), class: "btn btn-primary", role: "btn" %></td> 
          <% if user_signed_in? && current_user.id == list.user_id %> 
           <td><%= link_to "Edit", edit_list_path(list), class: "btn btn-default", role: "btn" %></td> 
           <%= render "delete_button", path: list %> 
          <% end %> 
         </tr> 
        <% end %> 
       <% end %> 
      </tbody> 
     </table> 
    </div> 

部分,进行最爱按钮

<td><%= link_to "aaaaa", favourite_list_path(object, type: "unfavourite"), method: :put %><i class="fa fa-star" aria-hidden="true"></i></td> 

<td><%= link_to "aaaaaaaa", favourite_list_path(object, type: "favourite"), method: :put %><i class="fa fa-star-o" aria-hidden="true"></i></td> 
+0

您是否可以显示相关模型的代码? –

+0

他们在控制器上面 – Aram

+0

当你在用户模型里调用'FavouriteList'并且FavouriteList类不存在时,经常会出现'uninitialized constant User :: FavouriteList'这样的错误 –

回答

0

的问题是与型号名称。即使设置了所有关系,该模型也需要被称为FavouriteLists而不是收藏夹。