0

当用户单击帖子中的“喜欢”链接时,我想更新帖子表中的视图计数值............

模型

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :answers
  attr_accessible :acceptedanswerid, :body, :userid, :tag, :title, :viewcount, :vote, :anscount
  validates_presence_of :title ,:body ,:tag
  scope :unanswered, where(:anscount => 0)
  scope :byvote, where(:vote=>maximum("vote"))
end

控制器

post_controller.rb

   class PostController < ApplicationController
  def index
   @post=Post.new
   @answer=Answer.new
   @anscomment=Anscomment.new
   @posts=(Post.all).reverse
   @posts1=Post.all(:limit => 5 ,:order => "id desc")
   @unanswered = Post.unanswered
   @byvote=Post.byvote
   #fid=params[:fid]
   session[:flag]=nil
   fid=params[:fid]
   session[:flag]=fid
  end

  #def new
   # @post=Post.new
 # end

  def create
  # @post=Post.new(params[:post])
   @post=Post.new(params[:post])
   respond_to do |format|
   if @post.save
      #if (session[:flag!=nil])
      #session[:flag]=1
      #end
      format.html { redirect_to :controller=>"post" ,:action=>"index" }
     format.json { render json: @post, status: :created, location: @post }
     #redirect_to :controller=>"post" ,:action=>"index"
    else
     #session[:flag]=3
     format.html { redirect_to :controller=>"home" ,:action=>"index" }
     format.json { render json: @post, status: :created, location: @post }
     #redirect_to :controller=>"post" ,:action=>"index"
   end  
  end
  end

  def show
    id=params[:id]
    @post = Post.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

        def vote
             vcount = User.find(params[:id])
             vcount.update_attribute(:viewcount, vcount.viewcount + 4)
          end
end

看法

意见/post/show.html.erb

<table align=center width="60%" bordercolor="black" >

    <tr>
        <td align="center">
            <h2>
                <%=@post.title%> 
            </h2>
        </td>
    </tr>
    <tr>
        <td align="center" width="60">
            <h3><%=@post.body%></h3>
        </td>
    </tr>
    <tr>
        <td align="center">
            This Post comes under:<%=@post.tag%>
        </td>
    </tr>
    <tr >
        <td align="right">
            <%id=@post.userid %>
            <%if id==nil %>
            <%id='15'%>
            <%end%>
            <%@user=User.find(id)%>
            posted by:<%=@user.fullname%> <p>on <%=@post.created_at%></p>
            <%id=nil%>
            <h1 align="left"><%=@post.answers.count%></h1>
            <!--<%Post.increment_counter(:viewcount,@post.id) %>-->
            <%= link_to "like", {:controller => "post", :action => "vote", :id => @post.id } %>
        </td>
    </tr>

</table>

当我单击“喜欢”链接时,我收到一个错误,例如-----找不到带有 id=vote 的帖子

*应用程序跟踪:* app/controllers/post_controller.rb:42:in `show'

请帮我找出错误.......

4

1 回答 1

1

我认为这是因为在您的 routes.rb 文件/post/:id中之前:controller/:action:/:id,所以您必须为此操作创建命名路由。

resources :posts do
  get 'vote', on: member
end

和使用路径助手vote_post_path(post)

http://guides.rubyonrails.org/routing.html

于 2012-06-18T10:49:20.990 回答