2012-06-18 56 views
0

我想,當用戶點擊「喜歡」的帖子鏈接更新帖子表格的觀看次數值........遞增值使用的link_to

模型

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 

層視圖

的意見/後/ show.html.erb

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

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

</table> 

當我點擊「喜歡」之類的鏈接我得到這樣的錯誤-----找不到帖子使用id =投票

* 應用程序跟蹤:* 應用程序/控制器/ post_controller.rb:42:在'秀」

plz幫助我找到錯誤.......

+0

發生了什麼事你縮進? – Ashe

+0

u能說清楚,請........ – Cyber

回答

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

+0

感謝您的答覆...........可以請你告訴我怎麼在我的項目中使用這個.....我是Rails的初學者。 – Cyber

+1

m8,你必須閱讀http://guides.rubyonrails.org/routing.html和其他指南 –