Ruby on rails 正在尝试将列表添加到我的集合中(表单_,路由)
嘿,伙计们,我正在尝试将列表添加到我的收藏中。我是rails新手,如有任何帮助,将不胜感激。目前,我正在尝试构建一个表单来创建一个新列表,但是新建/创建操作似乎很混乱 这些列表最终将通过ajax显示在collections show视图中 最终目标是让每个用户拥有多个集合,并且在每个集合中将有多个列表,在每个列表中有多个项目 收藏Ruby on rails 正在尝试将列表添加到我的集合中(表单_,路由),ruby-on-rails,activerecord,crud,form-for,nested,Ruby On Rails,Activerecord,Crud,Form For,Nested,嘿,伙计们,我正在尝试将列表添加到我的收藏中。我是rails新手,如有任何帮助,将不胜感激。目前,我正在尝试构建一个表单来创建一个新列表,但是新建/创建操作似乎很混乱 这些列表最终将通过ajax显示在collections show视图中 最终目标是让每个用户拥有多个集合,并且在每个集合中将有多个列表,在每个列表中有多个项目 收藏 class CollectionsController < ApplicationController def index @user = User.
class CollectionsController < ApplicationController
def index
@user = User.find(current_user)
@collection = Collection.where(:user_id => current_user.id)
end
def new
@collection = Collection.new
end
def show
@collection = Collection.find(params[:id])
@list = List.all
end
def create
@collection = Collection.new(collection_params)
@collection.user_id = current_user.id
# render :text => CGI.escapeHTML(@collection.inspect)
if @collection.save
redirect_to root_path(@user)
else
render 'new'
end
end
def edit
@collection = Collection.find(params[:id])
end
def update
if @collection.update(collection_params)
redirect_to root_path(@user)
else
render 'edit'
end
end
def destroy
@collection.destroy
redirect_to root_path(@user)
end
private
def collection_params
params.require(:collection).permit(:alias, :notes, :visibility)
end
def find_collection
@collection = @user.collection.find(params[:id])
end
end
失败的表单\u
<%= form_for([@collection, @collection.lists.build]) do |f| %>
<% end %>
将ListsController的新建和创建操作更改为以下内容:
def new
@collection = Collection.find(params[:collection_id])
end
def create
@collection = Collection.find(params[:collection_id])
@list = @collection.lists.build(params[:list])
if @list.save
redirect_to root_path(@user)
else
render 'new'
end
end
您使用的是Rails 3还是Rails 4?更改后,我仍然得到了“list”的未定义方法。在new.html.erb中使用。我应该在db中提到集合引用用户,并列出引用集合。如果是这样的话,你确定你的收藏模型中有“has_many:list”这一行吗?啊!非常感谢你,现在我得到了错误“未知属性:集合id”,但我感觉距离你近了一百万倍。你需要在“列表”表中有集合id字段。啊,它是复数的,谢谢你,伙计,我整天都在看这个。我真的很感激!
<%= link_to '<i class="fa fa-plus-square"></i> Add Subcategory'.html_safe, new_collection_list_path(@collection.id) %>
devise_scope :user do
authenticated :user do
root 'collections#index', as: :authenticated
resources :collections do
resources :lists
end
end
<%= form_for([@collection, @collection.lists.build]) do |f| %>
<% end %>
Users has_many :collections
Collections belong_to :user
has_many :lists
Lists belong_to :collection
def new
@collection = Collection.find(params[:collection_id])
end
def create
@collection = Collection.find(params[:collection_id])
@list = @collection.lists.build(params[:list])
if @list.save
redirect_to root_path(@user)
else
render 'new'
end
end