Rails best_in_place gem with nested resource
Asked Answered
M

4

15

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

Maybe answered 8/1, 2012 at 17:24 Comment(0)
M
20

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!

Maybe answered 9/1, 2012 at 2:8 Comment(1)
can you assist, I have a similar issue: #30090719Talithatalk
D
10

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.

Defensive answered 19/2, 2013 at 10:6 Comment(0)
B
4

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!

Begone answered 6/10, 2016 at 9:49 Comment(0)
P
1

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

Phonetic answered 19/4, 2012 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.