Dav*_*ave 4 ajax redirect ruby-on-rails remote-forms ruby-on-rails-4
我正在使用Rails 4.2.3.我想在模态对话框中提交一个表单,所以我已经设置了我的表单
<%= form_for @my_object, :remote => true do |f| %>
Run Code Online (Sandbox Code Playgroud)
但是如果用户成功提交表单,我想重新加载调用模式对话框的页面,并注意"已成功保存".我无法弄清楚我需要在"format.js"中添加什么才能生成这发生了.到目前为止,这就是我在控制器中所拥有的......
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
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试执行上述操作时,成功提交会导致500错误,抱怨缺少部分错误.无论如何,很确定我的错误.
您可以执行以下操作:
#app/controllers/redirect.rb
...
format.js { render js: "window.location='#{url.to_s}'" }
...
Run Code Online (Sandbox Code Playgroud)
如果你喜欢将事物分开,只需放入format.js你的控制器并在你的视图中进行javascript重定向(redirect.js.erb)
在这两种情况下,只需将flash [:notice]设置为重定向前所需的任何内容.