I am new to rails and jQuery. I have two separate forms on one page, and I want to submit them separately by ajax (with jQuery). This is how far I got. Can someone add or fix this code to make it work. I am using Rails 3.1 and jQuery 1.6. Thank you in advance.
application.js
$(".savebutton").click(function() { $('form').submit(function() { $(this).serialize(); }); });
first form:
<%=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%>
second form:
<%=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%>
Schoolcontroller
class SchoolController < 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 in the same form as SchoolController
jquery ajax ruby-on-rails-3
katie Jul 17 '11 at 10:45 2011-07-17 10:45
source share