简体   繁体   English

将参数传递到自定义的Rails路线中

[英]passing params into a custom rails route

I have struggled with this for a wee while now. 我已经为此苦苦挣扎了一段时间。

there is the model 'custom pages' which belongs to 'leisure centres', a leisure centre can have many custom pages. 有一个属于“休闲中心”的“自定义页面”模型,一个休闲中心可以有许多自定义页面。

There is a defined custom route to show custom pages like so: 有一个定义的自定义路线来显示自定义页面,如下所示:

resources :centres,:path=>'leisure' do
...
  member do
...
     match "/page/:id" => "custom_pages#show", as: 'custom_page_path'

the desired url is something like this 所需的网址是这样的

/leisure/name_of_leisure_centre/page/377

I am trying to reach it by doing something like this... 我正在通过做这样的事情来达到目标​​...

=link_to custom_page.title, custom_page_path_centre_path(custom_page)

but this just gives me a url path 但这只是给我一个URL路径

/leisure/384/page/384

the last '384' is correct as it is the page id and where the first '384' should be the centre name should be there instead(which works fine if i use 'centre_path', everything works correctly there). 最后一个'384'是正确的,因为它是页面ID,而应该在第一个'384'应该是中心名称的地方(如果我使用'centre_path'可以正常工作,那么在那里一切正常)。

Here is the full block: 这是完整的块:

                -borough.leisure_centres.each do |venue|
                  %span=venue.title
                  %li
                    =link_to "Overview", centre_path(venue)
                  %li
                    =link_to 'News', centre_news_index_path(venue)
                  %li
                    =link_to 'Facilities', facilities_centre_path(venue)
                  %li
                    =link_to 'Contact Us', new_centre_contact_form_path(venue)
                  -unless venue.venue_hire_content.nil?
                    %li
                      =link_to 'Hire', venue_hire_centre_path(venue)
                  -unless venue.virtual_tour.nil?
                    %li
                      =link_to 'Virtual Tour', tour_centre_path(venue)
                  -unless venue.custom_pages.nil?
                    - venue.custom_pages.each do |custom_page|
                      %li
                        =link_to custom_page.title, custom_page_path_centre_path(custom_page)

UPDATE: @Iceman's answer has got me closer, however the url is still correct 更新:@Iceman的答案使我更加接近,但是URL仍然正确

eg; 例如;

"/leisure/384/barking-sporthouse-and-gym/page/384"

should be 应该

"barking-sporthouse-and-gym/page/384"

update: added controller 更新:添加了控制器

def get_custompage
    @custom_page = CustomPage.find(params[:id])
    @centre = @custom_page.venue
    @venue=@centre #because calling it @centre is pissing me off

    #Sets standard Meta Content

    unless @custom_page.nil?
      @page_title = @custom_page.meta_page_title unless @custom_page.meta_page_title.nil? or @custom_page.meta_page_title == ""

      @meta_description =  @custom_page.meta_description unless @custom_page.meta_description.nil? or @custom_page.meta_description == ""
    end

    @main_class = "centres"
  end

rake routes 耙路

 memberships_leisure        /memberships_leisure(.:format)                                                                            pages#memberships_leisure
                                    borough_leisure        /leisure/areas/:id(.:format)                                                                              boroughs#show
                              gmaps_borough_leisure        /leisure/areas/:id/gmaps_data(.:format)                                                                   boroughs#gmaps_data
                                            courses        /leisure/lessons-courses(.:format)                                                                        courses#index
                                             course        /leisure/lessons-courses/:id(.:format)                                                                    courses#show
                                    activity_centre        /leisure/:centre_id/activity/:id(.:format)                                                                venues#activity
                                 leisure_gmaps_data        /leisure/gmaps_data(.:format)                                                                             venues#gmaps_data
                                centre_competitions        /leisure/:centre_id/competitions(.:format)                                                                centres#competitions {:param=>:name_of_leisure_centre}
                                 centre_competition        /leisure/:centre_id/competition(.:format)                                                                 centres#competition {:param=>:name_of_leisure_centre}
                                    centre_day_pass        /leisure/:centre_id/day_pass(.:format)                                                                    centres#guest_pass {:param=>:name_of_leisure_centre}
                             centre_day_pass_result        /leisure/:centre_id/day_pass_result(.:format)                                                             centres#day_pass_result {:param=>:name_of_leisure_centre}
                                  centre_guest_pass        /leisure/:centre_id/guest_pass(.:format)                                                                  centres#guest_pass {:param=>:name_of_leisure_centre}
                           centre_guest_pass_result        /leisure/:centre_id/guest_pass_result(.:format)                                                           centres#guest_pass_result {:param=>:name_of_leisure_centre}
                              centre_search_results        /leisure/:centre_id/search_results(.:format)                                                              centres#search_results {:param=>:name_of_leisure_centre}
                            centre_postcode_results        /leisure/:centre_id/postcode_results(.:format)                                                            centres#postcode_results {:param=>:name_of_leisure_centre}
                                             photos GET    /leisure/:id/photos(.:format)                                                                             photos#index {:param=>:name_of_leisure_centre}
                                                    POST   /leisure/:id/photos(.:format)                                                                             photos#create {:param=>:name_of_leisure_centre}
                                          new_photo GET    /leisure/:id/photos/new(.:format)                                                                         photos#new {:param=>:name_of_leisure_centre}
                                         edit_photo GET    /leisure/:id/photos/:id/edit(.:format)                                                                    photos#edit {:param=>:name_of_leisure_centre}
                                              photo GET    /leisure/:id/photos/:id(.:format)                                                                         photos#show {:param=>:name_of_leisure_centre}
                                                    PUT    /leisure/:id/photos/:id(.:format)                                                                         photos#update {:param=>:name_of_leisure_centre}
                                                    DELETE /leisure/:id/photos/:id(.:format)                                                                         photos#destroy {:param=>:name_of_leisure_centre}
                                     academy_centre GET    /leisure/:id/academy(.:format)                                                                            centres#academy {:param=>:name_of_leisure_centre}
                                  activities_centre GET    /leisure/:id/activities(.:format)                                                                         centres#activities {:param=>:name_of_leisure_centre}
                         activitiesprintable_centre GET    /leisure/:id/activitiesprintable(.:format)                                                                centres#activitiesprintable {:param=>:name_of_leisure_centre}
                                      custom_centre GET    /leisure/:id/custom(.:format)                                                                             centres#custom {:param=>:name_of_leisure_centre}
                                  directions_centre        /leisure/:id/directions(.:format)                                                                         centres#directions {:param=>:name_of_leisure_centre}
                                  venue_hire_centre        /leisure/:id/venue_hire(.:format)                                                                         centres#events_venue_hire {:param=>:name_of_leisure_centre}
                                  facilities_centre GET    /leisure/:id/facilities(.:format)                                                                         centres#facilities {:param=>:name_of_leisure_centre}
                       facility_openingtimes_centre POST   /leisure/:id/facility_openingtimes(.:format)                                                              centres#facility_openingtimes {:param=>:name_of_leisure_centre}
                            custom_page_path_centre GET    /leisure/:id/page/:id(.:format)                                                                           custom_pages#show {:param=>:name_of_leisure_centre}
                                  gmaps_data_centre GET    /leisure/:id/gmaps_data(.:format)                                                                         centres#gmaps_data {:param=>:name_of_leisure_centre}
                              health_fitness_centre GET    /leisure/:id/health_fitness(.:format)                                                                     centres#health_fitness {:param=>:name_of_leisure_centre}
                                      offers_centre GET    /leisure/:id/offers(.:format)                                                                             centres#offers {:param=>:name_of_leisure_centre}
                                       offer_centre GET    /leisure/:id/offer(.:format)                                                                              centres#offer {:param=>:name_of_leisure_centre}
                                        page_centre GET    /leisure/:id/page(.:format)                                                                               centres#page {:param=>:name_of_leisure_centre}
                                        team_centre GET    /leisure/:id/team(.:format)                                                                               centres#team {:param=>:name_of_leisure_centre}
                                     coaches_centre GET    /leisure/:id/coaches(.:format)                                                                            centres#team {:param=>:name_of_leisure_centre}
                                        tour_centre GET    /leisure/:id/tour(.:format)                                                                               centres#tour {:param=>:name_of_leisure_centre}
                                       story_centre GET    /leisure/:id/story(.:format)                                                                              centres#story {:param=>:name_of_leisure_centre}
                                 sports_hall_centre GET    /leisure/:id/sports_hall(.:format)                                                                        centres#sports_hall {:param=>:name_of_leisure_centre}
                                   promotion_centre GET    /leisure/:id/promotion(.:format)                                                                          centres#promotion {:param=>:name_of_leisure_centre}
                                 disciplines_centre GET    /leisure/:id/disciplines(.:format)                                                                        centres#disciplines {:param=>:name_of_leisure_centre}
                            weekly_programme_centre GET    /leisure/:id/weekly_programme(.:format)                                                                   centres#weekly_programme {:param=>:name_of_leisure_centre}
                                                    POST   /leisure/:id/weekly_programme(.:format)                                                                   centres#weekly_programme {:param=>:name_of_leisure_centre}
                                      centre_alerts GET    /leisure/:centre_id/alerts(.:format)                                                                      alerts#index {:param=>:name_of_leisure_centre}
                                                    POST   /leisure/:centre_id/alerts(.:format)                                                                      alerts#create {:param=>:name_of_leisure_centre}
                                   new_centre_alert GET    /leisure/:centre_id/alerts/new(.:format)                                                                  alerts#new {:param=>:name_of_leisure_centre}
                                  edit_centre_alert GET    /leisure/:centre_id/alerts/:id/edit(.:format)                                                             alerts#edit {:param=>:name_of_leisure_centre}
                                       centre_alert GET    /leisure/:centre_id/alerts/:id(.:format)                                                                  alerts#show {:param=>:name_of_leisure_centre}
                                                    PUT    /leisure/:centre_id/alerts/:id(.:format)                                                                  alerts#update {:param=>:name_of_leisure_centre}
                                                    DELETE /leisure/:centre_id/alerts/:id(.:format)                                                                  alerts#destroy {:param=>:name_of_leisure_centre}
                            centre_competitionitems GET    /leisure/:centre_id/competitionitems(.:format)                                                            centre_newsitems#index {:param=>:name_of_leisure_centre}
                                                    POST   /leisure/:centre_id/competitionitems(.:format)                                                            centre_newsitems#create {:param=>:name_of_leisure_centre}
                         new_centre_competitionitem GET    /leisure/:centre_id/competitionitems/new(.:format)                                                        centre_newsitems#new {:param=>:name_of_leisure_centre}
                        edit_centre_competitionitem GET    /leisure/:centre_id/competitionitems/:id/edit(.:format)                                                   centre_newsitems#edit {:param=>:name_of_leisure_centre}
                             centre_competitionitem GET    /leisure/:centre_id/competitionitems/:id(.:format)                                                        centre_newsitems#show {:param=>:name_of_leisure_centre}
                                                    PUT    /leisure/:centre_id/competitionitems/:id(.:format)                                                        centre_newsitems#update {:param=>:name_of_leisure_centre}
                                                    DELETE /leisure/:centre_id/competitionitems/:id(.:format)                                                        centre_newsitems#destroy {:param=>:name_of_leisure_centre}
                               centre_contact_forms POST   /leisure/:centre_id/contactus(.:format)                                                                   contact_forms#create {:param=>:name_of_leisure_centre, :trailing_slash=>false}
                            new_centre_contact_form GET    /leisure/:centre_id/contactus(.:format)                                                                   contact_forms#new {:param=>:name_of_leisure_centre, :trailing_slash=>false}
                               search_centre_events POST   /leisure/:centre_id/events/search(.:format)                                                               events#search {:param=>:name_of_leisure_centre}
                               sports_centre_events GET    /leisure/:centre_id/events/sports(.:format)                                                               events#sports {:param=>:name_of_leisure_centre}
                                      centre_events GET    /leisure/:centre_id/events(.:format)                                                                      events#index {:param=>:name_of_leisure_centre}
                                                    POST   /leisure/:centre_id/events(.:format)                                                                      events#create {:param=>:name_of_leisure_centre}
                                   new_centre_event GET    /leisure/:centre_id/events/new(.:format)                                                                  events#new {:param=>:name_of_leisure_centre}
                                  edit_centre_event GET    /leisure/:centre_id/events/:id/edit(.:format)                                                             events#edit {:param=>:name_of_leisure_centre}
                                       centre_event GET    /leisure/:centre_id/events/:id(.:format)                                                                  events#show {:param=>:name_of_leisure_centre}
                                                    PUT    /leisure/:centre_id/events/:id(.:format)                                                                  events#update {:param=>:name_of_leisure_centre}
                                                    DELETE /leisure/:centre_id/events/:id(.:format)                                                                  events#destroy {:param=>:name_of_leisure_centre}
                              centre_event_searches GET    /leisure/:centre_id/event_searches(.:format)                                                              event_searches#index {:param=>:name_of_leisure_centre}
                                                    POST   /leisure/:centre_id/event_searches(.:format)                                                              event_searches#create {:param=>:name_of_leisure_centre}
                            new_centre_event_search GET    /leisure/:centre_id/event_searches/new(.:format)                                                          event_searches#new {:param=>:name_of_leisure_centre}
                           edit_centre_event_search GET    /leisure/:centre_id/event_searches/:id/edit(.:format)                                                     event_searches#edit {:param=>:name_of_leisure_centre}
                                centre_event_search GET    /leisure/:centre_id/event_searches/:id(.:format)                                                          event_searches#show {:param=>:name_of_leisure_centre}
                                                    PUT    /leisure/:centre_id/event_searches/:id(.:format)                                                          event_searches#update {:param=>:name_of_leisure_centre}
                                                    DELETE /leisure/:centre_id/event_searches/:id(.:format)                                                          event_searches#destroy {:param=>:name_of_leisure_centre}
                     competitions_centre_news_index GET    /leisure/:centre_id/news/competitions(.:format)                                                           centre_newsitems#competitions {:param=>:name_of_leisure_centre}
                 show_competition_centre_news_index        /leisure/:centre_id/news/competitions/:id(.:format)                                                       centre_newsitems#show {:param=>:name_of_leisure_centre}
                                  centre_news_index GET    /leisure/:centre_id/news(.:format)                                                                        centre_newsitems#index {:param=>:name_of_leisure_centre}
                                                    POST   /leisure/:centre_id/news(.:format)                                                                        centre_newsitems#create {:param=>:name_of_leisure_centre}
                                    new_centre_news GET    /leisure/:centre_id/news/new(.:format)                                                                    centre_newsitems#new {:param=>:name_of_leisure_centre}
                                   edit_centre_news GET    /leisure/:centre_id/news/:id/edit(.:format)                                                               centre_newsitems#edit {:param=>:name_of_leisure_centre}
                                        centre_news GET    /leisure/:centre_id/news/:id(.:format)                                                                    centre_newsitems#show {:param=>:name_of_leisure_centre}
                                                    PUT    /leisure/:centre_id/news/:id(.:format)                                                                    centre_newsitems#update {:param=>:name_of_leisure_centre}
                                                    DELETE /leisure/:centre_id/news/:id(.:format)                                                                    centre_newsitems#destroy {:param=>:name_of_leisure_centre}
                                            centres GET    /leisure(.:format)                                                                                        centres#index {:param=>:name_of_leisure_centre}
                                                    POST   /leisure(.:format)                                                                                        centres#create {:param=>:name_of_leisure_centre}
                                         new_centre GET    /leisure/new(.:format)                                                                                    centres#new {:param=>:name_of_leisure_centre}
                                        edit_centre GET    /leisure/:id/edit(.:format)                                                                               centres#edit {:param=>:name_of_leisure_centre}
                                             centre GET    /leisure/:id(.:format)                                                                                    centres#show {:param=>:name_of_leisure_centre}
                                                    PUT    /leisure/:id(.:format)                                                                                    centres#update {:param=>:name_of_leisure_centre}
                                                    DELETE /leisure/:id(.:format)                                                                                    centres#destroy {:param=>:name_of_leisure_centre}
                                    course_contacts GET    /leisure/lessons-courses/contact/form(.:format)                                                           course_contacts#index
                                                    POST   /leisure/lessons-courses/contact/form(.:format)                                                           course_contacts#create
                                 new_course_contact GET    /leisure/lessons-courses/contact/form/new(.:format)                                                       course_contacts#new
                                edit_course_contact GET    /leisure/lessons-courses/contact/form/:id/edit(.:format)                                                  course_contacts#edit
                                     course_contact GET    /leisure/lessons-courses/contact/form/:id(.:format)                                                       course_contacts#show
                                                    PUT    /leisure/lessons-courses/contact/form/:id(.:format)                                                       course_contacts#update
                                                    DELETE /leisure/lessons-courses/contact/form/:id(.:format)                                                       course_contacts#destroy
                              admin_leisure_centres GET    /admin/leisure_centres(.:format)                                                                          admin/leisure_centres#index
                                                    POST   /admin/leisure_centres(.:format)                                                                          admin/leisure_centres#create
                           new_admin_leisure_centre GET    /admin/leisure_centres/new(.:format)                                                                      admin/leisure_centres#new
                          edit_admin_leisure_centre GET    /admin/leisure_centres/:id/edit(.:format)                                                                 admin/leisure_centres#edit
                               admin_leisure_centre GET    /admin/leisure_centres/:id(.:format)                                                                      admin/leisure_centres#show
                                                    PUT    /admin/leisure_centres/:id(.:format)                                                                      admin/leisure_centres#update
                                                    DELETE /admin/leisure_centres/:id(.:format)                                                                      admin/leisure_centres#destroy

Please help me! 请帮我! thanks! 谢谢!

From your line in routes: 从路线中的路线:

resources :centres,:path=>'leisure' do
  ...
  member do
    ...
    match "/page/:id" => "custom_pages#show", as: 'custom_page_path'

It generates this path: 它生成此路径:

custom_page_path_centre GET /leisure/:id/page/:id(.:format)

Here if you see, it takes :id/page/:id which is causing Rails to put id at two places. 在这里,如果看到的话,它使用:id/page/:id ,这导致Rails将id放在两个位置。

You need to change your route to: 您需要将路线更改为:

resources :centres,:path=>'leisure' do
  ...
  member do
    ...
    ...
  end
  resources 'custom_pages', path: 'page'
  ...
end

This should generate one of the routes like this: centere_custom_page_path pointing to: CustomPages#show . 这将生成以下路由之一: centere_custom_page_path指向: CustomPages#show

Then you can change view to this: 然后,您可以将视图更改为此:

-borough.leisure_centres.each do |venue|
  %span=venue.title
  %li
    =link_to "Overview", centre_path(venue)
  %li
    =link_to 'News', centre_news_index_path(venue)
  %li
    =link_to 'Facilities', facilities_centre_path(venue)
  %li
    =link_to 'Contact Us', new_centre_contact_form_path(venue)
  - unless venue.venue_hire_content.nil?
    %li
      =link_to 'Hire', venue_hire_centre_path(venue)
  - unless venue.virtual_tour.nil?
    %li
      =link_to 'Virtual Tour', tour_centre_path(venue)
  - unless venue.custom_pages.nil?
    - venue.custom_pages.each do |custom_page|
      %li
        =link_to custom_page.title, centre_custom_page_path(centre_id: venue, id: custom_page.id)

You can read more on nested routing here: http://guides.rubyonrails.org/routing.html#nested-resources 您可以在此处阅读有关嵌套路由的更多信息: http : //guides.rubyonrails.org/routing.html#nested-resources

This would work for you. 这将为您工作。

# routes.rb
match 'leisure/:centre_name/page/:id' => "custom_pages#show", as: 'custom_page'

# view file
= link_to custom_page.title, custom_page_path(centre_name: 'name_of_leisure_centre', id: 1)

You can use :param key if you don't want to change your routes structure. 如果不想更改路由结构,可以使用:param键。 In your routes.rb file: 在您的routes.rb文件中:

resources :centres, :path=>'leisure', :param=> :name_of_leisure_centre do
...
  member do
...
     get "/page/:id", to: "custom_pages#show", as: 'custom_page_path'

And then in your view file use: 然后在您的视图文件中使用:

= link_to custom_page.title, custom_page_path_centre_path(name_of_leisure_centre: centre.name, id: custom_page)

Produced 'href' would be 产生的“ href”将是

leisure/leisure_name/page/999

Works for me with Rails 4.1.1 适用于Rails 4.1.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM