crcerror crcerror -3 years ago 77
Ruby Question

Rails - Access to routes from seedfile

I want to seed a single Page, that has many files and now I do it like this:

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


But I think it's not flexible and I want it to depend on by routes.rb data:

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


I tried:

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)) }


All of them return errors or nil. What am I doing wrong?

Answer Source

use url helpers with :

Rails.application.routes.url_helpers.my_page_path

For url:

Rails.application.routes.url_helpers.my_page_url

Note: url will add default host, to change host, you will need to add following in application.rb: Rails.application.routes.default_url_options[:host] = "mysite.com"

To access it across rake, you can include Rails.application.routes.url_helpers in file and access my_page_path normally.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download