我可以在创建和销毁后将Flash消息传递到重定向到root_path但在由于某种原因更新重定向到show.html.erb时无法使其工作。您认为这个问题是什么?我的报价模型有4列(id,category,author和quotetext)
我的节目视图
<% if !flash[:notice].blank? %>
<div class="notice">
<%= flash[:notice] %>
</div>
<% end %>
<% @category.each do |quote| %>
<div>
<p><strong> <%= quote.category %></strong></p>
<p><em><%= quote.author %></em></p>
<p> <%= quote.text %> </p>
<hr>
</div>
<% end %>
<%= link_to 'Home', quotes_path %>
我的控制器:
class QuotesController < ApplicationController
before_action :set_quote, only: [:show, :edit, :update, :destroy]
# GET /quotes
# GET /quotes.json
def index
@quotes = Quote.sorted
end
# GET /quotes/1
# GET /quotes/1.json
def show
@quote = Quote.find(params[:id])
end
# GET /quotes/new
def new
@quote = Quote.new
#apparently this doesn't really do anything but good to specify
end
def create
@quote = Quote.new(quote_params)
if @quote.save
flash[:notice] = "Quote #{@quote.pkey} created!"
redirect_to root_path
else
render('new')
end
end
# GET /quotes/1/edit
def edit
@quote = Quote.find(params[:id])
end
# PATCH/PUT /quotes/1
# PATCH/PUT /quotes/1.json
def update
@quote = Quote.find(params[:id])
if @quote.update_attributes(quote_params)
flash[:notice] = "Quote #{@quote.pkey} updated!"
redirect_to(:action => 'show', :id => @quote.id)
else
render('edit')
end
end
# DELETE /quotes/1
# DELETE /quotes/1.json
def destroy
@quote = Quote.find(params[:id]).destroy
flash[:notice] = "Quote #{@quote.pkey} deleted!"
redirect_to root_path
end
private
def set_quote
@quote = Quote.find(params[:id])
end
def quote_params
params.require(:quote).permit(:category, :author, :quotetext)
end
end
我的索引视图
<div class='jumbotron'>
<h1>Root Index Page</h1>
</div>
<%= render 'shared/nav' %>
<div class="quotes index">
<%= render 'flash' %>
<table class="listing">
<tr class="header">
<th>Pkey</th>
<th>Category</th>
<th>Author</th>
<th>Quote Text </th>
</tr>
<% @quotes.each do |quote| %>
<tr>
<td><%=quote.pkey %></td>
<td><%=quote.category %></td>
<td><%=quote.author %></td>
<td><%=quote.text %></td>
<td class="actions">
<%= link_to("Show", {:action => 'show', :id => quote.id}) %>
<%= link_to("Edit", {:action => 'edit', :id => quote.id}) %>
<%= link_to quote_path(quote.id), method: :delete, data: {confirm: "Really delete Quote ##{quote.id}?"} do %><span class=edit>Delete</span><% end %>
</td>
</tr>
<% end %>
</table>
</div>
我的编辑视图
<h1>Edit Existing Quote <%=@quote.pkey%> </h1>
<%= form_for(@quote) do |f| %>
<p>
<%= f.label :category %><br>
<%= f.text_field :category %>
</p>
<p>
<%= f.label :author %><br>
<%= f.text_field :author %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :quotetext %>
</p>
<div class="actions">
<%= f.submit "Update quote" %>
</div>
<% end %>
<%= link_to 'Show', @quote %> |
<%= link_to 'Back', quotes_path %>
flash partial
<% if !flash[:notice].blank? %>
<div class="notice">
<%= flash[:notice] %>
</div>
<% end %>
答案 0 :(得分:0)
flash.keep是一个应用于flash的方法,params应该作为参数传递,而不是数组:
flash.keep(:notice)
http://guides.rubyonrails.org/action_controller_overview.html#the-flash
源代码:
http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-keep