路径到方法始终使用首先列出尽管名称不同?

时间:2013-05-08 20:16:43

标签: ruby-on-rails

我是rails的新手并试图创建上/下投票按钮(为了清晰起见,现在实现为文本链接)。但是,无论点击哪个链接,它都会调用第一个链接的操作,尽管名称不同。

我已经在这里阅读了一遍又一遍的文档和答案,并且整天都在努力解决,但仍然无法理解为什么rails无法看到差异,任何帮助都会非常感激。

路线

Futurebot::Application.routes.draw do
  resources :posts do
    resources :comments
  end

resources :posts do
       member do
         post 'delete'
         post 'upVote'
         post 'downVote'
  end
end


match ':posts/:id/:upVote', :controller => 'posts', :action => 'upVote'
match ':posts/:id/:downVote', :controller => 'posts', :action => 'downVote'

如果我删除资源:帖子阻止它无法找到路线,但似乎匹配语句应该有效(这就是网址的样子)

视图

<%= link_to "up: ", :action => 'upVote', :id => post.id %>
<%= link_to "down: ", :action => 'downVote', :id => post.id %>

控制器

  def upVote
    @post = Post.find(params[:id])
    if @post.increment!(:score)
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
      end
    end
  end

    def downVote
    @post = Post.find(params[:id])
    if @post.decrement!(:score)
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
      end
    end
  end

rake routes: with the 2 new routes commented out (so just the block is there)

     up_vote_post POST   /posts/:id/up_vote(.:format)                posts#up_vo
te
   down_vote_post POST   /posts/:id/down_vote(.:format)              posts#down_
vote
                  GET    /posts(.:format)                            posts#index

                  POST   /posts(.:format)                            posts#creat
e
                  GET    /posts/new(.:format)                        posts#new
                  GET    /posts/:id/edit(.:format)                   posts#edit
                  GET    /posts/:id(.:format)                        posts#show
                  PUT    /posts/:id(.:format)                        posts#updat
e
                  DELETE /posts/:id(.:format)                        posts#destr
oy
             root        /    

                                   posts#index

在routes.rb

中使用两条路线耙道路线
      delete_post POST   /posts/:id/delete(.:format)                 posts#delet
e
     up_vote_post POST   /posts/:id/up_vote(.:format)                posts#up_vo
te
   down_vote_post POST   /posts/:id/down_vote(.:format)              posts#down_
vote
                  GET    /posts(.:format)                            posts#index

                  POST   /posts(.:format)                            posts#creat
e
                  GET    /posts/new(.:format)                        posts#new
                  GET    /posts/:id/edit(.:format)                   posts#edit
                  GET    /posts/:id(.:format)                        posts#show
                  PUT    /posts/:id(.:format)                        posts#updat
e
                  DELETE /posts/:id(.:format)                        posts#destr
oy
                         /:post/up_vote/:id(.:format)                post#up_vot
e
                         /:post/down_vote/:id(.:format)              post#down_v
ote
             root        /    

                                   posts#index

添加的路线

match ':post/up_vote/:id' => "post#up_vote"
match ':post/down_vote/:id' => "post#down_vote"

更新

奇怪的是,如果我改变路线到:

match ':post/:id/up_vote/' => "post#up_vote"
match ':post/:id/down_vote/' => "post#down_vote"

..因为看起来像链接,然后错误是

uninitialized constant PostController

我已尝试根据另一个问题中的解决方案使用帖子和帖子

1 个答案:

答案 0 :(得分:0)

在我看来

resources :posts do
  member do
    post 'delete' 
    post 'up_vote'
    post 'down_vote'
  end
end

应该为您提供类似于此

的路线输出
POST /posts/:id/delete(.:format)
     /posts/:id/up_vote(.:format)
     /posts/:id/down_vote(.:format)

他们应该映射到PostsController及其各自的操作deleteup_votedown_vote

你有没有理由在最后有这两条匹配路线?它们看起来像是通配符,我觉得你不需要它们。

真正发生的是与以下内容匹配的任何内容

something/another/path

将被映射到这些路由,特别是第一个路由,它将被分成参数,如

params[:posts] => something
params[:id]    => another
params[:upVote]=> path

发生这种情况是因为您使用的冒号字符允许您指定动态路由。比如像这样的东西

match 'hello/world/:name' => "hello#say"

将映射到HelloController和操作say。在行动中,你将params[:name]等于名字下的任何东西。

因此hello/world/leo params[:name]等于leo

有关详细信息,请查看以下内容:Rails Routes: Dynamic Segments

注意另外,尽量不要在Ruby中使用驼峰大小写的方法名称:D

更新您需要:method => "post" link_to才能发送帖子请求