首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义方法“备注”用于零:使用best_in_place的NilClass错误

未定义方法“备注”用于零:使用best_in_place的NilClass错误
EN

Stack Overflow用户
提问于 2012-08-20 16:44:59
回答 1查看 289关注 0票数 0

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

我遇到的问题是使用就地编辑创建新备忘录,下面的代码返回undefined method备注‘for nil:NilClass`’

代码语言:javascript
复制
#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和编程都很陌生,所以我确信这是一个简单的修复方法,但这是我似乎无法弥补的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-20 21:03:44

请看一下(参见3.3.4):

rendering.html#using-partials

使用部分和变量,意味着您需要坚持特定的命名约定。如果您没有这样做,那么您需要(从调用视图)发送它的变量。

代码语言:javascript
复制
<%= render :partial => "form", :locals => { :zone => @zone } %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12041692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档