I'm trying to update the values of a select statement when another field is changed.
This is my code:
Form (_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 %>
partial with field to be updated:
<% unless @projectmilestone.department_id.nil? %>
<% form.inputs :partial do %>
<%= form.input :user_id , :as => :select, :collection => @users %>
<% end %>
<% end %>
jQuery function in my application.js file:
$('.submittable').live('change', function() {
$(this).parents('form:first').submit();
return false;
});
Controller:
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 file in my views:
$('#projectmilestone_department_id').css("color","red");
$("#stakeholders").replaceWith("<%= escape_javascript( render :partial => "stakeholders_form", :locals => { :form => form } ) %>");
The partial loads fine at first page load but when loaded through the javascript, I get following error:
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/projec开发者_如何学Gotmilestones/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'
The javascript UJS loop works ok too, as the css is changed to red when I change the department_id field.
I guess the error has something to do with the locals but i don't know what.
Please help!!!
Thx,
Nicolas.
When you call the update.js.erb you don't have a form
object so this will fail.
Perhaps you could look at the HTML that the partial churns out then rewrite it so that takes @projectmilestone
and @users
as parameters.
Your update method has both of these parameters available so should work.
精彩评论