0

我只是设置了acts_as_votable,它工作正常,但问题是它重定向到帖子的链接,当我添加

redirect_to :back

它刷新页面。

添加了代码

这是路线.rb

Rails.application.routes.draw do
  #root 'pages#index'
  resources :posts

  devise_for :users
  # Define root URL
  root 'pages#index'

  # Define roots for pages
  get '/home' => 'pages#home'

  get '/user/:id' => 'pages#profile'

  get '/explore' => 'pages#explore'

  resources :posts do
    member do
      put "like", to: "posts#upvote"  # maybe change it to POSTS??
      put "dislike", to: "posts#downvote" # maybe change it to POSTS??
    end
    resources :comments
  end
######and post_cotroller.rb


def upvote
    @post = Post.find(params[:id])
    @post.upvote_by current_user
    redirect_to :back
  end

  def downvote
  @post = Post.find(params[:id])
  @post.downvote_by current_user
  redirect_to :back
  end
##########and index

<%= link_to like_post_path(post), method: :put, class: 'upvote' do %>
                    Upvote
                    <%= post.get_upvotes.size %>
                <% end %>

                <%= link_to dislike_post_path(post), method: :put, class: 'downvote' do %>
                    Downvote
                    <%= post.get_downvotes.size %>
                <% end %>
4

0 回答 0