Ruby on rails 3 rails 3中的投票应用程序:如何链接到投票方法?
为了自学Rails,我正在构建一个极其简单的投票应用程序 有两个模型,问题和选项。问题有很多选项,选项属于问题 使用标准脚手架,我已经到了可以添加问题、查看问题、添加选项并查看这些选项的阶段 我现在要做的是添加代码,在单击链接时将option.count值增加1。我在期权模型中有一种投票方法:Ruby on rails 3 rails 3中的投票应用程序:如何链接到投票方法?,ruby-on-rails-3,controller,models,erb,Ruby On Rails 3,Controller,Models,Erb,为了自学Rails,我正在构建一个极其简单的投票应用程序 有两个模型,问题和选项。问题有很多选项,选项属于问题 使用标准脚手架,我已经到了可以添加问题、查看问题、添加选项并查看这些选项的阶段 我现在要做的是添加代码,在单击链接时将option.count值增加1。我在期权模型中有一种投票方法: class Option < ActiveRecord::Base validates :text, :presence => :true belongs_to :question
class Option < ActiveRecord::Base
validates :text, :presence => :true
belongs_to :question
def vote_up
self.count += 1
end
end
class选项:true
属于:问题
投票表决
self.count+=1
结束
结束
我的选项控制器看起来像:
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
<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 %>
类选项控制器
我的问题模型如下所示:
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
<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 %>
类问题{:message=>'问题通常有文本…'}
有许多:选项,:依赖=>:销毁
def投票
#也许投票代码会在这里???
结束
结束
我的问题控制器有脚手架创建的常用的新建、创建、编辑和销毁方法。这里没有什么定制
我想在show.html.erb视图中放置投票方法的链接,如下所示:
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
<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 %>
问题:
可耻!目前没有投票的选择。加一些
投票的一个选择是独裁。。。也许再加一些
选项:
:**点击这里投票!
添加一个投票选项
|
所以我想做的是在每个选项旁边添加一个“投票”链接,在选项模型中调用投票方法。这可能很容易,但我遇到了麻烦,非常感谢您的帮助
此外,任何关于如何做得更好的建议都是欢迎的
谢谢
Simon我要做的是在控制器中创建投票方法:
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 %>
{:action=>“投票”,:option_id=>option.id})do | f |%>
我认为@oded harth展示了一条正确的道路,但我有两点意见:
首先,Rails是一种漂亮的语言,编写它是为了简化开发人员的生活;)在Rails中编程时,千万不要忘记这一点。考虑到这一点,我想向你们指出。因此,您只需向上投票,而无需不必要的+=1
。要否决投票,请使用减量()。我相信你可以这样使用:选项。递增(:计数)
第二,我认为为一个简单的投票活动提供一个完整的表单有点脏。你可以用这样的东西
<%= link_to "Vote Up", :url => { :action => :vote_up, :option_id => option.id }, :method => :put %>
谢谢你。只是想澄清一下-你是说在选项控制器中?谢谢。越来越近了。现在,当我点击投票链接时,我会被带到附加了参数的当前页面。。。。。还有,你是指资源:选项而不是资源:投票吗?我已在问题控制器中设置了投票方法。再次感谢!我忘了提到,如果您的项目中有可用的Javascript库,例如jQueryUJS,或者Scriptaculous和Prototype,默认情况下这些库包含在rails项目的“public/javascripts”中,那么这种方法是有效的。您可能还需要定义继承的路由:resources:questions do resources:options do 35;是的,我是说options;)put:vote\u up put:vote\u down end
然后使用rake routes
查看您的路线,并找到put questions/:question\u id/options/:id/vote\u up
。然后使用它的URL方法而不是:URL=>{…}
顺便说一句,投票
方法进入选项控制器
,因为您通过将其值增加/减少1来更改选项
模型。