I have worked with rails scaffold to generate forms and views automatically.
But now I had to use namespaced controller with model in root namspace.
So, I generated controller manually without scaffold, I’m having trouble working with form.
I suppose that the following code should generate a form for namespaced controller
<%= form_for @menu do |f| %>
<div class="field">
<%= f.label :label %><br />
<%= f.text_field :label %>
</div>
<div class="field">
<%= f.label :order %><br />
<%= f.text_field :order %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
but it doesnt. it says
undefined method `model_name' for NilClass:Class
it seems very easy to do rails with scaffold generators, but if one wants to do manually, rails helper method turns negative on them.
I have searched for form_for documentation and changed the code to
<%= form_for @menu, :url => admin_menu_path do |f| %>
then it shows a different error that
No route matches {:action=>"show", :controller=>"admin/menus"}
my rake routes gives the following output
admin_root /admin(.:format) admin/menus#index
admin_menus GET /admin/menus(.:format) admin/menus#index
POST /admin/menus(.:format) admin/menus#create
new_admin_menu GET /admin/menus/new(.:format) admin/menus#new
edit_admin_menu GET /admin/menus/:id/edit(.:format) admin/menus#edit
admin_menu GET /admin/menus/:id(.:format) admin/menus#show
PUT /admin/menus/:id(.:format) admin/menus#update
DELETE /admin/menus/:id(.:format) admin/menus#destroy
correct me if I’m wrong, but I think ruby cannot find model Menu in Admin namespace, which is obvious. So, I tried with ::Menu.new , I thought it would look up in upper namespace, but no result!
You might be forgetting to instantiate
@menuin your controller.The message “undefined method ‘model_name’ for NilClass:Class” says that
@menuisnil.Since
admin_menu_pathneeds a Menu instance, Rails cannot generate the route correctly whennilis passed.Once you have the instance variable properly set in your controller you can use: