2017-10-30 1 views
0

목표 : ruby ​​on rails 앱은 테이블의 정보를 동적으로 빌드하므로 누군가가 테이블의 정보를 편집 할 때 두 서버에서 자동으로 경로를 새로 고치는 방법이 필요합니다. 템플리트를 포함하는 뷰에 폴더가 있습니다. 해당 뷰에 대한 경로가 자동으로 테이블의 데이터를 기반으로 생성됩니다2 대의 서버가있는 레일즈 애플리케이션의 루비 경로를 새로 고치는 방법은 무엇입니까?

routes.rb

namespace :templates do 
    templates = PdfTemplate.all.pluck(:shortcode) 
    resources :pdfs, only: templates do 
    templates.each do |template| 
     get template.to_s, on: :collection 
    end 
    end 
end 

우리는 경로가 이벤트 누군가에 자동으로 업데이트 할 템플릿의 단축 코드의 변경 데이터베이스

내 시도 : 이전에, 나는 우리가 필요로하는 것처럼 경로를 갱신하는 Rails.application.reload_routes!을 수행하는 after_saveattribute_changed? 경우 다시 모델에 전화를 추가했습니다. 그러나 우리의 애플 리케이션은 2 개의 서버를 가지고 있기 때문에 하나의 서버에있는 라우트 만 업데이트하므로 둘 다 업데이트되지 않아 그 불일치로 인해 문제가 발생합니다.

진짜 질문 (마침내) : 우리 앱의 두 서버에서 경로를 새로 고치는 가장 좋은 방법은 무엇입니까? 우리는 Ruby v2.1.2, Rails v4.1.6 및 mysql을 사용하고 있습니다.

+0

:

namespace :templates do [:pdfs, :html].each do |template_type| scope template_type do get '*shortcode', to: "#{template_type}#show", as: template_type end end end 

당신 준다? –

+0

@CamdenNarzt mysql – Alex

+1

데이터베이스의 레코드가 변경된 경우 라우트를 다시로드하는 이유를 생각할 수 없습니다. 그 점에 대해 자세히 설명하고 예제를 추가 할 수 있습니까? 나에게 이것은 [xy 문제] (https://meta.stackexchange.com/a/66378/284887)처럼 들린다. – spickermann

답변

2

스피커 맨은 바로 그것을 가지고 (특히 XY 문제 설명을).

namespace :templates do 
    scope :pdfs do 
    get '*shortcode', to: 'pdfs#show', as: :pdf 
    end 
end 

당신에게주는 :

templates_pdf GET /templates/pdfs/*shortcode(.:format) templates/pdfs#show 

'*shortcode' 와일드 카드의 정규이다 난 그냥 당신이 또한 할 수있는 언급하고 싶었다. 그래서, 당신은 같은 URL이있는 경우 : 그것은 params[:shortcode] == 'a-cool-template'templates/pdfs#show 노선 것이다

localhost:3000/templates/pdfs/a-cool-template 

합니다. app/views/templates/pdfs/a-cool-template.html.erb이있는 경우

class Templates::PdfsController < ApplicationController 

    def show 
    begin 
     render params[:shortcode] 
    rescue MissingTemplateError 
     # do something here 
    end 
    end 

end 

이,이 렌더링됩니다

그런 다음 Templates::PdfsController은 같을 수 있습니다. 그렇지 않으면 MissingTemplate 오류를 catch하고 그에 따라 응답합니다. (BTW, 나는 MissingTemplate 오류의 실제 이름이 무엇인지 잊어 버린다. 그래서 내가 가지고있는 것은 아마도 정확하지 않을 것이다.)

여러 템플릿 유형이있는 경우, 당신은 할 수 : DB 당신이 어떤을 사용하는

templates_pdfs GET /templates/pdfs/*shortcode(.:format) templates/pdfs#show 
templates_html GET /templates/html/*shortcode(.:format) templates/html#show  
+0

그래서이 예제에서'show' 액션을 넣어서 app/views/templates/pdfs/a-cool-template.html.erb' 뷰를 렌더링 할 것을 추천합니다. 구체적으로? – Alex

+0

답변에'Template :: PdfsController # show'를 추가했습니다. – jvillian

0

이것을 달성하는 방법은 여러 가지가 있지만 간단히 말해서 mysql 데이터베이스에 트리거를 추가하여 경로를 업데이트하는 API 끝점을 호출하는 것입니다. can be found here

1

왜 그냥 모든 경로를 허용하고, 컨트롤러 (404)를 처리하지 않습니다

MySQL을 사용의 예는 HTTP 호출을 트리거?

get 'templates/pdfs/:shortcode', to: 'pfds#routing', via: :get 

을 그리고 PdfsController이 같은 방법을 추가 :

나는 이런 식으로 config/routes.rb을 바꿀 것

def routing 
    pfd_template = PdfTemplate.find_by!(shortcode: params[:shortcode]) 
    # code needed to handle the shortcode 
end 
관련 문제