سؤال

I am on RailsTutorial by Michael Hartl on chapter 10 and did some extra stuff to add a paperclip image to every micropost. As I added a photo to the Micropost, I call it a Microphoto.
As now I did the following:

  1. Migrating the database correctly

    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. Changing the microphotos model and controller

  3. Writing some view

And this is my files:

User Model

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

Microphoto Model

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

Microphotos Controller

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

I want one thing to happen:

  • Logged-in users can vote to Microphotos from other users, but not their owns

    What should I do?

I tried to change microphotos controller change action. I know this may not be a real question, but as I am new to Rails, I could not do that.

[UPDATE 1]

I used the method suggested by anonymousxxx, but it ended up to this routing error message:

No route matches {:controller=>"microphotos", :action=>"vote_up", :id=>#}

Using command rake routes I have this:

        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

[UPDATE 2]

After doing the instructions by him, I got this 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)>'

this is 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
هل كانت مفيدة؟

المحلول 3

Solution 1 (user can multiple vote)

You could using if.. else.. statement on your view microphoto.

See Listing 10.46. Adding a delete link to the micropost partial.

Micropost can be delete only by owner user.

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

Example (not tested but i hope this help) :

  # 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

Solution 2 (user can't multiple vote)

If you want user can't multiple vote on microphoto, you should make a separate model with microphoto model to vote and vote model have a relationship with the microphoto model, relationship looks like :

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

نصائح أخرى

You could use acts_as_votable gem.

Installing gem:

gem 'acts_as_votable', '~> 0.5.0'
bundle

Installing and running migrations:

rails generate acts_as_votable:migration
bundle exec rake db:migrate

In your Microphoto model, you should have:

acts_as_votable

Having this done, you can vote/unvote microphotos with, for example:

@microphoto.liked_by @user1
@microphoto.downvote_from @user2
@microphoto.vote :voter => @user3
@microphoto.vote :voter => @user4, :vote => 'bad'
@microphoto.vote :voter => @user5, :vote => 'like'

Consider act_as_rateable gem, it may suit you better than the one suggested by Marek Lipka for this specific use case you explain.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top