Rails:重定向问题。遇到奇怪的 302 状态码
Rails: redirect issues. Encounter weird 302 status code
在按照 rails 路线构建整个 CRUD 系统时,我遇到了一个问题,即在执行创建、更新和删除操作后会出现 302 状态代码。
该页面将被重定向到“显示”而不是我编码的“索引”。
controller.rb
class CandidatesController < ApplicationController
def index
@candidates = Candidate.all
end
def new
@candidate = Candidate.new
end
def create
@candidate = Candidate.new(filtered)
if @candidate.save
flash[:notice] = "Successfully Added!"
redirect_to 'candidates_path'
else
render :new
end
end
def show
@candidate = Candidate.find_by(id: params[:id])
end
def edit
@candidate = Candidate.find_by(id: params[:id])
end
def update
p @candidate = Candidate.find_by(id: params[:id])
if @candidate.update(filtered)
flash[:notice] = "Candidate Updated :)"
redirect_to 'candidates_path'
else
render :edit
end
end
def destroy
p @candidate = Candidate.find_by(id: params[:id])
@candidate.destroy
flash[:notice] = "Successfully Deleted!"
redirect_to 'candidates_path'
end
private
def filtered
params.require(:candidate).permit(:name, :party, :age, :politics)
end
end
观看次数:指数
<h1> Candidate Index </h1>
<div><%= link_to 'Add new candidate', new_candidate_path %></div>
<div><%= flash[:notice] %> </div>
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Party</td>
<td>Politics</td>
</tr>
<% @candidates.each do |candidate| %>
<tr>
<td><%= link_to candidate.name, candidate_path(candidate.id) %></td>
<td><%= candidate.age %></td>
<td><%= candidate.party %></td>
<td><%= candidate.politics %></td>
<td><%= link_to 'edit', edit_candidate_path(candidate.id) %></td>
<td><%= link_to 'delete', candidate_path(candidate.id), method: 'delete', data: {confirm: "Please confirm delete"} %></td>
</tr>
<% end %>
</table>
观看次数:显示
<h1> Candidate Info </h1>
<% if @candidate %>
<tr>
<td><%= @candidate.name %></td>
<td><%= @candidate.age %></td>
<td><%= @candidate.party %></td>
<td><%= @candidate.politics %></td>
</tr>
<% else %>
<h2>No record found </h2>
<% end %>
<div>
<%= link_to 'HOME', candidates_path %>
</div>
**留言**
来自浏览器
并且刷新页面后,它总是转到“显示”而不是“索引”。
已创建路线
有人知道为什么吗?
使用candidates_path
方法,而不是'candidates_path'
字符串:
redirect_to candidates_path
在按照 rails 路线构建整个 CRUD 系统时,我遇到了一个问题,即在执行创建、更新和删除操作后会出现 302 状态代码。 该页面将被重定向到“显示”而不是我编码的“索引”。
controller.rb
class CandidatesController < ApplicationController
def index
@candidates = Candidate.all
end
def new
@candidate = Candidate.new
end
def create
@candidate = Candidate.new(filtered)
if @candidate.save
flash[:notice] = "Successfully Added!"
redirect_to 'candidates_path'
else
render :new
end
end
def show
@candidate = Candidate.find_by(id: params[:id])
end
def edit
@candidate = Candidate.find_by(id: params[:id])
end
def update
p @candidate = Candidate.find_by(id: params[:id])
if @candidate.update(filtered)
flash[:notice] = "Candidate Updated :)"
redirect_to 'candidates_path'
else
render :edit
end
end
def destroy
p @candidate = Candidate.find_by(id: params[:id])
@candidate.destroy
flash[:notice] = "Successfully Deleted!"
redirect_to 'candidates_path'
end
private
def filtered
params.require(:candidate).permit(:name, :party, :age, :politics)
end
end
观看次数:指数
<h1> Candidate Index </h1>
<div><%= link_to 'Add new candidate', new_candidate_path %></div>
<div><%= flash[:notice] %> </div>
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>Party</td>
<td>Politics</td>
</tr>
<% @candidates.each do |candidate| %>
<tr>
<td><%= link_to candidate.name, candidate_path(candidate.id) %></td>
<td><%= candidate.age %></td>
<td><%= candidate.party %></td>
<td><%= candidate.politics %></td>
<td><%= link_to 'edit', edit_candidate_path(candidate.id) %></td>
<td><%= link_to 'delete', candidate_path(candidate.id), method: 'delete', data: {confirm: "Please confirm delete"} %></td>
</tr>
<% end %>
</table>
观看次数:显示
<h1> Candidate Info </h1>
<% if @candidate %>
<tr>
<td><%= @candidate.name %></td>
<td><%= @candidate.age %></td>
<td><%= @candidate.party %></td>
<td><%= @candidate.politics %></td>
</tr>
<% else %>
<h2>No record found </h2>
<% end %>
<div>
<%= link_to 'HOME', candidates_path %>
</div>
**留言**
来自浏览器
并且刷新页面后,它总是转到“显示”而不是“索引”。
已创建路线
有人知道为什么吗?
使用candidates_path
方法,而不是'candidates_path'
字符串:
redirect_to candidates_path