我现在一直在努力解决这个问题。
模型'自定义页面'属于'休闲中心',休闲中心可以有许多自定义页面。
有一个定义的自定义路由来显示自定义页面,如下所示:
resources :centres,:path=>'leisure' do
...
member do
...
match "/page/:id" => "custom_pages#show", as: 'custom_page_path'
所需的网址是这样的
/leisure/name_of_leisure_centre/page/377
我试图通过这样做来达到它......
=link_to custom_page.title, custom_page_path_centre_path(custom_page)
但这只是给我一个网址路径
/leisure/384/page/384
最后的'384'是正确的,因为它是页面ID,而第一个'384'应该是中心名称应该在那里(如果我使用'centre_path',那么一切正常就可以正常工作)。< / p>
这是完整的块:
-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)
更新:@ Iceman的回答让我更接近,但网址仍然正确
例如;
"/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
@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
请帮帮我!谢谢!
答案 0 :(得分:1)
从你的路线:
resources :centres,:path=>'leisure' do
...
member do
...
match "/page/:id" => "custom_pages#show", as: 'custom_page_path'
它生成此路径:
custom_page_path_centre GET /leisure/:id/page/:id(.:format)
如果您看到,则:id/page/:id
会导致Rails将id
放在两个位置。
您需要将路线更改为:
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
答案 1 :(得分: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)
答案 2 :(得分: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
为我工作