将表单对象传递给从 ajax 请求呈现的部分

发布于 2024-09-26 08:01:48 字数 1200 浏览 4 评论 0原文

我有一个带有一些嵌套属性模型的事件表单。从选择框中选择客户端后,将渲染其他模型。观察者观察并调用控制器操作,该操作呈现包含 fields_for 嵌套模型的部分。我遇到的问题是我无法将事件“表单”块传递给新渲染的部分 - 至少我不知道如何...

下面的代码引发错误:“参数数量错误( 0 为 1)”。任何帮助或建议表示赞赏。如下所述,如果您可以提供此场景的示例,我也愿意使用不显眼的 JavaScript 重新实现此功能。

事件表单:

<%- form_for @event do |form| %>

  <%= select_tag :id=>event_client_id %>
  <%= observe_field :event_client_id, url => {:action => 'client_questions'}, :with => "'client_id=' + encodeURIComponent(value)+'&event_id='+#{@event.id} %>

事件控制器

 def client_questions
   @event = Event.find(params[:event_id])
   @client = Client.find(params[:client_id])
   @client_questions = @client.questions.active
   respond_to do |format|
     format.js {
       render :update do |page|
         page[:client_questions].replace_html :partial => 'client_questions', :layout => false
       end
     }
   end
 end

_client_questions.html.erb部分

<%- form.fields_for :client, @client do |client| %>
  <%= client_text_field :name %>

  <%- client.fields_for :questions do |question| %>
    <%=question.text_field :content %>

I have an event form with some nested attribute models. The additional models are rendered after a client is selected from a select box. An observer watches and calls a controller action which renders a partial containing the fields_for nested models. The issue I'm having is that I can't pass the event 'form' block to the newly rendered partial - at least I can't figure out how...

The code below raises the error: "wrong number of arguments (0 for 1)". Any help or suggestions are appreciated. As mentioned below, I'm also willing to re-implement this using unobtrusive JavaScript if you can provide an example for this scenario.

Event Form:

<%- form_for @event do |form| %>

  <%= select_tag :id=>event_client_id %>
  <%= observe_field :event_client_id, url => {:action => 'client_questions'}, :with => "'client_id=' + encodeURIComponent(value)+'&event_id='+#{@event.id} %>

Event Controller

 def client_questions
   @event = Event.find(params[:event_id])
   @client = Client.find(params[:client_id])
   @client_questions = @client.questions.active
   respond_to do |format|
     format.js {
       render :update do |page|
         page[:client_questions].replace_html :partial => 'client_questions', :layout => false
       end
     }
   end
 end

_client_questions.html.erb partial

<%- form.fields_for :client, @client do |client| %>
  <%= client_text_field :name %>

  <%- client.fields_for :questions do |question| %>
    <%=question.text_field :content %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再浓的妆也掩不了殇 2024-10-03 08:01:48

因此,您的主表单是在一个操作中呈现的,并且您希望将表单对象从该视图传递到在第二部分中呈现的另一个视图?这不是您可以做的事情,但是您可以在第二次渲染的部分中修改视图代码,以便它在不需要现有表单对象的情况下进行渲染,例如:

<%- fields_for "event[client_attributes]", @client do |client| %>
  <%= client_text_field :name %>

  <%- client.fields_for :questions do |question| %>
    <%= question.text_field :content %>

我还没有运行此代码来测试它,所以比较它生成的 html 与您现有的部分在与主表单相同的操作中呈现时将生成的 html 一起生成。

So your main form is rendered in one action, and you're hoping to pass the form object from that view, to another view rendered in a second partial? That's not something that you can do, but you can modify your view code in the partial rendered the second time, so that it renders without needing the existing form object, something like:

<%- fields_for "event[client_attributes]", @client do |client| %>
  <%= client_text_field :name %>

  <%- client.fields_for :questions do |question| %>
    <%= question.text_field :content %>

I haven't run this code to test it, so compare the html it generates with the html that your existing partial would generate if it was rendered in the same action as the main form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文