用于nil的未定义方法`memo':使用best_in_place的NilClass错误

时间:2012-08-20 16:45:00

标签: ruby ruby-on-rails-3 json best-in-place

我为自己的应用创建了一个简单的备忘录功能,并使用best_in_place gem成功实现了就地编辑。

我遇到的问题是使用就地编辑创建新备忘录,以下代码为nil返回undefined method备忘录: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和编程都很陌生,所以我确信这是一个简单的解决方案,尽管如此我似乎无法弥补。

1 个答案:

答案 0 :(得分:0)

看看(见3.3.4):

http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

使用部分和变量意味着您需要坚持某种命名约定。如果你不这样做,那么你需要用它发送变量(来自调用视图)。

<%= render :partial => "form", :locals => { :zone => @zone } %>