Is there a way to send ajax / json requests using simple_form for Rails

With standard Rails form_for, I was able to pass ajax requests, although select and collection_select helpers as such:

<%= address.collection_select :country, @countries, :id, :name, {:include_blank => false}, "data-remote" => true, "data-url" => "/ajax/states", "data-type" => :json %> 

I cannot figure out how to do this with simple_form

+6
source share
5 answers

Figured it out. You just need to add the following:

 :input_html => {"data-remote" => true, "data-url" => "/yoururl", "data-type" => :json} 
+14
source

I wanted to publish the corresponding sequel because it was hard for me to figure out how to implement a callback for this. I found this article very useful: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

The secret is to add an HTML5 data attribute, for example. data-complete or (better) link the ajax:complete or ajax:success callbacks provided by Rails to your form (see 4. Remote JavaScript callbacks in the article above):

From the article:

 jQuery(function($) { // create a convenient toggleLoading function var toggleLoading = function() { $("#loading").toggle() }; $("#tool-form") .bind("ajax:loading", toggleLoading) .bind("ajax:complete", toggleLoading) .bind("ajax:success", function(event, data, status, xhr) { $("#response").html(data); }); }); 

CoffeeScript Example:

 $("#new_post").on("ajax:success", (e, data, status, xhr)-> console.log data ) 
+6
source

Better to write like this:

 = simple_form_for sample_result, url: reject_sample_result_path(sample_result), method: :post, remote: true, input_html: {multipart: true} do |f| 

When you explicitly declare the data-url , it will still first compute the default route, which in my case failed because the default routes did not exist (and should not exist - as I am intercepting the URL). When you declare only url , it will just take the specified URL.

+6
source

It seems like the right way to do it now:

 :remote => true, :html => { :data => { :url => '/yoururl', :type => :json } } 

which might look a little better with the ruby ​​1.9 hash syntax like:

 remote: true, html: { data: { url: '/yoururl', type: :json } } 

https://github.com/plataformatec/simple_form/wiki/HTML5-Attributes

+2
source

The easiest way to do this, which I found, and for a more complete example:

In your opinion:

 <%= simple_form_for :my_object, url: my_objects_path(format: :json), remote: true do |f| %> <%= f.error_notification %> <%= f.input :an_attribute %> <%= f.submit %> <% end %> 

and in your controller:

 def create @my_object = MyObject.new(my_object_params) if @my_object.save respond_to do |format| format.html { redirect_to @my_object, notice: "Saved" } format.json { render json: @my_object, location: my_object_url(@object), status: :created } end else respond_to do |format| format.html { render :edit } format.json {render json: @my_object, status: :unprocessable_entity } end end end 

In Rails 5, this is even easier on a controller with Jbuilder installed to create simple json hashes, but this should work too.

0
source

Source: https://habr.com/ru/post/913760/


All Articles