简体   繁体   English

在Rails中重命名控制器并清除生成的内容

[英]Renaming controllers in Rails and cleaning out generated content

I was following along with the railscast regarding the restful_authentication plugin. 我跟随有关restful_authentication插件的railscast。

He recommended running the command: 他建议运行命令:

script/generate authenticated user session script /生成经过身份验证的用户会话

Which I did, and everything generated "fine", but then sessions wouldn't work. 我做了什么,一切都生成“很好”,但随后会话无效。 Checking the site again, he mentions a naming standard and listed updated code which stated: 他再次检查网站,提到了一个命名标准并列出了更新的代码,其中说明:

script/generate authenticated user sessions script /生成经过身份验证的用户会话

With sessions being pluralized. 会话是多元化的。

So now I have session_controller.rb with a SessionController in it, but I guess by naming standards, it is looking for SessionsController, causing the code to fail out with the error "NameError in SessionsController#create " 所以现在我有session_controller.rb,其中包含SessionController,但我想通过命名标准,它正在寻找SessionsController,导致代码失败并出现错误“SessionsController中的NameError #create”

I see the problem, which is pretty obvious, but what I don't know is, how do I fix this without regenerating the content? 我看到了问题,这很明显,但我不知道的是,如何在不重新生成内容的情况下修复此问题? Is there a way to reverse the generation process to clear out all changes made by the generation? 有没有办法扭转生成过程以清除一代所做的所有更改?

I tried just renaming the files to sessions_controller with e SessionsController class, but that failed. 我尝试用e SessionsController类将文件重命名为sessions_controller,但是失败了。

While writing this, I solved my own problem. 写这篇文章时,我解决了自己的问题。 I had to rename session to sessions in the routes file as a map.resource and rename the view directory from session to sessions, and update session_path in the html.erb file to sessions_path. 我必须将路径文件中的会话重命名为map.resource,并将视图目录从会话重命名为会话,并将html.erb文件中的session_path更新为sessions_path。

So I solved my problem, but my answer regarding removing generated content still remains. 所以我解决了我的问题,但是关于删除生成内容的答案仍然存在。 Is it possible to ungenerate content? 是否可以取消内容?

Actually, script/destroy works for any generator - generators work by reading a script of sorts on what files to create; 实际上, script/destroy适用于任何生成器 - 生成器通过读取要创建的文件的各种脚本来工作; script/destroy just reads that script in reverse and removes all the files created, as long as you give it the same arguments you passed to script/generate . script/destroy只是反向读取该脚本并删除所有创建的文件,只要你给它传递给script/generate的相同参数。

To sum up: script/destroy authenticated user session would have removed all the generated files for you, after which you could have run script/generate user sessions without a problem. 总结一下: script/destroy authenticated user session会为您删除所有生成的文件,之后您可以毫无问题地运行script/generate user sessions

I've never tried script/destroy , but if you're reverting changes that you just made, the generate command should give you a list of files added and changes made. 我从来没有尝试过script/destroy ,但是如果你要恢复你刚才所做的更改,那么generate命令应该会给你一个添加的文件列表和所做的更改。 If you're using a version control system of some sort, running status/diff might help as well. 如果您正在使用某种版本控制系统,则运行status / diff也可能有所帮助。

You can just roll back to the previous revision in subversion, and start again, right? 您可以在subversion中回滚到先前的修订版,然后重新开始,对吧? right? 对? :-) :-)

rails has script/destroy for 'ungenerating' stuff, but I suspect that will only work for the stuff rails ships with, not the restful authentication plugin. rails有'ungenerating'东西的script/destroy ,但我怀疑这只适用于随附的东西,而不是宁静的身份验证插件。

I'd say your best bet is find-in-files (or grep -R if you're not using an IDE) - find everything that refers to your old SessionController and change it 我想说你最好的选择是find-in-files(如果你没有使用IDE,还是grep -R ) - 查找引用旧SessionController的所有内容并更改它

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

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