Rails 3 中的投票应用程序:如何链接到投票方法?

发布于 2024-11-01 14:30:53 字数 2431 浏览 4 评论 0原文

为了自学 Rails,我构建了一个极其简单的投票应用程序。

有两种模型:问题和选项。问题有_许多选项和选项属于_问题。

使用标准脚手架,我已经达到了一个阶段,您可以添加问题、查看它、向其添加选项并查看这些选项。

我现在想做的是添加代码,在单击链接时将 option.count 值加一。我的选项模型中有一个 vote_up 方法:

class Option < ActiveRecord::Base
  validates :text, :presence => :true
  belongs_to :question

  def vote_up
    self.count += 1
  end
end

我的选项控制器看起来像:

class OptionsController < ApplicationController
  def create
    @question = Question.find(params[:question_id])
    @option = @question.options.create(params[:option])
    redirect_to question_path(@question)
  end

end

我的问题模型看起来像:

class Question < ActiveRecord::Base
  validates :text, :presence => {:message => 'A question normally has text...'}
  has_many :options, :dependent => :destroy


  def vote
    # Maybe the vote code will go here???
  end
end

我的问题控制器具有脚手架创建的通常的新、创建、编辑、销毁方法。这里有一点定制。

我想要将投票方法的链接放在我的 show.html.erb 视图中,如下所示:

  <p id="notice"><%= notice %></p>
    <p>
      <b>Question <%= @question.guid %></b>:
      <%= @question.text %>
    </p>
    <% if @question.options.count == 0 %>
        <p>Shame! there are currently no options to vote on. Add some! </p>
    <% elsif @question.options.count == 1 %>
        <p>One option in a vote is a dictatorship... Maybe add some more?</p>
    <% end %>
    <h2>Options:</h2>
    <% @question.options.each do |option| %>
      <p>
        <%= option.text %>:  ** Link to vote here!
      </p>
    <% end %>

    <h2>Add an option to vote on</h2>
    <%= form_for([@question, @question.options.build]) do |f| %>
      <div class="field">
        <%= f.label :text %><br />
        <%= f.text_field :text %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>

    <% if @question.options.count == 0 # Only show edit if no options saved. %> 
        <%= link_to 'Edit', edit_question_path(@question) %> |
    <% end %>
    <%= link_to 'Back', questions_path %>

所以我想做的是在选项模型中调用 vote_up 方法的每个选项旁边添加一个“投票”链接。这可能很简单,但我遇到了困难,非常感谢任何帮助。

此外,欢迎任何有关如何做得更好的建议!

谢谢西蒙

To teach myself Rails, im building an extremely simple Voting app.

There are 2 models, Question and Option. Question has_many Options and Option belongs_to Question.

Using the standard scaffolding, I have reached a stage where you can add a question, view it, and add options to it and see these options.

What I would like to do now is add the code that increases an option.count value by one when clicking on a link. I have a vote_up method in the Option model:

class Option < ActiveRecord::Base
  validates :text, :presence => :true
  belongs_to :question

  def vote_up
    self.count += 1
  end
end

My Options controller looks like:

class OptionsController < ApplicationController
  def create
    @question = Question.find(params[:question_id])
    @option = @question.options.create(params[:option])
    redirect_to question_path(@question)
  end

end

My Question model looks like:

class Question < ActiveRecord::Base
  validates :text, :presence => {:message => 'A question normally has text...'}
  has_many :options, :dependent => :destroy


  def vote
    # Maybe the vote code will go here???
  end
end

And my Question controller has the usual new, create, edit, destroy methods that the scaffold creates. V little customisation here.

My show.html.erb view where I would like to put the link to the vote method looks like:

  <p id="notice"><%= notice %></p>
    <p>
      <b>Question <%= @question.guid %></b>:
      <%= @question.text %>
    </p>
    <% if @question.options.count == 0 %>
        <p>Shame! there are currently no options to vote on. Add some! </p>
    <% elsif @question.options.count == 1 %>
        <p>One option in a vote is a dictatorship... Maybe add some more?</p>
    <% end %>
    <h2>Options:</h2>
    <% @question.options.each do |option| %>
      <p>
        <%= option.text %>:  ** Link to vote here!
      </p>
    <% end %>

    <h2>Add an option to vote on</h2>
    <%= form_for([@question, @question.options.build]) do |f| %>
      <div class="field">
        <%= f.label :text %><br />
        <%= f.text_field :text %>
      </div>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>

    <% if @question.options.count == 0 # Only show edit if no options saved. %> 
        <%= link_to 'Edit', edit_question_path(@question) %> |
    <% end %>
    <%= link_to 'Back', questions_path %>

So what I am trying to do is add a "vote" link next to each option that calls the vote_up method in the options model. This is probably laughably easy, but i've hit a wall and would really appreciate any help.

Also, any suggestions on how to do this better would be welcome!

Thanks

Simon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-11-08 14:30:53

我认为 @oded-harth 已经展示了正确的方法,但我有两点意见:

首先,Rails 是一门美丽的语言,编写它是为了简化我们的开发人员生活;)在 Rails 中编程时,你永远不能忘记这一点。考虑到这一点,我想向您指出 “increment()”方法。因此,您可以简单地投票,而无需不必要的 += 1。要否决投票,请使用 decrement()。我相信您可以像这样使用它: option.increment(:count)

其次,我认为为简单的投票操作使用整个表单有点肮脏。你可以使用这样的东西

<%= link_to "Vote Up", :url => { :action => :vote_up, :option_id => option.id }, :method => :put %>

要使其工作,你必须设置你的路线,如下所示:

resources :votes
  put :vote_up
end

I think @oded-harth has showed a right way, but I have two remarks:

First of all, Rails is a beautiful language and is written to simplify our developer lives ;) When programming in Rails you must never forget that. With this in mind, I want to point you to "increment()" method. So you can simply up-vote without unnecessary += 1. To down-vote use decrement(). I believe you can use it like this: option.increment(:count)

Second, I think it's a little dirty to have a whole form for a simple vote action. You can use something like this

<%= link_to "Vote Up", :url => { :action => :vote_up, :option_id => option.id }, :method => :put %>

To make it work you'll have to set your route something like this:

resources :votes
  put :vote_up
end
荒路情人 2024-11-08 14:30:53

我要做的就是在控制器中创建 vote_up 方法:

def vote_up 

option = Option.find(params[:option_id])
option.count += 1

redirect (to where do you want...)

end

在视图中我会这样调用该方法:

<%= form_for( option, :url => { :action => "vote_up", :option_id => option.id} ) do |f| %>
<%= f.submit("vote up") %>  
<% end %>

What I would do is make the vote_up method in the controller:

def vote_up 

option = Option.find(params[:option_id])
option.count += 1

redirect (to where do you want...)

end

And in the view I would call that method this way:

<%= form_for( option, :url => { :action => "vote_up", :option_id => option.id} ) do |f| %>
<%= f.submit("vote up") %>  
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文