2014-04-17 35 views
0

嘿,伙计们我试图能够将列表添加到我的集合中。我是新的铁路任何帮助将不胜感激。目前我正在尝试构建一个表单来创建一个新列表,但新的/创建的操作似乎已经搞乱了。试图将列表添加到我的集合(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 
+0

4? –

+0

我正在使用Rails 4 – Gillzilla

回答

0

更改您的ListsController新创建行动以下:您使用Rails 3或

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

更改后,我仍然得到'列表'的未定义方法。使用<%= form_for([@ collection,@ collection.lists.build])do | f | %>在new.html.erb中。我应该在db中提及集合引用用户,并列出引用集合。如果这很重要 – Gillzilla

+0

您确定您的Collection模型中有'has_many:lists'这一行吗? – dostu

+0

啊!非常感谢,现在我收到错误'未知属性:collection_id',但我感觉距离更近了一百万倍。 – Gillzilla

相关问题