所以我已经对Stack Overflow做了一些研究并尝试了各种解决方案,但似乎没有任何工作。我试图渲染我的ListsController显示页面,但我不断收到错误:未定义的方法`name'为零:NilClass。当我使用rails控制台时,我的参数传递了ID,我可以调用@ list = List.find(params [:id]然后@ list.name。但是,我一直收到相同的错误消息。我的控制器看起来像这样:
class ListsController < ApplicationController
def index
@lists = List.order("created_at DESC")
@list = List.new
end
def create
@list = List.new(list_params)
if @list.save
if request.xhr?
# somehow only send back the html for the page update
render :layout => false
else
redirect_to root_path
end
else
@lists = List.all
render :index
end
def show
@list = List.find(params[:id])
end
end
private
def list_params
params.require(:list).permit(:name)
end
end
我的观点目前看起来像这样:
<section class="todoapp">
<header class="header">
<h1><%= @list.name %></h1>
</header>
<section class="main">
<input class="toggle-all" type="checkbox">
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<%= render @list.items %>
<!-- exactly the same as: -->
<%#= render :collection => @list.items, :partial => "items/item" %>
</ul>
</section>
</section>
我已尝试使用
答案 0 :(得分:0)
class ListsController&lt; ApplicationController中
def index @lists = List.order(“created_at DESC”) @list = List.new 端
def创建 @list = List.new(list_params) if @ list.save 如果request.xhr? #不知何故只发回html进行页面更新 render:layout =&gt;假 其他 redirect_to root_path 结束 其他 @lists = List.all 渲染:索引 结束 端
def show @list = List.find(params [:id]) 端私人 def list_params params.require(:名单).permit(:名称) 端
端