我正在使用if elsif elsif elsif end语句,因为ActionView正在寻找一个create.js.erb文件,因为它是方法的名称,我得到以下错误。我试着指定:action具体但它不起作用。如果我删除elsifs
并仅使用一个if end
语句,则会显示:action 'create_like
'..如下所示:
def create
if params[:liked_id]
@post = Post.find(params[:appreciation][:liked_id])
current_user.like!(@post)
respond_to do |format|
format.html { redirect_to @post }
format.js { render :action => "create_like" }
end
end
end
## ERROR
ActionView::MissingTemplate (Missing template appreciations/create with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", "*/*"], :locale=>[:en, :en]} in view paths "/Users/iHal/Desktop/WORK/GP/app/views", "/Users/iHal/.rvm/gems/ruby-1.9.2-p290@rails3/bundler/gems/rails_admin-0060ad2d78ed/app/views", "/Users/iHal/.rvm/gems/ruby-1.9.2-p290@rails3/gems/client_side_validations-3.1.0/app/views", "/Users/iHal/.rvm/gems/ruby-1.9.2-p290@rails3/gems/devise-1.3.4/app/views"):
class AppreciationsController < ApplicationController
before_filter :authenticate_user!
def create
if params[:liked_id]
@post = Post.find(params[:appreciation][:liked_id])
current_user.like!(@post)
respond_to do |format|
format.html { redirect_to @post }
format.js { render :action => "create_like" }
end
elsif params[:voted_id]
@post = Post.find(params[:appreciation][:voted_id])
current_user.vote!(@post)
respond_to do |format|
format.html { redirect_to @post }
format.js { render :action => "create_vote" }
end
elsif params[:thanked_id]
@post = Post.find(params[:appreciation][:thanked_id])
current_user.thank!(@post)
respond_to do |format|
format.html { redirect_to @post }
format.js {render :action => 'create_thank'}
end
end
end
def destroy
if params[:liked_id]
@post = Appreciation.find(params[:id]).liked
current_user.unlike!(@post)
respond_to do |format|
format.html { redirect_to @post }
format.js { render :action => "destroy_like" }
end
elsif params[:vote_id]
@post = Appreciation.find(params[:id]).voted
current_user.unvote!(@post)
respond_to do |format|
format.html { redirect_to @post }
format.js { render :action => "destroy_vote" }
end
elsif params[:thanked_id]
@post = Appreciation.find(params[:id]).thanked
current_user.unthank!(@post)
respond_to do |format|
format.html { redirect_to @post }
format.js { render :action => "destroy_thank" }
end
end
end
end
Started POST "/appreciations" for 127.0.0.1 at 2011-11-24 23:57:18 -0700
Processing by AppreciationsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"T3PeKRaSJSrESFTOCQ7+4LiM4BKaWkcaQ6cXpSqK38k=", "appreciation"=>{"liked_id"=>"75"}, "commit"=>"Like"}
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Completed in 309ms
ActionView::MissingTemplate (Missing template appreciations/create with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml], :formats=>[:js, "application/ecmascript", "application/x-ecmascript",...
答案 0 :(得分:1)
抱歉,您应在每次return
后致电respond_to
。因为在调用if后,方法进程持续到结束并尝试调用render default。例如:
render 'your_template' and return
或者在你的情况下
respond_to do |format|
format.html { redirect_to @post }
format.js { render :action => "create_like" }
end and return
答案 1 :(得分:0)
如果您仔细检查请求中的参数,请注意您未通过任何params[:liked_id]
,params[:voted_id]
或params[:thanked_id]
:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"T3PeKRaSJSrESFTOCQ7+4LiM4BKaWkcaQ6cXpSqK38k=", "appreciation"=>{"liked_id"=>"75"}, "commit"=>"Like"}
你宁愿通过params[:appreciation][:liked_id]
。看来你应该改变你的条件:
def create
if params[:appreciation][:liked_id]
# ...
elsif params[:appreciation][:voted_id]
# ...
elsif params[:appreciation][:thanked_id]
# ...
end
end