Hey guys I am trying to be able to add lists to my collections. I am new to rails any help would be appreciated. Currently I am trying to build a form to make a new list, but the new/create actions seem to be messed up.
The lists will end up living in the collections show view later via ajax.
Ultimately the goal is for each user to own multiple collections and in each collection there will be multiple lists, in each lists multiple items.
Collections
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
Lists
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
Users
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
The link I was trying
<%= link_to '<i class="fa fa-plus-square"></i> Add Subcategory'.html_safe, new_collection_list_path(@collection.id) %>
Current routes
devise_scope :user do
authenticated :user do
root 'collections#index', as: :authenticated
resources :collections do
resources :lists
end
end
Failed form_for
<%= form_for([@collection, @collection.lists.build]) do |f| %>
<% end %>
Models
Users has_many :collections
Collections belong_to :user
has_many :lists
Lists belong_to :collection