7

act_as_voire redirection when attempting to vote / vote down

 2 years ago
source link: https://www.codesd.com/item/act-as-voire-redirection-when-attempting-to-vote-vote-down.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

act_as_voire redirection when attempting to vote / vote down

advertisements

I've problem with acts_as_votable gem. I have simple forum app and I want to have feature to vote up and down posts. I used for this acts_as_votable gem. I've added to posts controllers two methods:

def upvote
    @post = post.find(params[:id])
    @post.liked_by current_user
    redirect_to forum_topic_path(@post.topic.forum, @post.topic)
  end

  def downvote
    @post = post.find(params[:id])
    @post.downvote_from current_user
    redirect_to forum_topic_path(@post.topic.forum, @post.topic)
  end

My routes is :

  resources :topics, except: :index do
    resources :posts do
      member do
        put "like", to: "posts#upvote"
        put "dislike", to: "posts#downvote"
      end
    end
  end

And in my topic show action view i have the followings links:

= link_to "Upvote", like_topic_post_path(post.topic.id,post.id, method: :put)
= link_to "Downvote", dislike_topic_post_path(post.topic.id,post.id, method: :put)

When I'm trying to click upvote or downvote i have been redirected to: http://localhost:3000/topics/104/posts/55/like?method=put and i have following error: No route matches [GET] "/topics/104/posts/55/like" What's wrong?


Try "GET" method instead of "PUT" as shown below,

resources :topics, except: :index do
    resources :posts do
      member do
        get "like", to: "posts#upvote"
        get "dislike", to: "posts#downvote"
      end
    end
  end


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK