2014-07-09 40 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

仍然沒有工作,請參閱我的更新回答 –