Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 你有很多贯穿和部分_Ruby On Rails_Partials - Fatal编程技术网

Ruby on rails 你有很多贯穿和部分

Ruby on rails 你有很多贯穿和部分,ruby-on-rails,partials,Ruby On Rails,Partials,我有一个用户模型、一个帖子模型和一个兴趣模型 控制器的应用程序如下: class ApplicationController < ActionController::Base before_filter :login_from_cookie before_filter :find_user_interests helper :all # include all helpers, all the time session :session_key => '_blah

我有一个用户模型、一个帖子模型和一个兴趣模型



控制器的应用程序如下:

class ApplicationController < ActionController::Base
  before_filter :login_from_cookie
  before_filter :find_user_interests
  helper :all # include all helpers, all the time
  session :session_key => '_blah_session'

  include AuthenticatedSystem   

  def find_user_interests
    @user_interests = current_user ? current_user.interests : []  
    true
  end

end
<%= render :partial => "users/interests", :object => @user_interests %>
ul
  <% unless current_user.nil? then -%>  
    <% @user_interests.each do |interest| -%>
      li<%= interest.post.title %>/li
    <% end %>
  <% end -%>   
/ul    
class ApplicationController“\u blah\u会话”
包括认证系统
def查找用户兴趣
@用户兴趣=当前用户?当前用户兴趣:[]
真的
结束
结束

Application.html.erb具有以下功能:

class ApplicationController < ActionController::Base
  before_filter :login_from_cookie
  before_filter :find_user_interests
  helper :all # include all helpers, all the time
  session :session_key => '_blah_session'

  include AuthenticatedSystem   

  def find_user_interests
    @user_interests = current_user ? current_user.interests : []  
    true
  end

end
<%= render :partial => "users/interests", :object => @user_interests %>
ul
  <% unless current_user.nil? then -%>  
    <% @user_interests.each do |interest| -%>
      li<%= interest.post.title %>/li
    <% end %>
  <% end -%>   
/ul    
“用户/兴趣”:object=>@user\u interests%>
_interests.html.erb部分内容如下:

class ApplicationController < ActionController::Base
  before_filter :login_from_cookie
  before_filter :find_user_interests
  helper :all # include all helpers, all the time
  session :session_key => '_blah_session'

  include AuthenticatedSystem   

  def find_user_interests
    @user_interests = current_user ? current_user.interests : []  
    true
  end

end
<%= render :partial => "users/interests", :object => @user_interests %>
ul
  <% unless current_user.nil? then -%>  
    <% @user_interests.each do |interest| -%>
      li<%= interest.post.title %>/li
    <% end %>
  <% end -%>   
/ul    
ul
李/李
/保险商实验室

考虑到所有这些,当我在localhost:3000/posts/1时,我的部分显示良好,但在localhost:3000/posts中,我得到一个错误
nil:NilClass的未定义方法“title”
,因此在上面的_interests.html.erb部分中显示的
li/li
行中有一个错误

到底是什么问题


TIA

这只意味着其中一方的利益在另一端没有相关的帖子。很可能是被删除了。这本可以通过以下措施防止:

class Post < ActiveRecord::Base
  has_many :interests, :dependent => :destroy
end
class Post:破坏
结束
同时,您应该清理数据库中的孤立项

编辑:您声称这已经存在于您的模型中,但如果确实存在,则不清楚您如何能够拥有孤立的兴趣,如错误所示。也许它是在添加从属子句之前创建的?再次,通过SQL删除孤立项,然后重试。如果问题在以后再次出现,您必须在某处删除而不回调

关于你的尺寸问题。您可能正在使用
当前用户.interests.count
。这是由于Rails关联的一些魔力
count
是运行SQL的Rails关联上的一种特殊方法
length
只是一个数组方法,告诉您数组中有多少项。Rails关联有一些特殊的方法,但其余的方法是透明地传递给数组对象的

进一步评论:当您传递
:object=>@user\u interests
时,您正在设置一个名为partial的变量。因此,您可以引用局部变量
interests
。但是,您引用的是@user\u interests,因此不需要传递对象。在所有其他条件相同的情况下,传递对象并使用局部变量可能更好(它更显式,更像函数式编程风格),但在这种情况下,您没有使用它


最后,就样式而言,我可能是错的,因为我没有完整的上下文,但通常我会在模板中设置logged_in条件,而不是在没有登录用户的情况下将user_interest设置为空数组。这将允许您在模板中引用当前_user.interests.count,并独立设置要在@user_interests中显示的兴趣(例如分页)

我通过Posts模型via has_many:interest,:dependent=>:destroy涵盖了这一点。另外,奇怪的是,如果我去掉部分中的代码,该代码会破坏标题,从而绕过这个错误。我在部分中有一个标题,设置为“You have@user_interests.length interests”,当我在帖子页面上说帖子页面有10篇帖子时,我的标题读起来像“You have 10 interests”,因此即使用户没有选择该帖子作为兴趣,也会将所有帖子作为兴趣。当我转到一个特定的帖子页面时,因此,显示操作,在该页面上,上面引用的标题为“你有1个兴趣”,因此它似乎在计算特定页面(索引或显示)上的帖子数量,并将这些帖子合计为用户兴趣。哦,对不起,我颠倒了从属子句。如果你真的有你所说的,那么不清楚这是怎么发生的。编辑更多评论。