Rails 3:无法弄清楚这个Actionview Template错误,需要帮助吗?

时间:2011-11-25 20:40:57

标签: ruby-on-rails ruby-on-rails-3 model-view-controller

尝试使用ajax响应进行POST时,我收到了一个ActionviewTemplate错误。 'create'方法一直在寻找默认的create.js.erb文件,但我特意指出了它应该呈现的文件。

我尝试将“结束并返回”添加到我的respond_to块,但没有任何反应。

Started POST "/appreciations" for 127.0.0.1 at 2011-11-25 13:30:09 -0700
  Processing by AppreciationsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"T3PeKRaSJSrESFTOCQ7+4LiM4BKaWkcaQ6cXpSqK38k=", "appreciation"=>{"liked_id"=>"75"}, "commit"=>"Like"}
  User Load (1.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Completed   in 343ms

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"

Appreciatins控制器

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

2 个答案:

答案 0 :(得分:1)

您的params支票不正确; liked_id嵌套在:appreciation哈希。

答案 1 :(得分:0)

liked_id不在最高级别的参数中,它嵌套在params[:appreciation]中。您可以在find中的正确位置查找,但在if; elsif逻辑中,您正在寻找params[:liked_id]

那里不存在,所以没有任何条件在运行。该操作结束时没有任何关于渲染内容的指令,因此默认发生。