对铁杆来说是新的,而这个是踢我的屁股。
我的应用设置为Collections => Lists =>链接。
集合
have_many :lists
belongs_to :user
解释
have_many :links
belongs_to :collection
链接
belongs_to :list
收藏控制器
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])
# render :text => CGI.escapeHTML(@collection.inspect)
redirect_to_good_slug(@collection) and return if bad_slug?(@collection)
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
before_filter :find_collection, only: [:new, :create, :destroy, :edit, :update]
def new
@collection = Collection.find(params[:collection_id])
end
def create
@list = @collection.lists.build(list_params)
@list.save!
respond_to do |format|
format.html {root_path(@collection.id)}
format.js
end
end
def edit
@list = List.find(params[:id])
respond_to do |format|
format.html {root_path(@collection.id)}
format.js
end
end
def update
@list = List.find(params[:id])
if @list.update(list_params)
redirect_to collection_path(@collection.id)
else
render 'edit'
end
end
def destroy
@list = List.find(params[:id])
@list.destroy
respond_to do |format|
format.html {redirect_to collection_path(@collection.id)}
format.js
end
end
private
def list_params
params.require(:list).permit(:alias)
end
def find_collection
@collection = Collection.find(params[:collection_id])
end
end
现在,在集合的show视图中,我有一个循环
<%= @collection.lists.each do |list| %>
这是抓住馆藏中的所有列表,效果很好。我试图在这里嵌套另一个循环,抓住每个列表的所有链接。
链接控制器
class LinksController < ApplicationController
before_filter :find_list, only: [:new, :create, :destroy, :edit, :update]
def new
@list = List.find(params[:list_id])
end
def create
@link = @collection.list.links.build(link_params)
@link.save!
respond_to do |format|
format.html {root_path(@list.id)}
format.js
end
end
def edit
@list = List.find(params[:id])
@link = Link.find(params[:id])
respond_to do |format|
format.html {root_path(@list.id)}
format.js
end
end
def update
@list = List.find(params[:id])
@link = Link.find(params[:id])
if @link.update(link_params)
redirect_to list_path(@list.id)
else
render 'edit'
end
end
def destroy
@list = List.find(params[:id])
@link = Link.find(params[:id])
@link.destroy
respond_to do |format|
format.html {redirect_to list_path(@list.id)}
format.js
end
end
private
def link_params
params.require(:link).permit(:url)
end
def find_list
@list = List.find(params[:list_id])
end
end
我尝试像这样循环..
<%= @list.links.each do |link| %>
和
<%= @collection.lists.links.each do |link| %>
和
<%= @collection.list.links.each do |link| %>
并且它不会为nil:class抛出任何方法。
我的路线看起来像..
devise_scope :user do
authenticated :user do
root 'collections#index', as: :authenticated
resources :collections do
resources :lists
end
resources :lists do
resources :links
end
end
我认为我在ORM方面表现不错,但我对后端的所有人都很陌生,任何帮助都会非常感激。
答案 0 :(得分:0)
当您迭代@collection.lists
时,您正在使用块变量list
,它引用特定的list
实例。要获取列表的所有链接,您应该使用变量list
,如下所示:
<%= @collection.lists.each do |list| %> <%# Notice block variable |list| %>
<%= list.links.each do |link| %>
...
<% end %>
<% end %>