2011-01-25 3 views
9

나는 내 tv_shows/index.html.erb 는 왜 : 레일에 3

<div id="notice"><%= notice %></div> 

만에 다음 내 컨트롤러

def create 
    @tv_show = TvShow.new(params[:tv_show]) 

    respond_to do |format| 
     if @tv_show.save 
     format.html { redirect_to(tv_shows_path, :notice => 'Tv show was successfully created.') } 
     format.xml { render :xml => @tv_show, :status => :created, :location => @tv_show } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @tv_show.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

의 코드를 다음 한 리디렉션 후 표시되지주의 사항 tv_shows_path로 리디렉션 한 후 알림 메시지가 나타나지 않는 새 항목을 만듭니다. 누구에게 왜 아이디어가 있습니까?

답변

0

코드가없는 일이 내 인증 코드에 문제가 있었다했던 이유는 ... 후 :보기

respond_to do |format| 
    if @tv_show.save 
    format.html { 
     flash[:notice] = 'Tv show was successfully created.' 
     redirect_to tv_shows_path 
    } 
    format.xml { render :xml => @tv_show, :status => :created, :location => @tv_show } 
    else 
    format.html { render :action => "new" } 
    format.xml { render :xml => @tv_show.errors, :status => :unprocessable_entity } 
    end 
end 

위의 코드를 긁어 내면 작동합니다.

+3

당신이 구체적 일지 궁금합니다. 나는 똑같은 문제를 겪고 있는데,이 대답은 도움이되지 않습니다. 내 컨트롤러에는 before_filter : authenticate_user!가 있습니다. 틀렸어? –

+0

미안하지만 나는 1 년 전에 그 문제가 무엇인지 모릅니다. 지금은 레일즈 3.1부터 has_secure_password를 사용하고 있습니다. http://railscasts.com/episodes/270-authentication-in-rails-3-1 – maveonair

17

:notice이 아닌 flash[:notice]을 사용해야하는 이유가 있습니까?

컨트롤러 : 나는에서 인증 나의 새로운 방식을 구현

<% if flash[:notice] %> 
    <div id="notice"><%= flash[:notice] %></div> 
<% end %> 
+0

나는 숀이보기에 대한 올바른 생각을, 그것은 "[: 예고] 플래시"이어야합니다. –

+0

이것이 레일 3 버전의 새로운 방법이 될 것이라고 읽었습니다 ... 그러나 나는 당신의 제안을 시도 할 것입니다, 고마워요! – maveonair

+0

@Fabian :이 기능이 도움이 되었습니까? – Shaun

4

비슷한 '문제'가 발생했으며 그 원인은 다른 리디렉션이있는 작업으로 리디렉션하고 있다는 것입니다. 위의 경우 가장 가능성있는 원인은 tv_shows_path 내에 또 다른 리다이렉션이 존재한다는 것입니다.

redirect_to root_url, notice: 'Unauthorized access!' 

그리고 root_url

home#index를 가리 키도록 설정 :이 두 번째 redirect_to 내 'unauthorized_access'통지를하지 일으키는

# Home controller 
def index 
    if user_signed_in? && current_user.admin? 
    redirect_to users_path 
    else 
    redirect_to customers_path 
    end 
end 

내 경우

내가 필터에서 이런 일이 있었다 나타나기.

해결 방법은 바로 customers_path으로 리디렉션하고 root_url으로 리디렉션하지 않는 것입니다. 희망이 사람을 도움이됩니다.

+1

또는 두 번째 리디렉션이 발생하는 곳에서 ('redirect_to' 호출 전)'flash.keep '을 수행 할 수 있습니다. 이것은 내가 한 일이며 예상대로 작동하는 것 같습니다. – Koby

1

방금 ​​같은 문제가 있었으며 오류는 너무 어리 석었지만 때로는 알아볼 수 없었습니다. 나는 내 응용 프로그램 레이아웃에 다음 코드가

: 내 플래시 메시지를 볼 수없는 이유를 이제

<div id="content" > 
    <div class="wrapper"> 
     <% flash.each do |name, msg| %> 
      <% content_tag :div, msg, class: "flash #{name}"%> 
     <% end %> 
     <%= yield %> 
    </div> 
</div> 

, 당신은 볼 수 있습니까?

Yup! 당신이 여기에 = 기호 넣어 경우가 확인해야합니다 :

<%= content_tag :div, msg, class: "flash #{name}"%> 
+1

'content_tag'에서, 그것은': msg' 대신'msg'이어야합니다 –