Rails best_in_place gem with nested resource

4.9k views Asked by At

Does anyone know if it is possible (and, if so, what the syntax is) for using a nested resource with the best_in_place gem?

My routes.rb looks something like this

resources :users do
  resources :goals 
end

I would like to edit the :description field of the goal, but the code in my view for

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

gives a NoMethodError saying

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

Using

<%= best_in_place @goal, :description %>

give me an undefined method error also because there is no goal_path

I can get the gem to work for @user (the non nested resource) field without problems.

I'm running Rails 3.1.1, Ruby 1.9.2, best_in_place 1.0.4

4

There are 4 answers

1
bknoles On BEST ANSWER

I figured it out.

I needed to set the path option in the call like so

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

It works like a champ now!

0
hernamesbarbara On

Thank you, @bknoles. Your answer definitely helped me reach a similar solution of my own. Here's my implementation:

#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
    @[email protected](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"

You can checkout the entire project on github if you want to see more of the code.

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

Best, Austin

0
VintageOnRails On

Add path and the objects to the path:

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

Somehow the simple path solution of bknoles didn't work for me.

0
rakesh kumar On

Now above method is deprecated.

According to latest Documentation use ":url" instead of ":path" like below in the example

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

Cheers!