composed name of models in rails

2019-05-28 07:34发布

I'm working with RoR with a legacy database in spanish language. I have the table and registropersona and model RegistroPersona. Now I want to run rails generate scaffold_controller registro_persona But the generated name of controller should be RegistrosPersonasController (Note the final s in Registros and Personas). How can I do this?

2条回答
姐就是有狂的资本
2楼-- · 2019-05-28 08:19

Unfortunately, I don't think there is a way to specify the controller name using scaffold_controller without also affecting the model name. I've looked around and haven't been able to find anything that does what you're looking for.

I believe you have two options:

1 - use rails generate controller RegistrosPersonas. The downside is you end up with a blank controller, and you'd have to fill in all of the REST methods yourself.

2 - use rails generate scaffold_controller RegistrosPersonas. This will create the REST methods for you, however all of the model references will use the pluralized name (ie you'll see RegistrosPersonas.all). So the downside is that you'll need to go through the controller and change each reference from RegistrosPersonas to RegistroPersona. Hopefully you should simply be able to use a simple find + replace in whatever text editor or IDE you're using.

Hope that helps somewhat.

查看更多
Root(大扎)
3楼-- · 2019-05-28 08:23

You can either edit your config/initializers/infletions.rb file to support that, or just rename the controller manually.

查看更多
登录 后发表回答