2012-12-01 84 views
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没有,为什么?

回答

2

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

如果您想要超出此范围,请使用:flash => { :notice_small => '....' }选项redirect_to或在redirect_to之前明确设置flash[:notice_small]

+0

我如何在视图中访问它? – WebQube

+0

'flash [:notice_small]'。 – jdoe

+0

您的'notice'只是'flash.notice'的快捷方式,它依次调用'flash [:notice]'。所以你可以通过'flash [:...]''同时访问':notice'和':notice_small'。 – jdoe

0

重定向看起来像它应该尽我所能地工作。但是,要使其在您的视图中可用,要重定向到的操作必须采用params["notice_small"]并将其放入实例变量中。喜欢的东西

@notice_small = params["notice_small"] 

的行动,那么你可以做

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

您好斯科特,感谢您的快速回复。但它仍然无法使用'@notice_small ='您的照片已上传。祝你好运!“'并从(notice_small)更改(在视图中)到'@ notice_small' – WebQube

+0

你可以添加你的ruby/rails版本和你重定向到的动作吗? –

+0

rails版本是'3.2.9',视图是'show.html.erb' – WebQube