2017-08-16 41 views
0

我要種子單頁,有許多文件和訪問路線,如今我不喜歡這樣寫道:導軌 - 從seedfile

PageFile.create!([ 
    { file_body: seedfile('/file1.jpg'), page_id: Page.where(path: 'my_page').take.id } 

但我認爲這是不靈活,我想它取決於通過routes.rb中的數據:

get 'my_page' => 'pages#my_page', as: :my_page 

我想:

page_id: Page.where(path: my_page_path) } 
page_id: Page.where(path: :my_page_path) } 
page_id: Page.where(path: Page.select(:my_page_path)) } 

他們全部返回錯誤或爲零。我究竟做錯了什麼?

回答

2

使用URL傭工:

Rails.application.routes.url_helpers.my_page_path 

對於網址:

Rails.application.routes.url_helpers.my_page_url 

注:URL將添加默認主機,改主機,你將需要添加下面的application.rbRails.application.routes.default_url_options[:host] = "mysite.com"

要通過rake訪問它,您可以在文件include Rails.application.routes.url_helpers中訪問my_page_path n ormally。