将 ActionView::Helpers::FormBuilder 传递给部分

发布于 2024-12-02 06:45:07 字数 1890 浏览 8 评论 0原文

我正在尝试根据某个 AJAX 请求动态创建表单元素。

这是我的设置:

View:

    <%= link_to 'Next', check_unique_id_students_path, :remote => true %>

    <div id="guardian_student_details"></div>

Controller:

def check_unique_id
    @student = Student.new
    @this_form = ActionView::Helpers::FormBuilder.new(:student, @student, @template, {}, proc{})
  end

JS:

jQuery("#guardian_student_details").html("<%=escape_javascript(render :partial => "student_details", :locals => { :s => @this_form }) %>");

Partial:

<% puts s.text_field :first_name   %>
<% puts s.field_helpers   %>

为了调试目的,我将以下几行放在我的partial的最开始处:

<% puts s.class.to_s %>
<% puts s.object.to_s %>

这打印出:

ActionView::Helpers::FormBuilder
Student

这应该可以工作。但是,rails 给出以下错误:

ActionView::Template::Error (undefined method `text_field' for nil:NilClass):
1: <% puts s.class.to_s   %>
2: <p>
3: <%= s.text_field :first_name, :class => 'text_input is_empty' %>
4: <%= s.label :first_name %><strong>*</strong> 
5: </p>
6: 

app/views/students/_student_details.html.erb:3:in _app_views_students__student_details_html_erb__2485891544130782916_2214680440' app/views/students/check_unique_id.js.erb:2:in_app_views_students_check_unique_id_js_erb__3504800328150418937_2214933160'

这意味着“s”是 NIL 我之前仅验证了两行。有人有什么想法吗?我不知道这是否与控制器中初始化的“@template”变量有关。我玩过并且几乎接受任何东西,如果打印出来则为零。 任何帮助将不胜感激。谢谢

最后说明:

我尝试实现此:AJAX update of Accepts_nested_attributes_forpartials< /a>

I am atempting to dinamically create form elements given a certain AJAX request.

This is my setup:

View:

    <%= link_to 'Next', check_unique_id_students_path, :remote => true %>

    <div id="guardian_student_details"></div>

Controller:

def check_unique_id
    @student = Student.new
    @this_form = ActionView::Helpers::FormBuilder.new(:student, @student, @template, {}, proc{})
  end

JS:

jQuery("#guardian_student_details").html("<%=escape_javascript(render :partial => "student_details", :locals => { :s => @this_form }) %>");

Partial:

<% puts s.text_field :first_name   %>
<% puts s.field_helpers   %>

For debugging purposes i placed the following lines at the very beginning of my partial:

<% puts s.class.to_s %>
<% puts s.object.to_s %>

This prints out :

ActionView::Helpers::FormBuilder
Student

This should work. However rails is giving the following error:

ActionView::Template::Error (undefined method `text_field' for nil:NilClass):
1: <% puts s.class.to_s   %>
2: <p>
3: <%= s.text_field :first_name, :class => 'text_input is_empty' %>
4: <%= s.label :first_name %><strong>*</strong> 
5: </p>
6: 

app/views/students/_student_details.html.erb:3:in _app_views_students__student_details_html_erb__2485891544130782916_2214680440'
app/views/students/check_unique_id.js.erb:2:in
_app_views_students_check_unique_id_js_erb__3504800328150418937_2214933160'

Which implies that "s" is NIL something I verified just 2 lines before. Does anybody have any ideas? i dont know if this has something to do with the "@template" variable initialized in the controller. Which i played around with and accepts practically anything and if printed is nil.
Any help would be appreciated. Thanks

Final note:

I tried to implement this: AJAX update of accepts_nested_attributes_for partials

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

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

发布评论

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

评论(3

老娘不死你永远是小三 2024-12-09 06:45:07

对于任何需要在控制器中构建表单构建器的人来说,view_context 仍然可以在那里工作。使用 Rails 4.1.4:

@object = Object.new
@f = ActionView::Helpers::FormBuilder.new(:object, @object, view_context, {})

For anyone needing to build a form builder in the controller, view_context still works there. Using Rails 4.1.4:

@object = Object.new
@f = ActionView::Helpers::FormBuilder.new(:object, @object, view_context, {})
空城旧梦 2024-12-09 06:45:07

在视图中,我发现“view_context”在 Rails 3.1 中不起作用。相反,在创建 FormBuilder 对象时尝试“self”。

s = ActionView::Helpers::FormBuilder.new(:student, @student, self, {}, proc{})

In the view, I've found that 'view_context' does not work in Rails 3.1. Instead try 'self' when creating a FormBuilder object.

s = ActionView::Helpers::FormBuilder.new(:student, @student, self, {}, proc{})

梦亿 2024-12-09 06:45:07

在控制台中尝试此操作:

s = ActionView::Helpers::FormBuilder.new(:student, @student, @template, {}, proc{})
s.text_field :first_name

您将遇到相同的错误。我认为问题来自于您创建的 form_builder 对象,即使我不知道确切的错误......

在我看来,您的解决方案有点复杂。你可以尝试这个解决方案:

#html.erb
<% form_for @student do |f| %>
  <div id='guardian_student_details' class='hide-this-block'>
    <%= render :partial => "student_details", :locals => { :s => f }) %>
  </div>
<% end %>

#js
jQuery("#guardian_student_details").show();

一般来说,我更喜欢将 javascript 和 ruby​​ 分开。

Try this in a console :

s = ActionView::Helpers::FormBuilder.new(:student, @student, @template, {}, proc{})
s.text_field :first_name

You will have the same error. I think the problem come from your creation of the form_builder object, even if I don't know the exact mistake...

Your solution seems to me to be a little much complex. You can try this solution :

#html.erb
<% form_for @student do |f| %>
  <div id='guardian_student_details' class='hide-this-block'>
    <%= render :partial => "student_details", :locals => { :s => f }) %>
  </div>
<% end %>

#js
jQuery("#guardian_student_details").show();

Generally, I prefer keep javascript and ruby separated.

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