Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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 路由错误没有路由匹配id nil(Rails)_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 路由错误没有路由匹配id nil(Rails)

Ruby on rails 路由错误没有路由匹配id nil(Rails),ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我在包含贷记/编辑对象链接的页面上遇到以下错误: 路由错误 没有路由匹配{:controller=>“connections”,:action=>“edit”,:id=>nil} 我的应用程序有用户配置文件页面(#在users_controller.rb中显示操作),带有创建或编辑现有“连接”(Facebook或LinkedIn等用户之间的关系)的链接。如果连接不存在,该链接将发送用户以创建新连接。如果连接已经存在,链接将发送用户编辑现有连接。我的创建功能运行良好。但是,一旦创建了连接,访问用户

我在包含贷记/编辑对象链接的页面上遇到以下错误:

路由错误 没有路由匹配{:controller=>“connections”,:action=>“edit”,:id=>nil}

我的应用程序有用户配置文件页面(#在users_controller.rb中显示操作),带有创建或编辑现有“连接”(Facebook或LinkedIn等用户之间的关系)的链接。如果连接不存在,该链接将发送用户以创建新连接。如果连接已经存在,链接将发送用户编辑现有连接。我的创建功能运行良好。但是,一旦创建了连接,访问用户页面就会抛出路由错误。它显然不喜欢我的编辑操作链接

以下是用户页面上的链接:

<% unless current_user == @user %>
<% if @contact.present? %>

<%= @user.first_name %> is your <%= @connection.description %> &nbsp;(<%= link_to "edit contact", { :controller => 'connections', :action => 'edit', :id => @connection.id} %> )

<% else %>

How do you know <%= @user.first_name %>? (<%= link_to "edit contact", { :controller => 'connections', :action => 'create', :user_id => @user.id }, :method => 'post' %> )

<% end %>
routes.rb:

authenticated :user do
    root :to => 'activities#index'
  end
  root :to => "home#index"
  devise_for :users, :controllers => { :registrations => "registrations" }
  resources :users do
    member do
      get :following, :followers, :posts, :comments, :activities, :works, :contributions, :connections
    end
  end
  resources :works do
    resources :comments
  end
  resources :relationships, only: [:create, :destroy]
  resources :posts
  resources :activities
  resources :reposts
  resources :comments do
    member do
      put :toggle_is_contribution
    end
    end
  resources :explore
  resources :connections
有没有关于如何修复链接的想法?谢谢

编辑1:相关rake路由:

 connections GET    /connections(.:format)                         connections#index
                               POST   /connections(.:format)                         connections#create
                new_connection GET    /connections/new(.:format)                     connections#new
               edit_connection GET    /connections/:id/edit(.:format)                connections#edit
                    connection GET    /connections/:id(.:format)                     connections#show
                               PUT    /connections/:id(.:format)                     connections#update
                               DELETE /connections/:id(.:format)                     connections#destroy
您的问题在于:

@connection = current_user.connections.build(:otheruser_id => @user)

它实例化一个连接对象并将其分配给@connection。此对象尚未保存,因此它没有id。从概念上讲,它也是错误的,您如何编辑尚不存在的内容?如果要发送用户以创建新连接,则您需要的是新连接

您是否在控制台中尝试了
rake routes
,以验证路由是否存在?您好,谢谢您的回复。我刚刚在上面发布了rake路线。看起来应该可以了你好,谢谢你的回复。创建操作现在运行良好。我可以使用当前的创建操作(通过上面的链接发送)创建连接。然而,当我试图发送用户编辑一个已经存在的对象(我可以通过查看数据库来确认)时,我得到了路由错误,不客气。也许我解释得不好。您的编辑视图是正确的,不正确的是您的用户#show视图,其中@connection是尚未保存到数据库的连接实例(您只是用
connections.build
实例化)。因此,指向helper的链接会引发错误,因为@connection.id是nilOh ok,这是有意义的。我应该把它改成什么?我将其更改为@connection=current_user.connections.find(params[:connection]),但在访问用户页面时出错:没有ID无法找到连接。我无法使用:ID,因为它用于显示用户。如果我了解您的用户#show action,@connection是新的连接实例,而@contact是现有的连接实例。让我觉得,如果@contact.present?是@contact.id.
@contact=current\u user.connections,那么你在链接中想要的id就是@contact.id.
@contact=current\u user.connections.其中
实际上是一个关系实例,这有效地(简化)了一系列连接,所以你想要的是
@contact.first
 connections GET    /connections(.:format)                         connections#index
                               POST   /connections(.:format)                         connections#create
                new_connection GET    /connections/new(.:format)                     connections#new
               edit_connection GET    /connections/:id/edit(.:format)                connections#edit
                    connection GET    /connections/:id(.:format)                     connections#show
                               PUT    /connections/:id(.:format)                     connections#update
                               DELETE /connections/:id(.:format)                     connections#destroy
@connection = current_user.connections.build(:otheruser_id => @user)