简体   繁体   English

Rails的link_to方法多次发布

[英]Rails `link_to` method posting multiple times

[see later answer for more] [请参阅稍后的答案以获取更多信息]

I think this is just a simple rails question, and mostly on how i've named my models and reference them in my view. 我认为这只是一个简单的Rails问题,主要涉及我如何命名模型并在我看来引用它们。 So a little background, I'm using the vote_fu plugin, which I think is great, however I'm having a hard time getting the voting to work from a link as follows: 有一点背景知识,我使用的是vote_fu插件,我认为这很棒,但是我很难通过下面的链接来进行投票:

<%= link_to "vote for", current_user.vote_for(answer) %>

Where current_user is a helper and returns the current user that is logged in making this request, the issue is likely in the way I state answer which belongs to my question model, any help is really appreciated , even if you just help me down the right path! current_user是帮助程序并返回发出此请求的当前用户的情况下,问题可能出在我陈述属于我的question模型的answer的方式上,即使您只是正确地帮助了我,也非常感谢您的帮助路径!

oh! 哦! also it works in console... so if I do: 它也可以在控制台中工作...所以,如果我这样做:

user = User.find(1)
user.vote_for(Question.last)

It works as expected. 它按预期工作。

The example, which is pretty old, has this in the view: 该示例相当古老,它在视图中具有以下内容:

<%= link_to "Vote Up", :url => current_user.vote_for(answer), :method => :post %>

But this makes the URL have the vote method in the query string... he is using rjs, and I'd prefer to use jquery, not sure how to go about my next step in getting this to work and not vote for all the answers on the page? 但这使URL在查询字符串中具有表决方法...他正在使用rjs,而我更喜欢使用jquery,不确定如何继续进行下一步,而不是对所有页面上的答案?

A clarification of " vote_for belongs in your controller": 澄清“ vote_for属于您的控制器”:

You have: 你有:

<%= link_to "Vote Up", current_user.vote_for(answer) %>

What you really want is: 您真正想要的是:

<%= link_to "Vote Up", :controller => 'my_controller', :action => 'vote', :id => answer %>

The current_user.vote_for method is a method on the server. current_user.vote_for方法是服务器上的方法。 If you call that method in your view (.rhtml file), the vote will happen when the page is rendered, not when the user clicks the link. 如果在视图(.rhtml文件)中调用该方法,则投票将在呈现页面时发生,而不是在用户单击链接时发生。 Instead, create a 'vote' method on an appropriate controller (I called it my_controller here, make it whatever you want). 而是在适当的控制器上创建一个“表决”方法(我在这里将其称为my_controller ,使其随心所欲)。 The vote method then looks like the vote_fu example. 然后vote方法看起来像是vote_fu示例。 The link_to creates a hyperlink to the voting action. link_to创建指向投票操作的超链接。

If you're trying to do this via ajax, replace link_to with link_to_remote . 如果您尝试通过ajax执行此操作,请将link_to替换为link_to_remote

<%= link_to_remote "Vote Up", :url => { :controller => 'my_controller', :action => 'vote', :id => answer } %>

If your views/my_controller/vote template file is '.rjs', the javascript in that file will be executed when the result of the method returns to the page. 如果您的views / my_controller / vote模板文件为“ .rjs”,则当方法结果返回页面时,将执行该文件中的javascript。 If it is '.rhtml', you should look into the helpers for link_to_remote for easily updating a div on the page with the contains of the result of the method ( http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645 - you want the :update option ) 如果是'.rhtml',则应查看link_to_remote的帮助程序,以轻松地在页面上使用方法结果的内容更新div( http://api.rubyonrails.org/classes/ActionView/Helpers/ PrototypeHelper.html#M001645-您需要:update选项)

current_user.vote_for(answer) will call that method right away. current_user.vote_for(answer)将立即调用该方法。 Just because it's in :url doesn't mean it gets any special treatment. 仅仅因为它在:url中并不意味着它会得到任何特殊待遇。 It'll be executed just like any other ruby method. 它将像其他任何ruby方法一样执行。

You probably want to do something like this. 您可能想要做这样的事情。

// view
<%= link_to "Vote up", :url => vote_up_answer_path(answer), :method => "post" %>

// controller
class AnswersController < ApplicationController
  def vote_up
    answer = Answer.find(params[:id])
    current_user.vote_up(answer)
    redirect_to :back
  end
end

// routes
map.resources :answers, :member => {:vote_up => :post, :vote_down => :post}

Blah! 胡说! I solved the first part of my question... well... maybe, it seems my solution isn't very restful, here is what I did: 我解决了问题的第一部分...好吧...也许,看来我的解决方案不是很轻松,这是我所做的:

<%= link_to "Vote Up", :url => current_user.vote_for(answer), :method => :post %>

So the weird thing here is that it works, but every time I reload the page it adds a vote up to all the users with an answer (or comments as most blogs would be concerned), it also makes the url localhost/questions/65?method=post&url=true , I'm not sure what my next plan of action here is. 因此,这里很奇怪的是,它可以正常工作,但是每次我重新加载页面时,它都会为所有用户添加答案(或大多数博客会关注的评论)的投票,这也会使url localhost/questions/65?method=post&url=true ,我不确定我的下一个行动计划是什么。


After a little more investigation, the example the vote_fu example app has this in the controller's create action, but I don't even have a controller for my vote model and the votes are still made, any idea? 经过更多调查后,该示例poll_fu示例应用程序已在控制器的create动作中添加了此示例,但我什至没有投票模型的控制器,并且仍在进行投票,知道吗?

def create
@quote = Quote.find(params[:quote_id])

respond_to do |format|
  if current_user.vote(@quote, params[:vote])
    format.rjs  { render :action => "create", :vote => @vote }
    format.html { redirect_to([@quote.user, @quote]) }
    format.xml  { render :xml => @quote, :status => :created, :location => @quote }
  else
    format.rjs  { render :action => "error" }
    format.html { render :action => "new" }
    format.xml  { render :xml => @vote.errors, :status => :unprocessable_entity }
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM