试图将URL锚点传递给Rails中的控制器

时间:2015-08-28 16:04:03

标签: ruby-on-rails ruby

尝试将url的锚点传递给控制器​​时出现以下错误:

No route matches {:action=>"feed_index", :controller=>"feed"} missing required keys: [:anchor]

index.html.erb

<%= link_to "Feed", feed_feed_index_path(@feed_index, :anchor => "12345") %>

FeedController.rb

class FeedController < ApplicationController

  def feed_index
    @picture = Picture.where(:number => params[:anchor])
  end

end

的routes.rb

get "/feed/feed_index/:anchor" => "feed#feed_index", :as => :feed_feed_index

1 个答案:

答案 0 :(得分:1)

Rails查看网址中的:anchor密钥,并认为另一个参数是什么。

修正您的路线:

get "/feed/feed_index/:feed_index" => "feed#feed_index", :as => :feed_feed_index

使用相同的网址助手:

<%= link_to "Feed", feed_feed_index_path(@feed_index, :anchor => "12345") %>
                    ^^^^^^^^^^^^^^^^     ^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^
                    "/feed/feed_index"   :feed_index   #anchor

生成链接:

http://localhost:3000/feed/feed_index/1#12345

测试。

了解routes