Search code examples
rubyruby-on-rails-3jsonbest-in-place

undefined method `memo' for nil:NilClass error using best_in_place


I've created a simple memo feature for my app, and have successfully implemented in-place editing using the best_in_place gem.

The problem I am having is creating new memos using in-place editing, the following code returns undefined methodmemo' 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

I am new to rails and programming in general so I am sure it is a simple fix, none-the-less it's one that I cannot seem to remedy.


Solution

  • Have a look at (see 3.3.4):

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

    Using partials and variables, means you need to stick to a certain naming convention. If your not doing that, then you need to send the variable (from the calling view) with it.

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