在Ruby on Rails中为现有任务控制器添加upvote / score?

时间:2014-05-11 19:18:33

标签: ruby-on-rails ruby ruby-on-rails-4 devise

我对Rails非常陌生,我正在尝试构建一个能够提升Todos能力的Todo应用。目前我的Todo模型如下;

class Todo < ActiveRecord::Base
  belongs_to :user
  belongs_to :house

  default_scope { order("score, created_at DESC") }
end

我的Todos控制器如下;

class TodosController < ApplicationController

  before_filter :authenticate_user!
  before_filter :assign_house_from_invite!
  before_filter :ensure_house!

  def index
    @todo = Todo.new user: current_user
    @todos = current_user.house.todos
  end

  def create
    @todo = Todo.new(todo_params.merge(user: current_user, house: current_user.house))
    @todo.save
  end

  protected

  def todo_params
    params.require(:todo).permit(:task, :vote)
  end

  def assign_house_from_invite!

  end

  def ensure_house!
    redirect_to new_house_path if current_user.house.nil?
  end

end

和我的Todos erb部分是;

<div class="row input-append">
  <%= form_for(@todo, remote: true, id: "new_todo_form") do |t| %>
    <%= t.text_field :task, :placeholder => "Add new task", :class => "span2", :id => "appendedInputButton", :type => "text" %>
    <%= t.submit '+', :id =>'plus', :class => 'btn', :type => 'button' %>

  <% end %>
</div>

<div id="todos" class="row">
  <% @todos.each do |todo| %>
    <%= render partial: 'todo', locals: {todo: todo} %>
  <% end %>
</div>

我想知道我必须添加到每个任务中以使每个任务的初始分数分配为0,然后在它被投票时能够增加吗?任何帮助将不胜感激。

0 个答案:

没有答案