将ActionView :: Helpers :: FormBuilder传递给partial [英] Passing ActionView::Helpers::FormBuilder to a partial

查看:209
本文介绍了将ActionView :: Helpers :: FormBuilder传递给partial的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在给定某个AJAX请求的情况下创建表单元素。



这是我的设置:

查看:

 <%= link_to'下一步',check_unique_id_students_path,:remote => true%> 

< div id =guardian_student_details>< / div>

控制器:

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

JS:

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

部分:

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

出于调试的目的,我在我的部分开头部分放置了以下几行:

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

打印出来:

  ActionView :: Helpers :: FormBuilder 
Student

工作。但是,轨道提供了以下错误:

  ActionView :: Template :: Error(nil:NilClass的未定义方法`text_field') :
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,我之前只验证了2行。有人有任何想法吗?我不知道这是否与在控制器中初始化的@template变量有关。我玩过并接受几乎任何东西,如果打印是零。
任何帮助,将不胜感激。谢谢


最后说明:

我试图实现这一点: recensted_attributes_for partials的AJAX更新

解决方案

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

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


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

解决方案

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, {})

这篇关于将ActionView :: Helpers :: FormBuilder传递给partial的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆