rails重定向后传递params来查看

时间:2012-12-01 12:40:07

标签: ruby-on-rails controller

我希望在重定向后将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没有,为什么?

2 个答案:

答案 0 :(得分:2)

仅允许使用:notice设置:alertredirect_to

如果您需要此类内容,请使用:flash => { :notice_small => '....' }的{​​{1}}选项或明确redirect_to之前设置flash[:notice_small]

答案 1 :(得分:0)

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

这样的东西
@notice_small = params["notice_small"]

在行动中,你可以做

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