我正在进入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 %>
但是链接给出了错误
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"}
最佳答案
尝试
= link_to "vote", incvotes_post_path(post), :method=>:post
如果这不起作用,请尝试将方法更改为:put
关于ruby-on-rails - Ruby on Rails : link_to action,没有路线匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6050739/