我的 flash 消息出现了两次,我的网络研究告诉我这是由于渲染和重定向显示消息。我想我需要在某处使用 flash.now[] 或 flash[] 对其进行排序,但我无法确定它需要去哪里
指南控制器.rb
def update
@guideline = Guideline.find(params[:id])
respond_to do |format|
if @guideline.update_attributes(params[:guideline])
@guideline.update_attribute(:updated_by, current_user.id)
format.html { redirect_to @guideline, notice: 'Guideline was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "show" }
format.json { render json: @guideline.errors, status: :unprocessable_entity }
end
end
end
布局/application.html.erb
<div class="container">
<% flash.each do |type, message| %>
<div class="alert <%= flash_class type %>">
<button class="close" data-dismiss="alert">x</button>
<%= message %>
</div>
<% end %>
</div>
application_helper.rb
def flash_class(type)
case type
when :alert
"alert-error"
when :notice
"alert-success"
else
""
end
end
guideline_controller.rb
def show
@guideline = Guideline.find(params[:id])
if @guideline.updated_by
@updated = User.find(@guideline.updated_by).profile_name
end
if User.find(@guideline.user_id)
@created = User.find(@guideline.user_id).profile_name
end
respond_to do |format|
format.html # show.html.erb
format.json { render json: @guideline }
end
end