首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >rails在重定向后将参数传递给视图

rails在重定向后将参数传递给视图
EN

Stack Overflow用户
提问于 2012-12-01 20:40:07
回答 2查看 1.2K关注 0票数 1

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

代码语言:javascript
运行
复制
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

视图:

代码语言:javascript
运行
复制
<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不抛出,为什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-01 21:13:03

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

如果您想要更多,可以对redirect_to使用:flash => { :notice_small => '....' }选项,或者在redirect_to之前显式地设置flash[:notice_small]

票数 2
EN

Stack Overflow用户

发布于 2012-12-01 20:52:47

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

代码语言:javascript
运行
复制
@notice_small = params["notice_small"]

在行动中,那么你可以这样做

代码语言:javascript
运行
复制
<% if defined? @notice_small  %>
    <p id="small_notice" class="small_notice"><%= @notice_small %></p>
<% end %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13659541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档