[英]rails: redirect_to rendering but not reloading new page
I would like my application to show a list of all associated objects when one is updated, so I would like to load the index action after an update is completed. 我希望我的应用程序在更新一个对象时显示所有关联对象的列表,因此我想在更新完成后加载index动作。
I have tried formatting the block, removing the format, render, and redirect_to. 我尝试格式化该块,删除格式,渲染和redirect_to。 All of them just remain on the edit page 所有这些都保留在编辑页面上
Update action: 更新动作:
def update
respond_to do |format|
if @business_category.update_attributes(business_category_params)
format.html {redirect_to admin_business_categories_path}
return
end
end
end
edit view: 编辑视图:
<div class="container">
<div class="row">
<div class="col-xs-12">
<%= link_to 'Back to categories', admin_business_categories_path %>
</div><!-- .col -->
<%= simple_form_for(@business_category, url: admin_business_category_path(@business_category), remote: true, html: { class: '' }) do |f| %>
<%= render 'form', f: f %>
<% end %>
</div><!-- .row -->
</div><!-- .container -->
_form partial: _form部分:
<div class="col-xs-12 col-sm-10 col-md-10">
<%= f.input :name, label: 'Category Name' %>
</div>
<div class="col-xs-12 col-sm-2 col-md-2">
<div class="btn-group-vertical" role="group" aria-label="...">
<button id="businessCategoryCancelButton" class="btn btn-warning">CANCEL</button>
<%= f.submit 'SAVE', class: 'btn btn-success' %>
<br>
</div>
</div>
at the redirect_to a message appears in the console: 在redirect_to处,控制台中将显示一条消息:
No template found for Admin::BusinessCategoriesController#update, rendering head :no_content
Completed 204 No Content in 1505ms (ActiveRecord: 1.0ms)
I don't know why it is looking for the update template or why it is not redirecting to the index action 我不知道为什么它要寻找更新模板或为什么它不重定向到索引动作
I am trying to understand how format works and if/why it would be conflicting with the redirect. 我试图了解格式如何工作以及是否/为什么与重定向冲突。 Any advice would be helpful 任何意见将是有益的
Because you are issuing an AJAX call ( remote: true
on your form). 因为您正在发出AJAX调用( remote: true
在表单上为remote: true
)。
You have the following options: 您有以下选择:
format.js
to the controller 将format.js
添加到控制器 remote: true
from the form definition and the respond_to
from the controller: 从表单定义中删除remote: true
,从控制器中删除respond_to
: def update
if @business_category.update_attributes(business_category_params)
redirect_to admin_business_categories_path
else
render :edit
end
end
simple_form_for(@business_category, url: admin_business_category_path(@business_category), remote: true, html: { class: '' })
In your form you've mentioned remote: true
. 在您的表单中,您提到过remote: true
。 It process your request as a JS
request. 它将您的请求作为JS
请求处理。 In your controller you've mentioned format.html {redirect_to admin_business_categories_path}
but it will process it as format.js
and look for update.js.erb
file to handle the response because your format of request is 'JS' instead of 'HTML', hence it's showing an error. 在您的控制器中,您提到过format.html {redirect_to admin_business_categories_path}
但它将以format.js
处理,并查找update.js.erb
文件来处理响应,因为您的请求格式为“ JS”而不是“ HTML” ,因此显示错误。
You'll have to send the request as an HTML request. 您必须将请求作为HTML请求发送。
According to your implementation. 根据您的执行情况。 I think you just want to redirect in case of success and render edit page again in case of error. 我认为您只是想在成功的情况下进行重定向,并在出现错误的情况下再次呈现编辑页面。
You'll have to make 2 changes. 您必须进行2项更改。
format.html { render :edit }
in else case of update_attributes()
在update_attributes()
情况下,添加格式format.html { render :edit }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.