Rails路由:更改' show'资源的路径

时间:2016-08-01 14:07:34

标签: ruby-on-rails

我想更改show路径以使其更适合SEO。尝试转到edit

时,下面的代码会引发错误

的routes.rb

resources :posts, :only => [:index, :new, :create, :update, :edit]

match "posts/:id/:league_name/:post_description", to: 'posts#show', :as => :post, via: :get

错误:

No route matches {:controller=>"posts", :action=>"show", :format=>nil, :id=>#<Post id: 1, title: "2 Pick Parlay", content: "<h1>Here you go</h1>\r\nStarting off the season right...", link: nil, created_at: "2016-07-31 21:45:40", updated_at: "2016-07-31 22:01:58", user_id: 2, league_id: 1, initial_status: nil, home_team: "", favorite: "", points: nil, visiting_team: "", event_datetime: "2016-09-09 00:30:00", spread_home: nil, spread_away: nil, total_points: nil, user_spread: nil, user_team_pick: nil, user_total_pick: nil, user_total_points: nil, user_line_source: nil, post_type: nil, event_id: 137, subscriber_only: false, release_at_gametime: false, is_parlay: true, flagged: false, weight: 1, post_description: "pick">}

当我取出form_for页面上的edit时,页面呈现。所以问题必须出在edit页面

上的代码中

edit.haml.html中的违规代码:

    = form_for(@post) do |f| 
    %h3
      Title for write up
    = f.text_field(:title, :class => "field span8")
    %br
    %br
    %h3
      Your analysis
    = f.text_area(:content, :class => "field span8", :rows => "5")
    <br/>
    = f.hidden_field(:league_id)  
    = f.hidden_field(:event_id) 
    = f.hidden_field(:home_team) 
    = f.hidden_field(:visiting_team)
    = f.hidden_field(:favorite) 
    = f.hidden_field(:points) 
    = f.hidden_field(:event_datetime)  
    %br
    %br  
    %h4
      - if @picks_tweet_string && @just_this_post_tweet_string
        - if @just_this_post_tweet_string.size > 130
          = link_to "Tweet these picks out now", "https://twitter.com/intent/tweet?text=#{@just_this_post_tweet_string}", confirm: "Click OK below. Then you may need to shorten the tweet to 140 characters. Most people remove the city name from the picks."
        - else  
          = link_to "Tweet these picks out now", "https://twitter.com/intent/tweet?text=#{@just_this_post_tweet_string}"
        %br  
        - if @picks_tweet_string.size > 130
          = link_to "I want to tweet my current full card", "https://twitter.com/intent/tweet?text=#{@picks_tweet_string}", confirm: "Click OK below. Then you may need to shorten the tweet to 140 characters. Most people remove the city name from the picks."
        - else  
          = link_to "I want to tweet my current full card", "https://twitter.com/intent/tweet?text=#{@picks_tweet_string}"  
        %br
        %br  
    = f.submit "SUMBIT THIS WRITE-UP", class: 'btn-xlarge btn-block btn-primary' 
    %br
    %br
    = f.submit "No additional write-up", class: 'btn-xlarge btn-block btn-primary'

任何想法有什么问题?

1 个答案:

答案 0 :(得分:1)

这有点复杂,但是现在你已经改变了节目路线,你间接改变了编辑路线。通常,编辑路线基于节目路线。例如:

show => "posts/:id"
edit => "posts/:id/edit"

现在你必须制作一个编辑路线:

"posts/:id/:league_name/:post_description"

为了在您的编辑表单中执行此操作:

= form_for([@post, @league, post_description: @post.post_description) do |f|

这假设您的@league对象的to_param方法返回league_name,否则您必须写:

= form_for([@post, league_name: @league.league_name, post_description: @post.post_description) do |f|

我确信你可以看到这种复杂程度可能并不理想,但这就是Rails的工作原理。因此谚语:“约定优于配置”