我想,當用戶點擊「喜歡」的帖子鏈接更新帖子表格的觀看次數值........遞增值使用的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幫助我找到錯誤.......
發生了什麼事你縮進? – Ashe
u能說清楚,請........ – Cyber