【问题标题】:Ruby on Rails: link_to action, no route matchesRuby on Rails:link_to 操作,没有路由匹配
【发布时间】:2011-08-28 09:19:37
【问题描述】:

我正在进入 Rails 并尝试在此处的博客设置中添加“投票”功能:http://guides.rubyonrails.org/getting_started.html

在 app/controllers/posts_controller.rb 我创建了这个:

  def incvotes
    @post = Post.find(params[:id])
    post.update_attributes(:upvotes => 1 )
    format.html { redirect_to(@post, :notice => 'Vote counted.') }
    format.xml  { head :ok }
  end

在 app/views/posts/index.html.erb 我创建了这个:

<%= link_to 'Vote', :controller => "posts", :action => "incvotes", :id => post.id %>

但是链接报错

没有路线匹配 {:controller=>"posts", :action=>"incvotes", :id=>1}

我在这里遗漏了一些东西,但不确定是什么。

耙路线:

incvotes_post POST   /posts/:id/incvotes(.:format) {:action=>"incvotes", :controller=>"posts"}
        posts GET    /posts(.:format)              {:action=>"index", :controller=>"posts"}
              POST   /posts(.:format)              {:action=>"create", :controller=>"posts"}
     new_post GET    /posts/new(.:format)          {:action=>"new", :controller=>"posts"}
    edit_post GET    /posts/:id/edit(.:format)     {:action=>"edit", :controller=>"posts"}
         post GET    /posts/:id(.:format)          {:action=>"show", :controller=>"posts"}
              PUT    /posts/:id(.:format)          {:action=>"update", :controller=>"posts"}
              DELETE /posts/:id(.:format)          {:action=>"destroy", :controller=>"posts"}
   home_index GET    /home/index(.:format)         {:action=>"index", :controller=>"home"}
         root        /(.:format)                   {:action=>"index", :controller=>"home"}

【问题讨论】:

  • 你的路由文件对帖子有什么看法?

标签: ruby-on-rails


【解决方案1】:

试试

= link_to "vote", incvotes_post_path(post), :method=>:post

如果这不起作用,请尝试将方法更改为 :put

【讨论】:

  • with that or put,都报错:No route matches "/posts/1/incvotes"
  • 您的代码中可能有错字吗?你得到了post.update_attributes,而它应该是@post.update_attributes。不确定这是否会导致此问题。您也可以发布所有索引视图和控制器代码吗?你有 github 帐户,可以在其中发布模型、控制器和索引视图的代码吗?也许只是把它贴在馅饼上。很抱歉让您感到痛苦,但有些地方不对劲
  • 你的 incvotes 方法中肯定有错字 - 应该是 @post.update_attributes - 不能说这是否能解决问题
  • 看起来像。现在我得到“参数太少”,如果我解决它应该可以工作。
  • 当您将其更改为 @post.update_attributes(:upvotes => 1 ) 时,您的参数太少 - 我会考虑一下
【解决方案2】:

我的猜测是,您的路由文件中可能没有您刚刚在控制器中定义的操作的定义。为了让 Rails 正确生成 url,控制器中的动作和路由文件中的动作都必须定义。

你的路由文件可能是这样的:

resources :posts

但您想添加的不仅仅是resources 关键字生成的标准操作,所以请尝试以下操作:

resources :posts do 
  member do
    post 'incvotes'
  end
end

这告诉路由,您的帖子控制器中有另一个名为 incvotes 的操作,只要它们指向具有正确操作的成员路由(/posts/14 是成员路由,而 /posts/是“收集”路线)。因此,您将拥有一条可能像 /posts/14/incvotes 这样的新路线,您可以向该路线发布表单,并且一切都应该开始正常工作。

编辑:

实际上我猜因为您只是将 1 添加到模型的属性中,所以您不需要 POST 操作(通常与发布表单相关联,如 createupdate)。要发送帖子,您可能需要更改视图中的 HTML 以包含表单并将其发布到正确的 url。所以你可以试试,或者你可以将你的路由文件改为get 'incvotes'而不是post 'incvotes'。很抱歉给您带来了困惑,希望对您有所帮助!

【讨论】:

  • 我的路线文件看起来确实像这样,所以我将其更改为那个,现在投票按钮给出:没有路线匹配“/posts/1/incvotes”
  • 您可以从命令行运行rake routes 并将输出添加到您的问题中吗?
  • 你试过重启服务器吗?我相信如果您完全更改 routes.rb 文件,您需要重新启动 Web 服务器以使更改生效(即使 rake routes 的输出看起来正确,您的 Web 服务器也可能没有接收到更改,如果它没有)尚未重新启动)。
  • 我刚刚尝试重新启动“rails 服务器”和 Apache,但没有帮助。仍然被带到domain.com/posts/1/incvotes 并且错误 No route matches "/posts/1/incvotes"
  • 编辑了我的答案以提供有关 POST 与 GET 的更多信息
【解决方案3】:

incvotes_post 路由只接受 HTTP POST,而链接总是产生 HTTP GET。

改为使用带有按钮的表单(或使用 AJAX 进行 POST)。

【讨论】:

  • 几乎,link_to 文档声明将:method 参数作为 URL 选项传递给 link_to 动态创建一个带有 post 方法的 HTML 表单。所以你可以在技术上使用link_to 来创建一个 POST 请求。
【解决方案4】:

尝试使用button_to 代替link_to

在你看来:

<%= button_to 'Vote', incvotes_post_path(post) %>

在您的config/routes.rb 中,将incvotes 操作的路由添加为post

resources :posts do 
  member do
    post 'incvotes'
  end
end

在您的控制器中,创建incvotes 操作:

def incvotes
  # Something
  redirect_to :posts
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多