2017-04-05 8 views
0

私はRubyで新しいImからToDoアプリケーションをビルドしています。そのユーザーにはTodosの好きなリストが必要です。このアプリには公開と非公開という2種類のリストがあります。ユーザーがログインしていない場合は公開リストを見ることができますが、それらを好きにすることはできません(明らかに)。Rails 5のお気に入りのメカニズム

Implement "Add to favorites" in Rails 3 & 4このチュートリアルに従っていますが、私のコントローラーでは、初期化されていない定数User:FavouriteListと言っています。リストを好きにしようとするとエラーになります。どのように間違っているのかわからないImは、そのソリューションが誰のためにも働いたようだからです。

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 

見ると問題はモデル名とあったFAVボタン

<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

Userモデル内でFavouriteListを呼び出すとFavouriteListクラスが存在しないので、 'Uninitialized constant User :: FavouriteList'のようなエラーが頻繁に発生します –

答えて

0

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

部分その部分:ここに私のコードです。すべての関係が設定されていても、モデルはお気に入りの代わりにFavouriteListsと呼ばれる必要がありました。

関連する問題