4

I’m using Rails 4.2.3. I want to submit a form in a modal dialog, so I have set up my form like so

<%= form_for @my_object, :remote => true do |f| %>

but if the user submits the form successfully, I would like to reload the page that invoked the modal dialog with a notice of “Saved Successfully.” I can’t figure out what I need to put in my “format.js” to make this happen. This is what I have in my controller so far …

  def create
    @my_object = MyObject.new(my_object_params)
    @current_user = User.find(session["user_id"])
    @my_object.user = @current_user
    respond_to do |format|
      if @my_object.save
        format.html { redirect_to controller: "users", action: "index", notice: 'Saved successfully.' }
        format.js   { render action: ‘../users/index’, notice: ‘Saved Successfully’, location: @my_object }
      else
        format.html { render action: "index" }
        format.js   { render json: @my_object.errors, status: :unprocessable_entity }
      end
    end
  end

Right now, a successful submission results in a 500 error complaining about missing partials when I try and execute the above. Pretty sure what I have is wrong anyway.

Dave
  • 15,639
  • 133
  • 442
  • 830
  • 2
    Why do not want to use simple html version without `remote: true` ? if u want to redirect it. – 7urkm3n May 27 '16 at 23:07
  • Agreed with @7urkm3n. Adding a redirect/reload defeats the purpose of using a remote form. – vich May 28 '16 at 05:32
  • Per my comment to the other poster, if there are validation errors, I want those to be displayed in the modal window. Otherwise, I would have to reload my page and respawn the window and that looks messy. – Dave May 28 '16 at 17:34

5 Answers5

12

You can do the following:

#app/controllers/redirect.rb
...

format.js { render js: "window.location='#{url.to_s}'" }

...

If you like keeping things separated, just put format.js in your controller and do the javascript redirect in your view (redirect.js.erb)

In both cases, just set flash[:notice] to whatever you need before redirecting.

chrisandrew.cl
  • 867
  • 14
  • 30
3

redirect_to events_path, format: 'js'

For this you will need to have events/index.js.erb in your file structure.

teju c
  • 336
  • 2
  • 8
0

If you are redirecting anyway, you might as well avoid the remote/AJAX call, and just redirect from the create action.

<%= form_for @my_object do |f| %>

and

def create
  @my_object = MyObject.new(my_object_params)
  ...
  redirect_to some_path
end
Anand
  • 3,690
  • 4
  • 33
  • 64
  • 1
    But in this situation, how do I display validation errors? If I'm submitting it without AJAX, I have to reload the page, then respawn the modal window and I don't like the way that looks. – Dave May 28 '16 at 17:24
0

If you have want to redirect it after successfully create/updated and just use .html method. Otherwise just use JS option like in this LINK.

  def create
    @my_object = MyObject.new(my_object_params.merge(user: User.find(session["user_id"])))
    respond_to do |format|
      if @my_object.save
        format.html { redirect_to controller: "users", action: "index", notice: 'Saved successfully.' }
      else
      ....
      end
    end
  end
Community
  • 1
  • 1
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
  • You're saying just remove my "format.js" block in the success branch? I tried taht, but it didn't work. I saw this in my log, "Processing by UsersController#index as JS" but yet on my client side nothing happened. – Dave May 28 '16 at 17:33
  • 1
    Yeah but if I do that, how will I dynamically display validation errors? Since this is a modal window, submitting a normal form would require my reloading the page and respawning that window, and per my other comments, that just looks bad. – Dave May 29 '16 at 21:27
  • @Dave, `render` is not refreshing a page, but if its in modal then u gotta make it in `JS`, try `format.json{render json: @obj.errors}` and handle it through ajax. it should be if not success ! – 7urkm3n May 30 '16 at 20:49
0

That will help you, from your controller

render :js => "window.location = '/jobs/index'"

Félix Pujols
  • 107
  • 1
  • 7