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

私は1つのことをしたい:

  • ログインしたユーザーは、他のユーザーのマイクロ写真に投票できますが、自分のマイクロ写真には投票できません

    私は何をすべきか?

マイクロフォトコントローラの変更アクションを変更してみました。これは本当の質問ではないかもしれませんが、私は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

1

act_as_rateable gemを検討してください。説明するこの特定のユースケースについて、Marek Lipka が提案したものよりも適している可能性があります。

于 2013-06-25T10:58:50.113 に答える