ror突进-p6

对 Topic 投票

建立投票的model

rails generate model vote topic_id:integer
rake db:migrate

将投票记录和Topics连接起来

修改指定文件app/models/topic.rb

class Topic < ActiveRecord::Base
  has_many :votes, dependent: :destroy
end

修改app/models/vote.rb

class Vote < ActiveRecord::Base
  belongs_to :topic
end

在railsconsole里面查看model

localhost:t2 alan$ rails c
Running via Spring preloader in process 87341
Loading development environment (Rails 5.0.0.1)
2.3.1 :001 > Topic.count
   (0.2ms)  SELECT COUNT(*) FROM "topics"
 => 1
2.3.1 :002 > my_topic = Topic.first
  Topic Load (0.2ms)  SELECT  "topics".* FROM "topics" ORDER BY "topics"."id" ASC LIMIT ?  [["LIMIT", 1]]
 => #<Topic id: 1, title: "top01", description: "des01", created_at: "2016-09-28 14:43:05", updated_at: "2016-09-28 14:43:05">
2.3.1 :003 > my_topic.update_attributes(title: 'Edited in the console')
   (0.1ms)  begin transaction
  SQL (0.7ms)  UPDATE "topics" SET "title" = ?, "updated_at" = ? WHERE "topics"."id" = ?  [["title", "Edited in the console"], ["updated_at", 2016-09-28 15:16:07 UTC], ["id", 1]]
   (1.5ms)  commit transaction
 => true
2.3.1 :004 > my_topic.votes.create
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "votes" ("topic_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["topic_id", 1], ["created_at", 2016-09-28 15:16:15 UTC], ["updated_at", 2016-09-28 15:16:15 UTC]]
   (0.8ms)  commit transaction
 => #<Vote id: 1, topic_id: 1, created_at: "2016-09-28 15:16:15", updated_at: "2016-09-28 15:16:15">
2.3.1 :005 >

这其中遇到任何问题先尝试使用spring stop解决。

添加投票器

修改app/controllers/topics_controller.rb

def upvote
  @topic = Topic.find(params[:id])
  @topic.votes.create
  redirect_to(topics_path)
end

为投票加route,修改config/routes.rb

Rails.application.routes.draw do
 root 'topics#index'
 resources :topics do
   member do
     post 'upvote'
   end
 end
end

在view层添加按钮,修改app/views/topics/index.html.erb的loop部分

<% @topics.each do |topic| %>
  <tr>
    <td><%= topic.title %></td>
    <td><%= topic.description %></td>
    <td><%= pluralize(topic.votes.count, "vote") %></td>
    <td><%= button_to '+1', upvote_topic_path(topic), method: :post %></td>
    <td><%= link_to 'Show', topic %></td>
    <td><%= link_to 'Edit', edit_topic_path(topic) %></td>
    <td><%= link_to 'Destroy', topic, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

修改Topic 之后转回 Topics 列表

修改app/controllers/topics_controller.rb,将create和update中的@topic改为topics_path

  def create
    @topic = Topic.new(topic_params)

    respond_to do |format|
      if @topic.save
        format.html { redirect_to topics_path, notice: 'Topic was successfully created.' }
        format.json { render :show, status: :created, location: @topic }
      else
        format.html { render :new }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /topics/1
  # PATCH/PUT /topics/1.json
  def update
    respond_to do |format|
      if @topic.update(topic_params)
        format.html { redirect_to topics_paths, notice: 'Topic was successfully updated.' }
        format.json { render :show, status: :ok, location: @topic }
      else
        format.html { render :edit }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

添加flash message

修改app/views/layouts/application.html.erb,添加

<% flash.each do |name, msg| %>
    <div><%= msg %></div>
<% end %>