加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby on Rails:link_to动作,没有路由匹配

发布时间:2020-12-17 03:27:34 所属栏目:百科 来源:网络整理
导读:我正在进入Rails并试图在博客设置上添加“投票”功能: http://guides.rubyonrails.org/getting_started.html 在app / controllers / posts_controller.rb中我创建了这个: def incvotes @post = Post.find(params[:id]) post.update_attributes(:upvotes =
我正在进入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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读