Reputation: 295
EDIT: I changed the code slightly.
I know this is a repeated question here, but I was not able to find the one that fits my problem. I think I am missing something, but I am not able to guess what.
I have a form to create some elements in my app, but I want that form to be executed with :remote => true
Form rendered in events/edit.html.erb
. This form is to create event_criteria_options
<%= form_for(@event_criteria_option, :remote => true) do |f| %>
...
<% end %>
This is the create action in event_criteria_options_controller.rb
def create
@event_criteria_option = EventCriteriaOption.new(params[:event_criteria_option])
respond_to do |format|
if @event_criteria_option.save
format.html { redirect_to edit_event_path(Event.find(session[:event_id]), :notice => 'Event criteria option was successfully created.') }
format.xml { render :xml => @event_criteria_option, :status => :created, :location => @event_criteria_option }
format.js
else
format.html { render :action => "new" }
format.xml { render :xml => @event_criteria_option.errors, :status => :unprocessable_entity }
format.js
end
end
end
and a js.erb file called create.js.erb
$('#show_event_criteria').html('<%=raw escape_javascript(render(:partial => "show_event_criteria")) %>');
rails.js
, applicationl.js
and jquery.js
files are included in the page, but when the form is submitted is doing HTML call, not AJAX. I checked the HTML code and data-remote="true"
exists in the form. I have other forms submiting with :remote => true
in the app and they work well. I’m using Rails 3.0.1 and Ruby 1.8.7
Upvotes: 1
Views: 1952
Reputation: 604
3 years late to the party but I have come accross this problem as well. For remote true to submit xhr requests, you need to have the rails jquery gem installed. HOWEVER, in your application.js file, you need to require BOTH:
//= require jquery
//= require jquery_ujs
the ujs is the bit that made remote true work with xhr requests. Mind. Blown.
Upvotes: 1
Reputation: 10898
I believe your javascript should be in update.js.erb
since that is the action your form will post to?
UPDATE:
You also need to detect whether it is an ajax call in your update method so that the controller handles everything correctly:
if request.xhr?
# respond to Ajax request
else
# respond to normal request
end
Upvotes: 2