2014-07-09 36 views
0

我一直在努力争取一小时,而现在。将PARAMS传递到自定义导轨路线

还有属于'休闲中心'的模特'自定义页面',休闲中心可以有很多自定义页面。

有显示自定义的页面,就像这样定义的自定义路线:

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

所需的URL是这样的

/leisure/name_of_leisure_centre/page/377 

我试图做这样的事情去实现它。 ..

=link_to custom_page.title, custom_page_path_centre_path(custom_page) 

但这只是给我一个url路径

/leisure/384/page/384 

最后的“384”是正确的,因为它是页面ID和最早的“384”应该是中心名称应该是有代替(如果我使用“centre_path”工作正常,一切正常有)。

以下是完整的块:

   -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:@冰人的答案已经让我更近,但是网址仍然是正确的

如;

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

应该

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

更新:加入控制器

def get_custompage 
    @custom_page = CustomPage.find(params[:id]) 
    @centre = @custom_page.venue 
    @[email protected] #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 

耙路线

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 

请帮帮我!谢谢!

+0

尝试:'=的link_to custom_page.title,custom_page_path_centre_path(场地,custom_page)'? – Surya

+0

@Surya不,这不起作用。 –

+0

你能从终端发布'rake routes'命令的输出吗?或去'localhost:3000/routes',因为它的轨道4. – Surya

回答

1

从你的路线是:如果你看到

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

在这里,它需要:id/page/:id这是造成的Rails把id在两个地方:

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

它生成此路径。

您需要将您的路线更改为:

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

这应该产生这样的途径之一:centere_custom_page_path指向:CustomPages#show

然后你可以改变视图这样的:

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

你可以阅读更多的嵌套路由位置:http://guides.rubyonrails.org/routing.html#nested-resources

+0

感谢您的帮助。这是迄今为止最接近的答案。它产生的URL路径“/leisure/copper-box-arena/page.226”,为什么这个点不断出现。再次感谢您的帮助。 –

+0

尝试更新的代码,我添加在视图中,应该工作。 – Surya

+0

苏里亚,多谢你的帮助。我在这里学到了很多东西。 –

0

这将适用于你。

# 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) 
+0

更接近,但这是输出'“/休闲/ 384 /吠叫 - 运动场和健身房/页面/ 384”一个额外的384,仍然! –

+0

感谢您的帮助,我怎样才能省略前面的网址 –

+0

请发布您的'耙路线'请 – Iceman

0

如果您不想更改路线结构,可以使用:param键。 在你的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' 

,然后在视图文件使用:

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

生产的 'HREF' 将

leisure/leisure_name/page/999 

对我的作品使用Rails 4.1。1

+0

仍然没有工作,请参阅我的更新回答 –