Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道5最喜爱的机构_Ruby On Rails - Fatal编程技术网

Ruby on rails 轨道5最喜爱的机构

Ruby on rails 轨道5最喜爱的机构,ruby-on-rails,Ruby On Rails,我正在构建一个ToDo应用程序,因为我是Ruby的新手,我希望用户可以查看最喜欢的ToDo列表。该应用程序有两种列表:公共列表和私人列表,因此,如果用户未登录,他将能够看到公共列表,但无法喜欢它们(显然) 我遵循了本教程:但当我尝试(取消)收藏列表时,我的控制器中出现了一个错误,即未初始化的常量User::favoriteList。我不确定我是怎么做错的,因为这个解决方案似乎对每个人都有效。这是我的密码: class ListsController < ApplicationControl

我正在构建一个ToDo应用程序,因为我是Ruby的新手,我希望用户可以查看最喜欢的ToDo列表。该应用程序有两种列表:公共列表和私人列表,因此,如果用户未登录,他将能够看到公共列表,但无法喜欢它们(显然)

我遵循了本教程:但当我尝试(取消)收藏列表时,我的控制器中出现了一个错误,即未初始化的常量User::favoriteList。我不确定我是怎么做错的,因为这个解决方案似乎对每个人都有效。这是我的密码:

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
class ListsControllercurrent_user.favorites问题在于型号名称。即使建立了所有的关系,模型需要称为FavoriteList,而不是Favorites。

您可以显示相关模型的代码吗?它们在控制器上方。当您在用户模型内部调用
FavoriteList时,经常会出现类似
未初始化常量User::FavoriteList
的错误,并且FavoriteList类不存在。我的朋友建议我这样做创建一个用户列表表,尽管我不确定是否是这样。如何修复此错误?
<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>