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]before をredirect_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 に答える