Rails最佳实践之嵌套资源使用best_in_place gem

15

有人知道是否可能(如果可以的话,语法是什么)使用嵌套资源与best_in_place宝石?

我的routes.rb文件大致如下:

resources :users do
  resources :goals 
end

我想编辑目标的:description字段,但我在视图中的代码为:

<%= best_in_place [@user, @goal], :description %>

出现 NoMethodError 错误

undefined method `description' for #<Array:0x20e0d28> 

使用

<%= best_in_place @goal, :description %>

由于没有goal_path,给我一个未定义的方法错误。

我可以在没有问题的情况下为@user(非嵌套资源)字段使宝石正常工作。

我正在运行Rails 3.1.1、Ruby 1.9.2和best_in_place 1.0.4。

4个回答

20

我弄清楚了。

我需要在调用中设置path选项,就像这样:

<%= best_in_place @goal, :description, :path => user_goal_path %>

现在它像个冠军一样运行得很好!


你能帮忙吗?我有一个类似的问题:http://stackoverflow.com/questions/30090219/best-in-place-building-exotic-method-after-creation - Sauron

10

添加路径和对象到路径中:

<%= best_in_place @goal, :description, :path => user_goal_path(@user,@goal) %> 

一些原因,bknoles 的简单路径解决方式对我不起作用。


4

现在上述方法已经过时。

根据最新的文档,在示例中使用":url"代替":path"。

<%= best_in_place @goal, :description, :url => user_goal_path %>

干杯!


1

谢谢你,@bknoles。你的回答确实帮助我找到了类似的解决方案。这是我的实现:

#widget.rb
class Widget < ActiveRecord::Base
  validates_presence_of :name
  has_many :gadgets
  attr_accessible :name, :description
end

#gadget.rb
class Gadget < ActiveRecord::Base
  belongs_to :widget
  attr_accessible :name, :widget_id, :id
end


#gadgets_controller.rb
  def update
    @gadget=@widget.gadgets.find(params[:id])
    if @gadget.update_attributes(params[:gadget])
      respond_to do |format|
        format.html
        format.json { respond_with_bip(@gadget) }
      end
    else
     respond_to do |format|
       format.html { render :action => "edit" }
       format.json { respond_with_bip(@gadget) }
     end
    end
  end


#views/gadgets/_gadget.html.haml
%tr{ :name => "gadget_", :id => gadget.id }
  %td= gadget.created_at.localtime.strftime("%B %d, %l:%M%p") 
  %td.big=best_in_place gadget, :name, :path => [@widget, gadget]
  %td.delete{:style => 'text-align:center;'}
    =check_box_tag "gadget_ids[]", gadget.id, false, :class => "checkbox"

如果你想查看更多的代码,可以在Github上检出整个项目。

https://github.com/hernamesbarbara/ajax-rails-full-crud

最好的祝福, 奥斯汀


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接