新手问题 {已解决,感谢 Rei}求教 create.js.erb 取不到 @event 对象的问题

i5ting · 2013年02月26日 · 最后由 i5ting 回复于 2013年02月26日 · 3341 次阅读

controller 里的代码:

def create
  @event = Event.new(params[:event])

  respond_to do |format|
    if @event.save
      # format.html { redirect_to @event, notice: 'Event was successfully created.' }
      # format.json { render json: @event, status: :created, location: @event }

      format.js { render :layout => false } # new add 
    else
      # format.html { render action: "new" }
      # format.json { render json: @event.errors, status: :unprocessable_entity }

      format.js { render :layout => false,:status => 406 } # new add 
    end
  end
end

然后 create.js.erb 里

alert("<%= escape_javascript @event %>")

可以确定 alert 是一定可以执行,不过由于取不到@event,会报 500 错误

取不到这个@event ,求教是何缘故


  • 错误:alert("<%= escape_javascript @event %>")
  • 正确:alert("<%= escape_javascript @event.name %>")

错误日志贴一下。

#1 楼 @Rei 嘻嘻,我确实参考找你的文章了,请看日志

Started POST "/events" for 127.0.0.1 at 2013-02-26 16:02:07 +0800
Processing by EventsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"DQVHVVQqOahNwADC03y59a6s7L2/nPtCR6u/ccGjfnw=", "event"=>{"name"=>"都是"}, "commit"=>"Create Event"}
  Event Load (0.2ms)  SELECT "events".* FROM "events" 
   (0.0ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "events" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", Tue, 26 Feb 2013 08:02:07 UTC +00:00], ["name", "都是"], ["updated_at", Tue, 26 Feb 2013 08:02:07 UTC +00:00]]
   (1.0ms)  commit transaction
  Rendered events/create.js.erb (1.4ms)
Completed 500 Internal Server Error in 6ms

ActionView::Template::Error (Missing partial events/event with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/Users/dudan/work/ruby/testujs/app/views"
):
    1: alert("<%= escape_javascript render(@event) %>")
  app/views/events/create.js.erb:1:in `_app_views_events_create_js_erb___12100624683502615_70174495627380'
  app/controllers/events_controller.rb:51:in `block (2 levels) in create'
  app/controllers/events_controller.rb:46:in `create'


  Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)
  Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (7.4ms)


Started GET "/events" for 127.0.0.1 at 2013-02-26 16:21:04 +0800

Missing partial events/event

@event 已经拿到了,但是没有对应的局部模板所以渲染错误。

#3 楼 @Rei ?我只是想在 js 获得@event的属性,然后 js 我就可以随意操作了,是不是我这种方式用的不对啊?

#3 楼 @Rei 我犯二了,alert("<%= escape_javascript(@event.name) %>"); 又可以了

你的模板里面写了 render(@event) ,所以就去找局部模板了啊。

如果只是要对象属性,操作放在客户端,那么应该用 json 接口。

#6 楼 @Rei 我有试了一下:alert("<%= escape_javascript(@event) %>"); 这样直接使用@event不可以,只能直接取出属性如 alert("<%= escape_javascript(@event.name) %>");

#7 楼 @i5ting 这就相当于 @event.to_s

#8 楼 @Rei 不能直接转成对象,我以前理解错了

Extra tip: Use escape_javascript short hand j :-)

#10 楼 @ashchan great,谢谢啊

需要 登录 后方可回复, 如果你还没有账号请 注册新账号