rails console error require './example_user'

大兔子大兔子 提交于 2019-12-24 04:18:40

问题


I'm using the railstutorial at http://ruby.railstutorial.org/chapters/rails-flavored-ruby#sec:a_user_class

I've been told to type require './example_user' but... I get this error

LoadError: cannot load such file -- example_user
from /Users/snook3/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in require'

and then further and further errors

I imagine that I'm not linking correctly to that folder, can anyone let me know where I should be linking too?


回答1:


Yes, just move the file to the actual root directoy. Doing mv example_user.rb* / should do it.

Also, don't worry if after requiring the file, you get a return of "true" instead of ["User"].




回答2:


You can use Rails.root to get your current Rails project root.

So require Rails.root + 'path_to_example_user'

would load the file.

You can also try the reload! command, which will reload the whole Rails environment.




回答3:


The file should end with .rb:

    require './example_user'    #will not work
    require  './example_user.rb'      #will work

To rename the file, use the following command line (not in the rails console):

    mv example_user example_user.rb



回答4:


try using absolute path for the file... something like this:

require '/Users/snook3/my/project/folder/example_user.rb'



回答5:


No need to specify the full path, simply append .rb to the filename: require './example_user.rb'.




回答6:


I had this exact same problem, but not being very familiar with Unix I can only explain it like this.

1) I created the example_user.rb file in Sublime Text 2 and saved it to the application root folder.

2) The "require" wasn't working for me, just like the OP. Even though I could see the file there in the folder.

3) However, opening up a Terminal window, navigating to the application root, and entering "dir", I could then see that the filename had a "/ " before it!? Not sure how/why that happened, or why it wasn't visible in Explorer (or whatever the Unix equivalent of that is called--Nautilus?).

4) After renaming the file from within Terminal, it all worked. But would love an explanation of what went wrong if this makes sense to any of you Unix/Rails folks.




回答7:


Try

touch example_user.rb 

in Unix terminal. And then add code to this file.




回答8:


The tutorial tells you to "create the file example_user.rb in your application root directory". I mistakenly initially put it in the app folder and got the same error message.

If you instead move the file into your project root directory, then require './example_user' will work.



来源:https://stackoverflow.com/questions/10616722/rails-console-error-require-example-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!