2011-05-18 86 views
5

我进入Rails和尝试在博客设置从这里添加一个“投票”功能:http://guides.rubyonrails.org/getting_started.htmlRuby on Rails的:行动的link_to,没有路由匹配

在应用程序/控制器/ 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 

在应用程序/视图/职位/ index.html.erb我创造了这个:

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

但链接是给错误

No route matches {: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"} 
+0

什么是你的路由文件说有关的职位? – sscirrus 2011-05-18 20:49:08

+0

资源:帖子 – Zeno 2011-05-18 20:55:09

回答

3

尝试

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

,如果还是不行,请尝试更改方法:把

+0

用这个或放,都给出了错误:没有路线匹配“/ posts/1/incvotes” – Zeno 2011-05-18 22:39:57

+0

你可能在你的代码中有一个错字吗?当它应该是'@ post.update_attributes'时,你有'post.update_attributes'。不知道这是否会导致这个问题。你可以发布所有的索引视图和控制器代码吗?你有github帐户,你可以在其中发布模型,控制器和索引视图的代码吗?也许只是坚持它的爱情。对不起,有点不对劲 – stephenmurdoch 2011-05-18 22:46:42

+0

3个文件在这里:http://pastie.org/1924217 – Zeno 2011-05-18 22:55:59

3

我的猜测是,你可能没有一个定义你的路由文件为你只是在控制器中定义的操作。必须为Rails定义控制器中的动作和路由文件中的动作,以正确生成URL。

你的路由文件可能有这样的事情:

resources :posts 

但是你想添加更多的不是由resources关键字生成的标准动作,所以尝试这样的事:

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

这告诉路由,您的帖子控制器中有另一个动作,称为incvotes,只要它们使用正确的动作指向成员路由(/ posts/14是成员路由,而/ posts /是'collection '路线)。因此,您可能会有一条新路线,可能类似/posts/14/incvotes,您可以发布表单并开始正常工作。

编辑:

其实我猜,因为你是刚刚加入1对模型的属性,你并不需要一个POST操作(这通常与发布形式与createupdate相关)。要发送帖子,您可能需要更改视图中的HTML以包含表单并将其发布到正确的网址。所以你可以试试,或者你可以改变你的路线文件来读取get 'incvotes'而不是post 'incvotes'。对于混淆抱歉,希望有所帮助!

+0

我的路由文件确实看起来像这样,所以我改变它,现在的投票按钮:没有路由匹配“/ posts/1/incvotes” – Zeno 2011-05-18 20:56:04

+1

你可以从命令行运行'rake routes'并添加输出到你的问题? – 2011-05-18 21:21:28

+0

我编辑它为我的第一篇文章格式的原因。 – Zeno 2011-05-18 21:41:28

1

incvotes_post路由只接受HTTP POST,并且链接始终生成HTTP GET。

改用一个按钮的形式(或使用AJAX进行POST)。

+2

几乎所有'link_to'文档都声明将':method'参数作为URL选项传递给link_to,可以使用post方法动态创建HTML表单。所以你可以在技术上使用'link_to'来创建一个POST请求。 – 2011-05-18 23:27:14

0

尝试使用button_to代替link_to

在你看来:

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

在你的config/routes.rb添加路线t Øincvotes行动post

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

而在你的控制器,创建incvotes行动:

def incvotes 
    # Something 
    redirect_to :posts 
end 
相关问题