How to perform an action inside a page in Rails without rendering a new page

In my rails app, I am trying to add a user contact popup that delivers an email message to the user.

I have javascript to display a popup form that is partial by itself.

When the user clicks the submit button on the contact form, they invoke a controller action that sends mail. After submitting the post, I want to stay on the same page but hide the popup. My problem is that I cannot get the controller action that is delivering the mail to not display its own view. I tried

render nothing: true

      

But that just makes a blank page.

My form is set up like this

= form_tag({:controller => 'users', :action => 'contact_user'}, :method => 'put') do 

      

And in my route settings I have

resources :users
  collection do
     put 'contact_user'
  end

      

+3


source to share


1 answer


If you are submitting your form via AJAX, you can do what you are trying to do.

Adding :remote => true

to your form will do the following:



= form_tag({:controller => 'users', :action => 'contact_user'}, :method => 'put', :remote => true) do 

      

Now the form submission will hit your controller as an AJAX request. Then you should either do nothing as you suggested, or even do something like rendering .js.erb

to execute some Javascript instead (say, to hide the popup you're talking about).

+7


source







All Articles