rails jquery formtastic partial =>未定义的方法'输入'

时间:2011-05-09 14:36:24

标签: jquery ruby-on-rails partial formtastic

我正在尝试在更改另一个字段时更新select语句的值。

这是我的代码:

表格(_form.html):

<%= semantic_form_for [:projects,@projectmilestone], :remote => true  do |form| %>

    <%= form.semantic_errors :state %>
    <%= form.inputs do %>
      <%= form.input :name, :as => :string%>
      <%= form.input :description, :as => :text%>
      <%= form.input :department_id, :as => :select, :collection => Department.all, :wrapper_html => { :class => "submittable" }%>
      <%= form.input :custom, :as => :hidden%>
      <%= form.input :projectcapstone_id , :as => :hidden %>
      <div id="stakeholders">
        <%=render :partial => "stakeholders_form", :locals => { :form => form }%>
      </div>
      <%= form.input :statusweight, :as => :numeric%>
      <%= form.input :ratingweight, :as => :numeric%>


    <%  end %>

    <%= form.submit "Submit" %>

<% end %>

部分包含要更新的字段:

<% unless @projectmilestone.department_id.nil? %>
  <% form.inputs :partial do %>
    <%= form.input :user_id , :as => :select, :collection => @users %>
  <% end %>
<% end %>

我的application.js文件中的jQuery函数:

$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
return false;
});

控制器:

  def update
    @projectmilestone = Projectmilestone.find(params[:id])
    @users= User.where("department_id = ?",@projectmilestone.department_id) unless @projectmilestone.department_id.nil?
    respond_to do |format|
      if @projectmilestone.update_attributes(params[:projectmilestone])
        format.js {render :layout => false}
      end
    end
  end

我的观点中的update.js.erb文件:

$('#projectmilestone_department_id').css("color","red");
$("#stakeholders").replaceWith("<%= escape_javascript( render :partial => "stakeholders_form", :locals => { :form => form } ) %>");

首页加载时部分加载正常,但是当通过javascript加载时,我收到以下错误:

ActionView::Template::Error (undefined method `inputs' for #<String:0x00000117e81940>):
    1: <% unless @projectmilestone.department_id.nil? %>
    2:   <% form.inputs :partial do %>
    3:     <%= form.input :user_id , :as => :select, :collection => @users %>
    4:   <% end %>
    5: <% end %>
  app/views/projects/projectmilestones/_stakeholders_form.html.erb:2:in `_app_views_projects_projectmilestones__stakeholders_form_html_erb__1391421669166525649_2348024640__1321038554949321784'
  app/views/projects/projectmilestones/update.js.erb:4:in `_app_views_projects_projectmilestones_update_js_erb__638343879728010384_2348034340_1083018837200874056'
  app/controllers/projects/projectmilestones_controller.rb:88:in `block (2 levels) in update'
  app/controllers/projects/projectmilestones_controller.rb:86:in `update'

javascript UJS循环也正常,因为当我更改department_id字段时,css会变为红色。

我猜这个错误与当地人有关,但我不知道是什么。

请帮助!!!

THX,

尼古拉斯。

1 个答案:

答案 0 :(得分:0)

当您调用update.js.erb时,您没有form对象,因此这将失败。

也许您可以查看部分生成的HTML然后重写它,以便将@projectmilestone@users作为参数。

您的更新方法可以使用这两个参数,因此应该可以使用。