레일에서 정적 페이지 전략에 문제가 있습니다.UrlGenerationError : 경로 일치, 필수 키 누락 및 link_to
정적 페이지를 처리하는 컨트롤러가 있습니다. 기본적으로 app/views/static
디렉토리의 템플릿을 읽고 질문대로 렌더링합니다. 그래서 같이 : 루트 경로가 잘 작동
Rails.application.routes.draw do
root 'static#show', page: 'home'
get ':page', to: 'static#show', as: :static, constraints: { page: /a-zA-Z\-_\/+/ }
end
, 내가 잘보기에 액세스 할 수 있어요 :
class StaticController < ApplicationController
def show
templ = File.join(params[:controller], params[:page])
puts params, templ
render templ
rescue ActionView::MissingTemplate => e
if e.message =~ %r{Missing template #{templ}}
raise ActionController::RoutingError, 'Not Found'
else
raise e
end
end
end
이 내 루트입니다. 나는 오류가 없다. 지금, 내 헤더 부분에, 나는 단순/관련성에 대한 수정이를 가지고 :
<%= link_to('Home', static_path(:home)) %>
부분 또는 기본 템플릿에 다른 루비 코드가 없습니다. 내가 뭘 잘못하고 있는지 모르겠다. 오류는 단지 의미가 없습니다.
ActionController::UrlGenerationError - No route matches {:action=>"show", :controller=>"static", :format=>nil, :page=>:home} missing required keys: [:page]
경우 정확히 필요한 키에서 누락? 다른 물건이나 모델이 없습니다.
지금이 잘 작동합니다 :
<%= link_to('Home', controller: 'static', action: 'show', page: 'home') %>
그래서 내가 어떻게 그런 static_path 작업을해야합니까?
감사합니다.
정규식을 삭제하면 문제가 해결되었습니다. – fspy