2010-06-15 6 views
0

레일에 관해서는 작은 질문이 있습니다. 데이터베이스에 이름을 검색하는 검색 컨트롤러가 있습니다. 발견 된 경우 세부 정보가 표시되거나 그렇지 않으면 새 이름 페이지로 리디렉션됩니다. 이름이 새 양식 페이지에 자동으로 표시되도록 검색 한 리디렉션이 있습니까?레일 리디렉션

미리 감사드립니다.

답변

2

ActionController::Flash을 사용하여 작업간에 데이터를 전달할 수 있습니다.

def search(searchedName) 
    # perform search on DB 

    flash[:searchedName] = searchedName 

    redirect_to new_name 
end 

def new_name 
end 

new_name.html.erb :

<% if flash[:searchedName] %> 
    <%= text_field_tag flash[:searchedName] %> 
<% end %> 
+0

좋은. 하지만 다음 페이지의 해당 텍스트 필드에서 어떻게 설정합니까? –

+0

@Cyborgo : 전달 된 데이터로 텍스트 상자 내용을 설정하기 위해보기에 스 니펫 추가 – Anero

+0

도움에 감사드립니다! :) –

2

음, 당신이라는 변수 @name에 사용자가 입력이 '이름'을 저장하는 가정 해 봅시다.
그래서, 검색 작업을 수행 컨트롤러에, 당신은 같은 한 : 호출 된 메소드의 선언에

if @name 
    ...#action if the name was found 
else 
    session[:name] = @name 
    ...#here is the action you did to redirect 

을 (당신은 '새로운'했다) :

def new 
    @name = session[:name] if session[:name] #I don't know exactly if the condition is this one, 
              #but you have to confirm that the session was instatiated 
    session.delete(:name) #to don't let garbage 
    ...#continuous the action of show the new page 
    #and on the page you have access to a variable @name, that have the name searched before. 
+0

나는 그것이 단지'if session [: name]' –

관련 문제