0

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

我遇到的问题是使用就地编辑创建undefined method新备忘录,以下代码为 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 和一般编程的新手,所以我确信这是一个简单的修复,尽管如此,我似乎无法修复它。

4

1 回答 1

0

看看(见 3.3.4):

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

使用部分和变量,意味着您需要遵守特定的命名约定。如果您不这样做,那么您需要将变量(从调用视图)与它一起发送。

<%= render :partial => "form", :locals => { :zone => @zone } %>
于 2012-08-20T21:03:44.310 回答