2012-06-18 38 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和其他指南 –