简体   繁体   中英

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

In my rails app I'm trying to add a contact user popup box which delivers an email message to a user.

I have the javascript in place to display the popup form, which is itself a partial.

When the user clicks submit on the contact form it calls a controller action which delivers the mail. After submitting the message I want to stay on the same page, but hide the popup box. My problem is that I can't get the controller action which delivers the mail not to render its own view. I tried

render nothing: true

But that simply renders a blank page.

My form is set up as follows

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

And in my routes config I have

resources :users
  collection do
     put 'contact_user'
  end

If you submit your form via AJAX, you can do what you're trying to do.

Adding :remote => true to your form will accomplish this:

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

Now, the form submission will hit your controller as an AJAX request. You should then be able to either render nothing, as you suggested, or even do something like render a .js.erb to execute some Javascript instead (say, to hide the popup you're talking about).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM