[英]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. 在我的rails应用程序中,我正在尝试添加一个联系人用户弹出框,向用户发送电子邮件消息。
I have the javascript in place to display the popup form, which is itself a partial. 我有javascript来显示弹出窗体,这本身就是一个部分。
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. 如果您通过AJAX提交表单,则可以执行您要执行的操作。
Adding :remote => true
to your form will accomplish this: 添加
:remote => true
表单将完成此操作:
= form_tag({:controller => 'users', :action => 'contact_user'}, :method => 'put', :remote => true) do
Now, the form submission will hit your controller as an AJAX request. 现在,表单提交将作为AJAX请求命中您的控制器。 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). 然后你应该能够像你建议的那样渲染任何东西,甚至做渲染一个
.js.erb
来执行一些Javascript代替(比如,隐藏你正在谈论的弹出窗口)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.