我为我的应用程序创建了一个简单的备忘录功能,并成功地使用best_in_place gem实现了就地编辑。
我遇到的问题是使用就地编辑创建新备忘录,下面的代码返回undefined method备注‘for nil:NilClass`’
#partial for logged in users on homepage
<% provide(:title, current_user.name) %>
<h1>Hey
<%= current_user.name %>
</h1>
<div class="row-fluid">
<div class="span12">
<div class="row-fluid">
<div class="span6">
<h2>Memos</h2>
<ul id="memos" data-update-url="<%= sort_memos_url %>">
<%# render @memos %>
<% @memos.each do |memo| %>
<%= content_tag_for :li, memo do %>
<%= best_in_place memo, :memo %>
<% end %>
<% end %>
</ul>
<p><%# link_to "Add a memo", new_memo_path %></p>
<p><%= best_in_place @memo, :memo, :path => new_memo_path, :nil => "Add a memo" %></p>
</div>
<div class="span6"><h2>Coming up...</h2></div>
</div>
</div>
</div>
#memos controller
def index
@memos = Memo.order("position")
end
def show
@memo = Memo.find(params[:id])
end
def new
@memo = Memo.new
end
def create
@memo = Memo.new(params[:memo])
if @memo.save
redirect_to @memo, notice: "Successfully created memo."
else
render :new
end
end
def edit
@memo = Memo.find(params[:id])
end
def update
@memo = Memo.find(params[:id])
@memo.update_attributes(params[:memo])
respond_with @memo
end
def sort
params[:memo].each_with_index do |id, index|
Memo.update_all({position: index+1}, {id: id})
end
render nothing: true
end
#home page controller
respond_to :html, :json
def home
if signed_in?
@memos = Memo.order("position")
end
end
def sort
params[:memo].each_with_index do |id, index|
Memo.update_all({position: index+1}, {id: id})
end
render nothing: true
end
def help
end
end一般来说,我对rails和编程都很陌生,所以我确信这是一个简单的修复方法,但这是我似乎无法弥补的。
发布于 2012-08-20 21:03:44
请看一下(参见3.3.4):
rendering.html#using-partials
使用部分和变量,意味着您需要坚持特定的命名约定。如果您没有这样做,那么您需要(从调用视图)发送它的变量。
<%= render :partial => "form", :locals => { :zone => @zone } %>https://stackoverflow.com/questions/12041692
复制相似问题