Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Remote form validation errors not showing

I have created an ajax form in Rails 4 using remote: true. For some reason that I'm missing, my validation errors are not showing in the form.

Here is my current codebase:

1. Form

= simple_form_for @foo, url: foos_path, as: :foo, remote: true do |f|

  .row
    .input-field
      = f.input :bar

  .right-align.card-button
    = f.button :submit, class: "waves-light blue", data: { disable_with: "Loading..." }

2. create.js.erb

$("#new_foo").html('<%= j render_cell :foo, :form, foo: @foo %>');

3. Controller

def create
  @foo = FooForm.new( Foo.new )
  respond_to do |format|
    if @foo.validate(params[ :country ])
        @foo.save
        format.html { redirect_to root_path }
        format.json { render :show, status: :created, location: @foo.model }
        format.js 
    else
      format.html { render :new }
      format.json { render json: @foo.errors, status: :unprocessable_entity }
      format.js 
    end
  end
end

I would expect this code to show validation errors after a for being submitted incorrectly. Yet for some reason nothing happens after an incorrect submission. I can confirm that the form is being submitted however since the submission is processed in the log.I can also confirm that the form works perfectly fine when I remove remote submission.

Is there anything that I'm missing?

PS. I am using the Reform gem which might make my controller object code seem a little bit unfamiliar to a lot of people. I am rendering a cell in my js file as per this gem

UPDATE I have struggled with this some more. I implemented a solution that I've copied a 'remote: true' solution that I've built in a previous app, verbatim. This worked flawlessly in my previous app but does not display validation errors in my new app. The only difference between these two apps are the version of Ruby and Rails.

In the first app I ran Ruby: 2.2.1 with Rails: 4.2.2. My new app ('the one that I can't get working') I'm running Ruby: 2.2.3 with Rails: 4.2.4.

Could this perhaps have an impact??

like image 884
HermannHH Avatar asked Jan 13 '16 05:01

HermannHH


1 Answers

After many hours I have finally managed to solve this issue. Still not sure why this is happening but I am posing the answer here in case somebody else ever experiences something similar. This Stackoverflow answer led me in the right direction.

I needed to add this line into my create action:

format.js    { render layout: false, content_type: 'text/javascript' }

Thus my full create action now looks like this:

def create
  @foo = FooForm.new( Foo.new )
  respond_to do |format|
    if @foo.validate(params[ :country ])
        @foo.save
        format.html { redirect_to root_path }
        format.json { render :show, status: :created, location: @foo.model }
        format.js 
    else
      format.html { render :new }
      format.json { render json: @foo.errors, status: :unprocessable_entity }
      format.js   { render layout: false, content_type: 'text/javascript' }
    end
  end
end
like image 146
HermannHH Avatar answered Sep 24 '22 02:09

HermannHH