Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 用户控制器显示中的命名错误

Ruby on rails 用户控制器显示中的命名错误,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,当我访问未登录到应用程序的用户配置文件页面时,我的应用程序中出现以下错误(我正在使用Desive): 用户控制器显示中的命名错误 nil:NilClass的未定义方法“connections” app/controllers/users\u controller.rb:19:in'show' 当我登录时,错误就会消失。我知道为什么失败了。我只是需要帮助想出正确的解决办法 “我的用户”控制器中的此行出错: def show @connection = current_user.connection

当我访问未登录到应用程序的用户配置文件页面时,我的应用程序中出现以下错误(我正在使用Desive):

用户控制器显示中的命名错误 nil:NilClass的未定义方法“connections”

app/controllers/users\u controller.rb:19:in'show'

当我登录时,错误就会消失。我知道为什么失败了。我只是需要帮助想出正确的解决办法

“我的用户”控制器中的此行出错:

def show
@connection = current_user.connections.build(user_id: current_user, otheruser_id: @user)
end
登录应用程序的用户会出现一个连接表单(简单地说,会出现一个按钮,询问您是否愿意以朋友身份与此人连接)。但是,我正在检查用户是否在表单之前使用
登录到用户视图“show”页面

以下是视图中的相关代码:

 <%= render 'connect_form' if user_signed_in? %>

连接表单

<% unless current_user == @user %>
  <% if @contact.present? && user_signed_in? %>
      <%= @user.first_name %> is your  <%= @contact.description %> (<%= link_to "edit contact", edit_connection_path(:id => @contact.id, :user => @user) %>)<br \>
  <% else %>
    <% if user_signed_in? %>How do you know <%= @user.first_name %>? (<%= link_to "edit contact", new_connection_path(:user => @user) %> )
  <% else %> 
<% end %><br \>
<% end %>
<% end %>

是您的(@contact.id,:user=>@user)%>)
你怎么知道的?(@user)%>)

连接表单(创建新表单)


你怎么知道的?
正确%>
@用户id)%%>

为什么我的应用程序在签入用户时仍尝试加载nil数组`@连接?非常感谢您的帮助

我要做的第一件事是选中当前用户存在时才建立连接

def show
  @connection = current_user.connections.build(user_id: current_user, otheruser_id: @user) if current_user
end
需要注意的主要问题是:如果当前用户在错误消息行的末尾

,则为:


nil:NilClass的未定义方法“connections”

当前用户
为零,需要对此进行快速测试


def秀
@connection=User.new.connections.build(User\u id:current\u User,otheruser\u id:mock\u User\u id)
结束


对于您的情况,可能您没有将登录用户存储在某个位置

您可以将用户控制器的内容也放进去吗您好,谢谢您的回复。带有
@connection
的第一个代码示例是发生错误的地方。我将用户控制器中的show action添加到clarifyGood idea中,但不幸的是,它没有改变任何东西:(.我尝试添加if user_登录?但仍然没有任何改变。还有其他想法吗?没关系!非常感谢!在我添加if current_user之后,发生了什么事,我有一个类似的实例变量,并在该实例变量上触发了错误(错误行号已更改)。我在变量末尾添加了if current_user,所有操作都正常。再次感谢您
def show
  @connection = current_user.connections.build(user_id: current_user, otheruser_id: @user) if current_user
end