0

我一直在RailsTutorialMichael Hartl一些chapter 10额外的事情来为每个微帖子添加一个回形针图像。当我在 中添加照片时Micropost,我将其称为Microphoto
现在我做了以下事情:

  1. 正确迁移数据库

    class CreateMicrophotos < ActiveRecord::Migration
      def change
        create_table :microphotos do |t|
          t.string :content
          t.integer :user_id
          t.integer :votes_up
          t.integer :votes_down
    
          t.timestamps
        end
        add_index :microphotos, [:user_id, :created_at]
      end
    end
    
  2. 更改缩微照片型号和控制器

  3. 写一些观点

这是我的文件:

用户模型

class User < ActiveRecord::Base
  require "paperclip" 
  attr_accessible :name, :email, :password, :password_confirmation, :avatar,
:avatar_file_name, :avatar_content_type, :avatar_file_size, :avatar_updated_at
  has_secure_password
  has_many :microphotos, dependent: :destroy
  has_attached_file :avatar, :styles => { :large => "120x120>", :medium => "48x48>", :thumb => "26x26>" }
  .
  .
  .
  def feed
    Microphoto
  end
end

显微照片模型

class Microphoto < ActiveRecord::Base
  attr_accessible :content, :votes_down, :votes_up, :photoclip,
  :photoclip_file_name, :photoclip_content_type, :photoclip_file_size, :photoclip_updated_at
  belongs_to :user
  has_attached_file :photoclip, :styles => { :large => "500x400>", :medium => "100x80>" }
  validates :content, presence: true, length: { maximum: 140 }  
  validates :user_id, presence: true
  default_scope order: 'microphotos.created_at DESC'
end

显微照片控制器

class MicrophotosController < ApplicationController
  before_filter :signed_in_user, only: [:create, :destroy]
  def index
  end
  def create
    @microphoto = current_user.microphotos.build(params[:microphoto])
    if @microphoto.save
      flash[:success] = "Your photo has been uploaded successfully!"
      redirect_to root_url
    else
      render 'static_pages/home'
    end
  end
  def destroy
  end
end

我希望发生一件事:

  • 已登录的用户可以从其他用户那里投票给 Microphotos,但不能从他们自己的用户那里投票

    我应该怎么办?

我试图更改 microphotos 控制器更改操作。我知道这可能不是一个真正的问题,但由于我是 Rails 的新手,所以我不能这样做。

[更新 1]

我使用了建议的方法anonymousxxx,但它最终出现了这个路由错误消息:

没有路线匹配 {:controller=>"microphotos", :action=>"vote_up", :id=>#}

使用命令rake routes我有这个:

        users GET    /users(.:format)           users#index
              POST   /users(.:format)           users#create
     new_user GET    /users/new(.:format)       users#new
    edit_user GET    /users/:id/edit(.:format)  users#edit
         user GET    /users/:id(.:format)       users#show
              PUT    /users/:id(.:format)       users#update
              DELETE /users/:id(.:format)       users#destroy
     sessions POST   /sessions(.:format)        sessions#create
  new_session GET    /sessions/new(.:format)    sessions#new
      session DELETE /sessions/:id(.:format)    sessions#destroy
  microphotos POST   /microphotos(.:format)     microphotos#create
   microphoto DELETE /microphotos/:id(.:format) microphotos#destroy
         root        /                          static_pages#home
       signup        /signup(.:format)          users#new
       signin        /signin(.:format)          sessions#new
      signout DELETE /signout(.:format)         sessions#destroy
         help        /help(.:format)            static_pages#help
        about        /about(.:format)           static_pages#about
        terms        /terms(.:format)           static_pages#terms
      vote_up PUT    /vote_up/:id(.:format)     microphotos#vote_up
    vote_down PUT    /vote_down/:id(.:format)   microphotos#vote_down

[更新 2]

在按照他的指示完成后,我得到了这个ArgumentError
can't use collection outside resource(s) scope

config/routes.rb:18:in `block (3 levels) in <top (required)>'
config/routes.rb:17:in `block (2 levels) in <top (required)>'
config/routes.rb:16:in `block in <top (required)>'
config/routes.rb:1:in `<top (required)>'

这是routes.rb

16  resources :microphotos do
17    collection do
18      put '/vote_up/:id' => "microphotos#vote_up", :on => collection, :as => :vote_up
19      put '/vote_down/:id' => "microphotos#vote_down", :on => collection, :as => :vote_down
20    end
21  end
4

3 回答 3

3

您可以使用acts_as_votable gem。

安装宝石:

gem 'acts_as_votable', '~> 0.5.0'
bundle

安装和运行迁移:

rails generate acts_as_votable:migration
bundle exec rake db:migrate

在您的Microphoto模型中,您应该具有:

acts_as_votable

完成此操作后,您可以投票/取消投票microphotos,例如:

@microphoto.liked_by @user1
@microphoto.downvote_from @user2
@microphoto.vote :voter => @user3
@microphoto.vote :voter => @user4, :vote => 'bad'
@microphoto.vote :voter => @user5, :vote => 'like'
于 2013-06-25T10:16:37.603 回答
1

考虑act_as_rateable gem,它可能比 Marek Lipka 为您解释的这个特定用例建议的更适合您。

于 2013-06-25T10:58:50.113 回答
0

方案一(用户可以多票)

您可以在您的视图缩微照片上使用if.. else..声明。

清单 10.46。向微博部分添加删除链接。

微博只能由所有者用户删除。

<% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method: :delete,
                                     data: { confirm: "You sure?" },
                                     title: micropost.content %>
  <% end %>

示例(未经测试,但我希望这会有所帮助):

  # on view (e.g show.html.erb)

  <% if signed_in? && current_user.id != feed_item.user_id %>
    <%= link_to "vote up", vote_up_microphotos_url(feed_item.id), :method => :put %> 
    <%= link_to "vote down", vote_down_microphotos_url(feed_item.id), :method => :put %>
  <% end %>


  # controller
  before_filter :signed_in_user, only: [:create, :destroy, :vote_up, :vote_down]


  def vote_up
    @microphoto = Microphoto.find(params[:id])
    @microphoto.update_attribute(:votes_up, @microphoto.votes_up + 1)
    redirect_to root_path
  end

  def vote_down
    @microphoto = Microphoto.find(params[:id])
    @microphoto.update_attribute(:votes_down, @microphoto.votes_down + 1)
    redirect_to root_path
  end

  # route
  resources :microphotos do
    collection do
     put '/vote_up/:id' => "microphotos#vote_up", :as => :vote_up
     put '/vote_down/:id' => "microphotos#vote_down", :as => :vote_down
    end
  end

解决方案2(用户不能多次投票)

如果您希望用户不能对 microphoto 进行多次投票,您应该制作一个单独的模型 with microphoto modelto vote 并与vote model有关系microphoto model,关系如下:

class User < ActiveRecord::Base
has_many :vote_photos
has_many :microphotos
end

class Microphoto < ActiveRecord::Base
has_many :vote_photos
belongs_to :user
end

class VotePhoto < ActiveRecord::Base
 belongs_to :microphoto
 belongs_to :user
end
于 2013-06-25T13:13:29.483 回答