1

我想在重定向后将 2 个字符串传递给视图。控制器:

def create
    @rating = Rating.new(params[:rating])

    respond_to do |format|
      if @rating.save
        format.html { redirect_to @rating, :notice => 'Got It!' ,
                      :notice_small => 'Your photo has been uploaded. good luck with it\'s coolness rating!' }
        format.json { render :json => @rating, :status => :created, :location => @rating }
      else
        format.html { render :action => "new" }
        format.json { render :json => @rating.errors, :status => :unprocessable_entity }
      end
    end
  end

风景:

<p id="notice" class="big_notice"><%= notice %></p>

<% if defined? notice_small  %>
    <p id="small_notice" class="small_notice"><%= notice_small %></p>
<% end %>

通知字符串抛出但notice_small 没有,为什么?

4

2 回答 2

2

只允许使用:notice:alert进行设置redirect_to

如果您想要超出此范围的内容,请使用:flash => { :notice_small => '....' }option forredirect_to或 set flash[:notice_small]beforeredirect_to明确。

于 2012-12-01T13:13:03.043 回答
0

据我所知,重定向看起来应该可以工作。但是,要使其在您的视图中可用,您重定向到的操作必须采取params["notice_small"]并将其放入实例变量中。就像是

@notice_small = params["notice_small"]

在行动中,那么你可以做

<% if defined? @notice_small  %>
    <p id="small_notice" class="small_notice"><%= @notice_small %></p>
<% end %>
于 2012-12-01T12:52:46.823 回答