Routing error in activerecord reputation gem in ra

2019-06-13 08:16发布

问题:

Hello I'm new to rails 4 I am working on project where we can post and rate it. I have used activerecord reputation gem. I have been following the http://railscasts.com/episodes/364-active-record-reputation-system video. I am stuck at the routing point. When I click on upvote or downvote it shows me error.

my routes.rb file has

resources :posts do
resources :comments, :only => [:create]
 member { post :vote }
 end
  get "posts/create"
  get "posts/destroy"
  get "posts/new"
  get "posts/index"
  get "posts/show"
   get "posts/edit"
  get "posts/update"


_post.html.erb


 <h4 class="timeline-title"><%= link_to_unless_current post.title, post %></h4>
     <p> Created : <%= post.created_at.strftime("%d/%m/%Y") %> by <%= post.postedby %>
</p>

<% if current_cubestudent.email == post.postedby %>
        <p><%= link_to 'Edit', edit_post_path(@post) %> 
        <%= link_to 'Destroy', posts_path, method: :delete, data: { confirm: 'Are you sure?' } %>



<% end %>
  <%= simple_format post.body , :class => "timeline-body"%>
  <%= pluralize post.reputation_for( :votes).to_i, "vote" %>
 <% if current_cubestudent && !current_cubestudent.voted_for?(post) %>
    <%= link_to "up", vote_post_path(post, type: "up"), method: "post" %>
  <%= link_to "down", vote_post_path(post, type: "down"), method: "post" %>
 <% end %>

my post#show is

<%= stylesheet_link_tag "application",:media =>"screen" %>
 <p id="notice"><%= notice %></p>
<p id="alert"><%= alert %></p>
<%= render :partial => @post %>
<%= link_to 'Back', posts_path %>
<h2 id="timeline">Comments</h2>
    <div>
        <%= render :partial => @post.comments %>
    </div>

    <div class="container">
        <%= form_for [@post, Comment.new] do |f| %>

            <div style="padding-bottom:0">

                <%= f.text_area :body , :placeholder => "Write your comment"%>
             <%= f.submit "Add comment", :class => "btn btn-info" %>
            </div>

        <% end %>
        </div>

posts controller is

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

def create
@post = Post.new(post_params)
@post.postedby = current_cubestudent.email
@post.postedbyid = current_cubestudent.id

respond_to do |format|
  if @post.save
    format.html { redirect_to @post, notice: 'Post was successfully created.' }
    format.json { render action: 'show', status: :created, location: @post }
  else
    format.html { render action: 'new' }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
 end
 end

def destroy
@post.destroy
respond_to do |format|
  format.html { redirect_to posts_url }
  format.json { head :no_content }
end
end

def new
@post = Post.new
end

def index
@posts = Post.find_with_reputation(:votes, :all, order: "votes desc") 
end

def show
@post = Post.find(params[:id])
end

def edit
end

def update
respond_to do |format|
  if @post.update(post_params)
    format.html { redirect_to @post, notice: 'Post was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @post.errors, status: :unprocessable_entity }
  end
 end
 end

def  vote
value = params[:type] == "up" ? 1 : -1
@post = Post.find(params[:id])
 @post.add_or_update_evaluation(:votes, value, current_cubestudent)
 redirect_to :back, notice: "Thank you for voting"
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
  @post = Post.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def post_params
  params.require(:post).permit(:title, :body, :subject)
end
end

after rake routes | grep post

C:\Sites\project>rake routes | grep post post_comments POST /posts/:post_id/comments(.:form comments#create vote_post POST /posts/:id/vote(.:format) posts#vote posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy posts_create GET /posts/create(.:format) posts#create posts_destroy GET /posts/destroy(.:format) posts#destroy posts_new GET /posts/new(.:format) posts#new posts_index GET /posts/index(.:format) posts#index posts_show GET /posts/show(.:format) posts#show posts_edit GET /posts/edit(.:format) posts#edit posts_update GET /posts/update(.:format) posts#update

Can someone help me on it....??

回答1:

Your main problem is in this code

<%= link_to "up", vote_post_path(post, type: "up"), method: "post" %>

you would like to send "post" request but in fact you send default ("get") request. I can suppose that link_to accepts only symbols for method (you pass string). So try this

<%= link_to "up", vote_post_path(post, type: "up"), method: :post %>

Another source of the problem can be wrong routes. If code above does not help you run in console rake routes | grep post and add output to the question.