Ruby on Rails model inside namespace can't be found in controller

后端 未结 4 1886
旧时难觅i
旧时难觅i 2021-02-04 11:52

I\'m new to rails and can\'t figure out this issue...

I have a controller

Admin::Blog::EntriesController

defined in app/controllers/adm

相关标签:
4条回答
  • 2021-02-04 12:07

    You can achieve a custom table name by using

    set_table_name('foo')
    

    at the top of your model.

    As for multiple namespaces, you might be able to get away with using

    polymorphic_path(@the_object)
    

    to generate your urls as it does more basic inference (in my experience at least, maybe form_for uses it under the hood).

    0 讨论(0)
  • 2021-02-04 12:20

    It is now 2011 and we are in Rails 3.1 territory, but this issue still arises. I just ran into it with a namespaced controller referencing a non-namespaced model, but only when there were no rows for that model in the database!

    Prefixing the model name with :: fixes the problem.

    0 讨论(0)
  • 2021-02-04 12:29

    Try:

    @blog_entries = ::Blog::Entry.find(:all)
    

    It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.

    0 讨论(0)
  • 2021-02-04 12:33

    Yeah, from looking at the code form_for uses polymorphic_path under the hood.

    0 讨论(0)
提交回复
热议问题