Ruby on rails 如何创建删除链接

Ruby on rails 如何创建删除链接,ruby-on-rails,routes,ruby-on-rails-4,Ruby On Rails,Routes,Ruby On Rails 4,如何在Rails应用程序中创建删除链接 投票控制员 class VotesController < ApplicationController def destroy @user = User.find(params[:id]) @user.votes.pluck(:author_uid).delete(current_user.uid) end end 我应该在视图中的链接中写什么 我试过了 = link_to 'Delete

如何在Rails应用程序中创建删除链接

投票控制员

    class VotesController < ApplicationController

    def destroy
        @user = User.find(params[:id])
        @user.votes.pluck(:author_uid).delete(current_user.uid)
    end

end
我应该在视图中的链接中写什么

我试过了

= link_to 'Delete vote', {controller: "votes", action: "destroy"}, method: "delete"

users/index.html.haml

- @vk.friends.get(uid: current_user.uid, fields: fields) do |friend|
  %td.span
    .centred
      .image
        .circled
          = image_tag friend.photo_medium
      %span= friend.uid
      %span= link_to "#{friend.first_name}  #{friend.last_name}", "http://vk.com/id#{friend.uid}", target: "_blank"
      %span= define_age(friend) == '' ? define_sex(friend) : define_sex(friend) + ', ' + define_age(friend)
      - if current_user.user_votes.pluck(:recipient_uid).include?(friend.uid)
        = link_to('Delete',{controller: :votes, id: vote.id, action: :destroy}, confirm: "Are you sure you want to delete ?", method:  :delete)
      - else
        = link_to 'Vote', {controller: "votes", action: "create", uid: friend.uid}, method: "post", confirm: "You sure", class: 'button medium pink'
当然不行了。我肯定我应该用路线来解决它,但我不知道怎么做

如果您需要更多信息,请发表评论

谢谢

试试这个

= link_to('Delete', vote_path(vote.id),:method => :delete, :confirm => "Are you sure you want to delete?")  
OR       
= link_to('Delete',{controller: :votes, id: vote.id, action: :destroy}, confirm: "Are you sure you want to delete ?", method:  :delete)
试试这个

= link_to('Delete', vote_path(vote.id),:method => :delete, :confirm => "Are you sure you want to delete?")  
OR       
= link_to('Delete',{controller: :votes, id: vote.id, action: :destroy}, confirm: "Are you sure you want to delete ?", method:  :delete)

你的第二个
链接到
在语法上很好。问题是,
投票
没有定义。在您的视图中尝试以下操作:

  %span= define_age(friend) == '' ? define_sex(friend) : define_sex(friend) + ', ' + define_age(friend)
  - vote = current_user.user_votes.find_by_recipient_uid(friend.uid).first
  - if vote
    = link_to('Delete', vote, confirm: "Are you sure you want to delete ?", method:  :delete)
  - else
    = link_to 'Vote', {controller: "votes", action: "create", uid: friend.uid}, method: "post", confirm: "You sure", class: 'button medium pink'

正如我在对你的问题的评论中所指出的,你还需要整理你的破坏行动;你的创造行为也可能以类似的方式受到怀疑;我还没有研究它。

你的第二个链接到
在语法上很好。问题是,
投票
没有定义。在您的视图中尝试以下操作:

  %span= define_age(friend) == '' ? define_sex(friend) : define_sex(friend) + ', ' + define_age(friend)
  - vote = current_user.user_votes.find_by_recipient_uid(friend.uid).first
  - if vote
    = link_to('Delete', vote, confirm: "Are you sure you want to delete ?", method:  :delete)
  - else
    = link_to 'Vote', {controller: "votes", action: "create", uid: friend.uid}, method: "post", confirm: "You sure", class: 'button medium pink'

正如我在对你的问题的评论中所指出的,你还需要整理你的破坏行动;你的创造行为也可能以类似的方式受到怀疑;我没有研究过。

我相信你的第二个例子应该有用。你得到了什么?它会生成什么HTML,当您单击链接时会发生什么请求?它会抛出一个错误:未定义的局部变量或方法“投票”给#Ah。当你的意思是“投票”或类似的时候,很可能你误用了“投票”。您能否发布控制器代码,该代码生成包含链接到
的视图?此外,您的销毁方法也不起作用。您可以找到id为表单所针对的
投票
的用户;然后生成该用户投票的数组“
author\u uid
字段;然后从该数组中删除当前用户的id,而不是从数据库中删除。您可能只需要
Vote.destroy(params[:id])
Updated Vote\u控制器。如果我不明白,请告诉我。我认为你的第二个例子应该有效。你得到了什么?它会生成什么HTML,当您单击链接时会发生什么请求?它会抛出一个错误:未定义的局部变量或方法“投票”给#Ah。当你的意思是“投票”或类似的时候,很可能你误用了“投票”。您能否发布控制器代码,该代码生成包含链接到的视图?此外,您的销毁方法也不起作用。您可以找到id为表单所针对的
投票
的用户;然后生成该用户投票的数组“
author\u uid
字段;然后从该数组中删除当前用户的id,而不是从数据库中删除。您可能只需要
Vote.destroy(params[:id])
Updated Vote\u控制器。告诉我我是否不理解NameError未定义的局部变量或方法'vote'for#NameError未定义的局部变量或方法'vote'for#