我是rails和jQuery的初学者.我在一个页面中有两个单独的表单,我想以ajax方式(使用jQuery)分别提交它们.这就是我走的路.任何人都可以添加或修复此代码以使其正常工作.我使用的是Rails 3.1和jQuery 1.6.提前谢谢你.
application.js
$(".savebutton").click(function() {
$('form').submit(function() {
$(this).serialize();
});
});
第一表格:
<%=form_for :users do |f| %>
<fieldset>
<legend>Basic details</legend>
<%= f.label :school %>
<%= f.text_field :school,:size=>"45",:class=>"round",:id=>"school" %><br/>
</fieldset>
<p><%= button_to "save and continue",{:class=>"savebutton"} %></p>
<%end%>
第二种形式:
<%=form_for :courses do |c| %>
<fieldset>
<legend>Your current classes</legend>
<label>class:</label><%= c.text_field :subject,:size=>"45",:class=>"round" %><br/>
</fieldset>
<p><%= button_to "save and continue",{:class=>"savebutton"} %></p>
<%end%>
学校控制器
class 学校控制器 < ApplicationController
respond_to :json
def create
@school = current_user.posts.build(params[:school].merge(:user => current_user))
if @school.save
respond_with @school
else
respond_with @school.errors, :status => :unprocessable_entity
end
end
end
CourseController is in the same shape as 学校控制器