嘿,伙计们我试图能够将列表添加到我的集合中。我是新的铁路任何帮助将不胜感激。目前我正在尝试构建一个表单来创建一个新列表,但新的/创建的操作似乎已经搞乱了。试图将列表添加到我的集合(form_for,路由)
列表最终将通过ajax生活在收藏展览视图中。
最终目标是让每个用户拥有多个收藏,并且在每个收藏中都会有多个列表,每个列表中会列出多个项目。
集合
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
列表
class ListsController < ApplicationController
def index
@list = List.all
end
def new
@list = List.new
end
def create
@collection = Collection.find(params[:collection_id])
@list =
@collection.lists.create(comments_params)
if @collection.lists.save
redirect_to root_path(@user)
else
render 'new'
end
end
end
用户
class UsersController < ApplicationController
def index
@user = User.find(current_user)
@collection = Collection.where(:user_id => current_user.id)
@user.collection = Collection.where(:user_id => current_user.id)
# render :text => CGI.escapeHTML(@collection.inspect)
end
end
的联系,我试图
<%= 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
<%= form_for([@collection, @collection.lists.build]) do |f| %>
<% end %>
模型
Users has_many :collections
Collections belong_to :user
has_many :lists
Lists belong_to :collection
4? –
我正在使用Rails 4 – Gillzilla