2016-08-01 79 views
0

我想更改show路径以使其更适合搜索引擎优化。当我拿出form_foredit页面上导轨路由:更改资源的“显示”路径

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">} 

:下面的代码试图去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 

错误时抛出一个错误。 。页面呈现。所以这个问题必须在此代码是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' 

任何想法有什么错呢?

+0

从form_for更改form_tag已工作。 form_tag(“/ posts/#{@post.id}”,方法:: put)do – slindsey3000

+0

最后..我去了这个form_for(@post,url:{action:“update”})do | f | ......当然,我更新了我的代码块以包含f表单助手。但form_tag也可以像我在上面的评论中提到的那样工作。 – slindsey3000

回答

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的工作原理。因此,格言:“公约超过配置”

+0

谢谢。我懂了。有趣的是......从form_for变成form_tag已经奏效了。 form_tag(“/ posts/#{@post.id}”,方法:: put)do – slindsey3000

+0

想知道我是否应该保留它,如何使用form_tag或者使用你的例子 – slindsey3000

+0

也是form_for中的'['故意的? – slindsey3000