【问题标题】:Why am I getting this error from my show.js.erb file?为什么我的 show.js.erb 文件会出现此错误?
【发布时间】:2011-04-08 06:33:14
【问题描述】:

我的个人资料显示视图中有此代码:

<div id='main_profile_column'>
  <div id="video_div">  
    <% @videos.each do |video| %>   
      <%= render 'videos/video', :video => video %>
    <% end %>
  </div>
         <%= paginate @videos %>
</div>

我正在使用 show.js.erb 文件来渲染视频部分以实现 AJAX 分页。我的 show.js.erb 文件中有这个:

$("#video_div").html("<%= escape_javascript(render("videos/video")) %>");

这段代码如何给我这个错误:

GET http://localhost:3000/profiles/45?page=2&_=1302255080024 500 (Internal Server Error)

我应该在 .js.erb 文件中添加什么代码?

这是日志中的错误:

Started GET "/profiles/45?page=2&_=1302255923695" for 127.0.0.1 at Fri Apr 08 02:45:23 -0700 2011
  Processing by ProfilesController#show as JS
  Parameters: {"id"=>"45", "page"=>"2", "_"=>"1302255923695"}
  Profile Load (0.2ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = 45 LIMIT 1
Creating scope :page. Overwriting existing method Profile::Listener.page.
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 57 LIMIT 1
Rendered videos/_video.html.erb (1.3ms)
Rendered profiles/show.js.erb (3.0ms)
Completed   in 172ms

ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
    1: <%= div_for video do %>
    2: <div class="voting_div"> 
    3:   <%= link_to "&uArr;".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => "up_arrow round #{current_user && (vote = current_user.votes_for(video)) && vote.value == 1 ? "voted" : "unvoted" }" %>
    4:   <div id="vote_display" class = "round">
  app/views/videos/_video.html.erb:1:in `_app_views_videos__video_html_erb___358525845_2174681160_1885950'
  app/views/profiles/show.js.erb:1:in `_app_views_profiles_show_js_erb__196650825_2174697160_0'

【问题讨论】:

  • 这是您从 development.log 或浏览器中得到的错误?
  • 在你的 development.log 中查看真正的错误
  • 如果我不得不猜测,我会说video 为空,而div_for 中的代码正在尝试调用model_name 的方法。

标签: jquery ruby-on-rails ruby ajax ruby-on-rails-3


【解决方案1】:

您的控制器/动作中的@videos 变量设置是否正确?

试试这个:

$("#video_div").html("<%= escape_javascript(render(:partial => "videos/video", :collection => @videos)) %>");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2022-01-04
    • 2018-12-06
    • 2020-07-21
    相关资源
    最近更新 更多