tonic
tonic

Reputation: 103

Acts_as_votable routing problems

I've been using the acts_as_votable gem and it works on things such as pictures, comments and events etc. But I'd like the option to also be able to like another user.

Reading the documentation I can do this by adding acts_as_votable and acts_as_voter to the model.

The problem is that I don't know how to adjust my routes to take this into account as it is configured a bit differently to the others.

In development the profile page is shown at localhost:3000/@user.profile_name

The route is get '/:id', to: 'profiles#show', as: 'profile'.

Using acts_as_votable the route would normally go as something like:

resources :comments do
member do
    put "like", to: "comments#upvote"
    put "dislike", to: "commentss#downvote"
  end
end

But how would I adjust my existing route to work in a similar way?

If I use

resources :profiles, only: :show, as: 'profile' do
    put 'like', on: :member
    put 'dislike', on: :member
end

That would work as localhost:3000/profiles/@user.profile_name/like.

And that doesn't work with the following:

def like
  @user = User.find_by_profile_name(params[:id])
  @user.liked_by current_user
  redirect_to profile_path(@user)
end

def dislike
  @user = User.find_by_profile_name(params[:id])
  @user.unliked_by current_user
  redirect_to profile_path(@user)
end

Or my buttons to like / dislike.

<%= link_to dislike_profile_path(@user), method: :put, class: "btn btn-success btn-xs" do %>
  <i class="fa fa-heart-o"></i> Remove Hype
<% end %>

<%= link_to like_profile_path(@user), method: :put, class: "btn btn-success btn-xs"  do %>
  <i class="fa fa-heart"></i> Hype
<% end %>

Upvotes: 0

Views: 193

Answers (1)

mswiszcz
mswiszcz

Reputation: 1006

resources :profiles, only: :show, path: '/' do
  member { put :like, :dislike }
end

And your links should be working properly

note: by typing in your browser the path it will be done via get, not put, so it will not be working, but I assume you know this :-)

Upvotes: 1

Related Questions