2009-08-25 77 views
1

[查看更多後來答案]Rails的`link_to`方法張貼多次

我認爲這只是一個簡單的軌的問題,主要是如何我命名我的模型,並在我看來引用它們。那麼一點點的背景下,我使用的是vote_fu插件,我認爲這是偉大的,但我有一個很難得到投票從一個鏈接的工作方式如下:

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

current_user是一個輔助並返回在提出這個要求登錄當前用戶,這個問題很可能在途中我的狀態answer屬於我的question模型, 任何幫助非常感謝,即使你只是幫我正確的道路!

哦!同時它在控制檯...因此,如果我做的:

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

它按預期工作。

+1

您是否確認current_user實際上正在返回用戶對象? – 2009-08-25 04:07:00

+2

因爲我不知道vote_fu,我只會發表評論,但'current_user.vote_for(answer)'返回的是什麼?你不應該有一個路徑或模型的實例具有寧靜的資源嗎? – theIV 2009-08-25 04:08:42

+0

嘿@theIV你是對的...我確實需要一條路徑,但是因爲它是has_many關聯的主題,那麼這條路徑是什麼樣子的?我很困惑... user_answer_votes_path(答案) – 2009-08-25 04:17:36

回答

1

current_user.vote_for(answer)將調用方法。只是因爲它在:url並不意味着它得到任何特殊待遇。它將像任何其他ruby方法一樣被執行。

你可能想要做這樣的事情。

// 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} 
0

Blah!我解決了我的問題的第一部分......嗯......也許,這似乎是我的解決方案是不是很寧靜,這裏是我做過什麼:

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

所以這裏的奇怪的是,它的工作原理,但每次我重新加載頁面時,它都會向所有用戶提供一個投票答案(或者大多數博客所關注的評論),但這也使得網址localhost/questions/65?method=post&url=true,我不確定我的下一步行動計劃是什麼。


多一點調查後,vote_fu示例應用程序有這個控制器的創建行動的例子,但我甚至不有我的投票模式,但仍取得了票,任何想法控制器?

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 
+2

就像我上面說的那樣,'vote_for'調用屬於你的控制器。 'vote_for'投了一票。 – 2009-08-25 06:06:37

+0

它已經在插件的功能,我仍然需要把它放在控制器? – 2009-08-25 06:12:17

+1

你提到它在控制檯中工作。您正在向您的用戶對象發送一條消息「爲最後一個問題投票」。現在想象一下當Ruby構建鏈接時會發生什麼:當您構建鏈接時,您正在向您的用戶對象發送一條消息「爲此答案投票」。讓我們看一下示例代碼 - /examples/votes/_voteable_vote.html.erb - 如果您想使用此插件,最簡單的方法是在該視圖之後對代碼進行建模。 – 2009-08-25 06:24:02

1

的例子,這是很老,有這樣的觀點:

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

但是這使得URL有查詢字符串的投票方法......他使用RJS,和我更喜歡使用jQuery,不確定如何開始我的下一步工作,而不是爲網頁上的所有答案投票?

1

澄清了 「vote_for屬於你的控制器」:

您有:

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

你真正想要的是:

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

current_user.vote_for方法是對的方法服務器。如果您在視圖(.rhtml文件)中調用該方法,則投票會在頁面呈現時發生,而不是在用戶單擊鏈接時發生。相反,在適當的控制器上創建一個「投票」方法(我在這裏稱它爲my_controller,隨心所欲)。 vote方法看起來像vote_fu示例。 link_to創建投票操作的超鏈接。

如果您嘗試通過ajax執行此操作,請將link_to替換爲link_to_remote

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

如果你的意見/ my_controller /票模板文件是」 .rjs',當方法的結果返回到頁面在該文件中的JavaScript將被執行。如果是「的.rhtml」,你應該考慮的助手爲link_to_remote在頁面上輕鬆地更新一個div與包含方法的結果(http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645 - 你想要的:update選項)