ruby-on-rails - Ruby on Rails : link_to action,没有路线匹配

标签 ruby-on-rails

我正在进入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/

相关文章:

html - 使用 Ruby,从 JSON 响应中的 HTML 字符串中获取值

ruby-on-rails - 如何在 Ruby on Rails 中将天数输出为小时数?

ruby-on-rails - 获取 OCIError : ORA-00932: inconsistent datatypes From Rails 3

ruby-on-rails - 在 Rails 中使用 has_and_belongs_to_many 实现友谊模型

javascript - 如何使用 Rails 对象的 id 设置选择下拉列表中选项的值

ruby-on-rails - rails : form_for with field for Hash

ruby-on-rails - Rails rescue_from 停止执行流程

ruby-on-rails - Uuid 和 Redis

javascript - 如何显示和链接对象的某个属性

ruby-on-rails - Rails-build_association不适用于has_one和belongs_to关系