简体   繁体   English

nil的未定义方法`gsub':NilClass让我在轨道上的红宝石中疯狂

[英]Undefined method `gsub' for nil:NilClass driving me insane in ruby on rails

I don't know what is up, but I was experimenting with GSUB then decided I didn't want to use it anymore so changed my link_to arguments back to: 我不知道到底是什么,但我正在尝试使用GSUB,然后我决定不再使用它,所以将我的link_to参数更改回:

<%= link_to root_url(@user.username), @user.username %>

Now I keep getting this error: undefined method `gsub' for nil:NilClass 现在我不断收到这个错误:nil的未定义方法`gsub':NilClass

and when I remove that whole line from my show file everything is fine. 当我从我的show文件中删除整行时,一切都很好。

or if I change it to this: 或者如果我改成它:

<%= link_to root_url(:user => @user.username), @user.username %>

It's like it has been confused some how and thinks I'm still using the gsub method when I'm not. 就像它已经混淆了一些如何并且认为我仍然在使用gsub方法时,我不是。 I've tested or url helpers such as login, password_reset etc and they all work fine. 我已经测试或url帮助程序,如login,password_reset等,他们都工作正常。 It seems to only be doing this with root_url. 它似乎只是用root_url来做这件事。

I also cloned a repo of my site from yesterday before I got this issue and guess what? 在我遇到这个问题之前,我还从昨天克隆了我的网站回购并猜猜是什么? Same error.. 同样的错误..

I've never been this frustrated with my programming. 我从未对编程感到沮丧。

Is there something I can clear? 有什么我可以清楚的吗? I've tried clearing everything inthe tmp folder still no luck. 我已经尝试清除tmp文件夹中的所有内容仍然没有运气。

routes: 路线:

Mysite::Application.routes.draw do

  resources :users
  resources :sessions
  resources :passwords


  root :to                   => "users#new"
  match 'success'            => "users#success"
  match 'login'              => "sessions#new"
  match 'logout'             => "sessions#destroy"
  match 'reset_password'     => "passwords#new"
  match 'setup_new_password' => "passwords#edit"
  match ':username'          => "users#show"

end

I think you have your arguments for link_to backwards. 我认为你有关于link_to倒退的论点。 It should be: 它应该是:

link_to(label, url)

The gsub method can be triggered by something else deeper inside of Rails. gsub方法可以由Rails内部更深层次的东西触发。 When you get an error, usually you get a stack trace that helps to explain the chain of events leading up to the failure. 当您收到错误时,通常会得到一个堆栈跟踪,有助于解释导致失败的事件链。

这应该可以解决问题,因为root_url(和root_path)没有参数:

<%= link_to @user.username, root_url %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM