我們正試圖讓我們的網站具有較少刮擦和更易讀的URL 例如, www.loomio.org/discussions/3122 變成 www.loomio.org/d/3saA4ds9/lets-go-to-the-moon 我們只是真的想要show-links上的人類可讀的slu so,所以www。 loomio.org/d/3saA4ds9/edit應該是URL用於編輯討論創建自定義路徑助手,同時使用FreindlyID
的解決方案至今如下頂部答案在這裏: Ruby on Rails: How to override the 'show' route of a resource?
修改routes.rb
:
get '/d/:id/:slug', to: 'discussions#show', as: :discussion
resources :discussions, path: 'd', except: [:edit, :show] do
get :activity_counts, on: :collection
member do
post :update_description
post :add_comment
post :show_description_history
get :new_proposal
post :edit_title
put :move
end
end
安裝寶石FriendlyID ;在討論表上填寫:key
列;將以下內容添加到discussion.rb
(型號):
KEY_LENGTH = 10
extend FriendlyId
friendly_id :key
爲group_path編寫自定義路徑助手。在groups_helper.rb
:
def group_url(group, options={})
url_for(options.merge(:controller => 'groups', :action => 'show',
:id => group.key, :slug => group.full_name.parameterize))
end
def group_path(group, options={})
group_url(group, options.merge(:only_path => true))
end
耙途徑產生: 組GET /g/:id/:slug(.:format)羣體#顯示
,並同時呼籲group_path(group)
似乎在某些情況下工作,我M還看到獲取生成奇怪無關的網址,如: http://loomio.org/group_requests/TegFOIx4DB/start_new_group?action=show&controller=groups%2Fgroups&slug=19-tory
,我也越來越錯誤,如:
[5] pry(main)> g = Group.last
[6] pry(main)> app.group_path(g)
ActionController::RoutingError: No route matches {:controller=>"groups", :action=>"show", :id=>#<Group id: 2811, name: "Sylvester Buckridge", created_at: "2013-12-10 06:25:42", updated_at: "2013-12-10 06:25:42", privacy: "public", members_invitable_by: "members", parent_id: nil, email_new_motion: true, hide_members: false, beta_features: false, description: "A description for this group", memberships_count: 1, archived_at: nil, max_size: 300, cannot_contribute: false, distribution_metric: nil, sectors: nil, other_sector: nil, discussions_count: 0, motions_count: 0, country_name: nil, setup_completed_at: "2013-12-10 05:25:01", next_steps_completed: false, full_name: "Sylvester Buckridge", payment_plan: "undetermined", viewable_by_parent_members: false, key: "rkdlTytOin">}
from /home/s01us/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.16/lib/action_dispatch/routing/route_set.rb:540:in `raise_routing_error'
我試過把group_path和grop_url方法放在ApplicationController
和ApplicationHelper
無效。
調用
group_path(group.key, group.fullname.parameterize)
的作品,但在理想情況下希望能夠只需要調用如
group_path(@group)
[解決],改變路線:得到「/ d/:id(/:slug)',即:'discussion#show',如::討論 – mix
如果您有答案,請提供答案作爲您的問題答案,然後接受答案。它可能看起來是自私的,但這是正確的答案,可以幫助其他人找到這個問題和建議 –