简体   繁体   English

在嵌套属性中使用 REST

[英]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. REST in Place是 Jan Varwig 开发的一款出色的 rails 插件,它使用 AJAX 在线编辑 object 属性。 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.我已经在网上阅读了使用 REST 和嵌套属性的人,但实际上我没有发现任何关于它是如何完成的提及。 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:我有一份调查 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:和相应的问题 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] 1. 使用非嵌套属性实现的 REST 就地 — [作品]

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).当您单击描述时,会出现一个表单 text_field,其中包含 oldValue(来自内部 HTML)。 I can edit the text, and on hitting 'Enter', the form submits an AJAX update and renders the new value with jQuery.我可以编辑文本,点击“Enter”后,表单会提交 AJAX 更新并使用 jQuery 呈现新值。 All is good here.这里一切都很好。

2. REST in Place implemented with nested attributes — [does not work] 2. REST in Place 使用嵌套属性实现 — [不起作用]

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.在这里,加载表单的 javascript 工作正常,但是当我尝试提交一个新值来更新问题标题时,我收到错误ActiveRecord::RecordInvalid (Validation failed: Question contents can't be blank ,告诉我要么它正在尝试创建一个新问题(这没有意义,因为请求仍将发送到调查控制器中的update操作),或者它正在重置两个值。

3. Setting nested attribute :update_only => true — [partly works] 3. 设置嵌套属性:update_only => true — [部分有效]

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:我将调查 model 的代码更改为以下内容:

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.然后,有趣的是,AJAX 请求运行良好——当我更改问题属性时,它已在数据库中更新,但由于某种原因,它卡在“正在保存...”并且从未恢复新更新的属性。 Reloading the page of course displays the new text fine, but the whole point of using AJAX is to prevent that necessity.重新加载页面当然可以很好地显示新文本,但是使用 AJAX 的全部目的是为了防止这种必要性。


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: jquery.rest_in_place.js第84行是:

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.构建 POST 数据时,您应该得到survey[question][title] ,这正是update_attributes所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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