두 개의 다른 레이아웃으로 블로그를 원한다고 가정 해보십시오. 하나의 레이아웃은 헤더, 꼬리말, 메뉴 등이있는 기존 블로그와 비슷해야합니다. 다른 레이아웃에는 블로그 게시물 만 포함하면됩니다. 모델에 대한 연결을 잃지 않고 하나의 액션 만 실행하고 렌더링하고 반복적으로 반복하지 않도록하려면 어떻게해야할까요?레일 3 : 컨트롤러와 액션이 동일한 두 개의 다른 레이아웃이 있습니까?
posts_controller.rb
class PostsController < ApplicationController
layout :choose_layout
# chooses the layout by action name
# problem: it forces us to use more than one action
def choose_layout
if action_name == 'diashow'
return 'diashow'
else
return 'application'
end
end
# the one and only action
def index
@posts = Post.all
@number_posts = Post.count
@timer_sec = 5
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
# the unwanted action
# it should execute and render the index action
def diashow
index # no sense cuz of no index-view rendering
#render :action => "index" # doesn't get the model information
end
[..]
end
은 아마 내가 잘못된 방향으로 가고 싶어하지만 오른쪽 하나를 찾을 수 없습니다.
업데이트 : 내 솔루션은 다음과 같습니다
:
posts_controller.rb
class PostsController < ApplicationController
layout :choose_layout
def choose_layout
current_uri = request.env['PATH_INFO']
if current_uri.include?('diashow')
return 'diashow'
else
return 'application'
end
end
def index
@posts = Post.all
@number_posts = Post.count
@timer_sec = 5
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
[..]
end
설정/routes.rb
Wpr::Application.routes.draw do
root :to => 'posts#index'
match 'diashow' => 'posts#index'
[..]
end
두 개의 서로 다른 경로가 가리키는된다 같은 위치 (컨트롤러/a ction). current_uri = request.env['PATH_INFO']
은 url을 변수에 저장하고 다음 if current_uri.include?('diashow')
이 우리가 설정 한 경로가 routes.rb인지 확인합니다.
감사합니다. 나는이 불쾌한'diashow' 행위가없는 또 다른 조건을 가지고있다. – Marc