简体   繁体   中英

Using REST in Place with nested attributes

REST in Place is a fantastic rails plugin by Jan Varwig that uses AJAX to edit object attributes inline. It was very easy to set up, simple to implement, and generally just works well.

I've read around online of people using REST in Place with nested attributes, but I haven't actually found any mentions of how it's done. I wouldn't expect it to be too difficult, but I haven't been able to figure it out yet (although I've had no trouble with non-nested attributes).

I have a Survey model:

class Survey < ActiveRecord::Base
  has_one :question, :dependent => destroy
  accepts_nested_attributes_for :question

  attr_accessible :description
  validates_presence_of :description
end

and a corresponding Question model:

class Question < ActiveRecord::Base
  belongs_to :survey

  attr_accessible :title, :contents
  validates_presence_of :title, :contents
end

My attempts have yielded three scenarios:

1. REST in Place implemented with non-nested attributes — [works]

The code in the view looks like this:

<span class="rest_in_place" data-url="<%= url_for @survey %>" data-object="survey" data-attribute="description">
  <%= @survey.description %>
</span>

and it works great. When you click on the description, a form text_field appears with the oldValue inside (from the inner HTML). I can edit the text, and on hitting 'Enter', the form submits an AJAX update and renders the new value with jQuery. All is good here.

2. REST in Place implemented with nested attributes — [does not work]

The code looks like this (let me know if there's something wrong)

<span class="rest_in_place" data-url="<%= url_for @survey %>" data-object="survey" data-attribute="question_attributes[title]">
  <%= @survey.question.title %>
</span>

Here, the javascript that loads the form works fine, but when I try to submit a new value to update the question title, I receive the error ActiveRecord::RecordInvalid (Validation failed: Question contents can't be blank , telling me that either it's trying to create a new question (which doesn't make sense since the request is still going to the update action in the Survey controller), or it's resetting both values.

3. Setting nested attribute :update_only => true — [partly works]

So, I figured that if I could prevent the creation of new Questions, it might work (even though this is not an adequate solution, since I need new questions to be created for every new survey). I changed the code of the Survey model to the following:

class Survey < ActiveRecord::Base
  has_one :question, :dependent => destroy
  accepts_nested_attributes_for :question, :update_only => true

  ...

end

And then, interestingly enough, the AJAX request worked fine — when I changed a question attribute, it was updated in the database, but for some reason it got stuck on "saving…" and never restored the newly updated attribute. Reloading the page of course displays the new text fine, but the whole point of using AJAX is to prevent that necessity.


That should be everything. Please let me know if you have any ideas as to why any of these problems are occurring, and if you have suggestions for fixing them. Thanks!

Is it possible to define a route for the question updates? Then it looks like you could write:

<span class="rest_in_place" data-url="<%= url_for @survey.question %>" data-object="question" data-attribute="title">
  <%= @survey.question.title %>
</span>

Line 84 of jquery.rest_in_place.js is:

data += "&"+this.objectName+'['+this.attributeName+']='+encodeURIComponent(this.getValue());

You want the params sent to the server to include something like:

survey[question][title]=knights

From that, it looks like the proper solution is:

data-url="<%= url_for @survey %>" data-object="survey[question]" data-attribute="title"

When the POST data is constructed, you should get survey[question][title] , which is what update_attributes expects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM