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.
source share