我正在建立一个ToDo应用程序,因为我是Ruby的新手,我希望用户可以收藏Todos列表。该应用程序有两种列表:公共和私人,所以如果用户没有登录,他将能够看到公共列表,但显然不能(显然)。
我遵循了本教程: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
查看及其部分
<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>
部分使用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)
问题在于模型名称。即使设置了所有关系,模型也需要被称为FavouriteLists而不是Favorites。