StanLe
StanLe

Reputation: 5157

Rails simple form routing issue

I have a form in Rails

<div class="page-header">
    <h3>Create Blah</h3>
</div>
<%= simple_form_for @blah do |f| %>
    <%= f.input :id %>
    <%= f.input :name %>
    <%= f.input :pho %>
    <%= f.input :fun %>
    <%= f.submit :class => 'btn btn-primary' %>
<% end %>
<br>

When I click the submit button, where does the code attempt to go? Does it call the create method for blah_controller.rb? Because currently, I get a routing error

Routing Error
uninitialized constant BlahsController

Here is the BlahController#create method:

 def create
    authorize! :create, :blahs
    @blah = Blah.new(params[:blah])
    if @blah.save
      redirect_to admin_blah_path(@blah), :notice => 'New blah created!'
    else
      render :new
    end
 end

In my rake routes, I have

    admin_blahs GET    /admin/blahs(.:format)                      admin/blahs#index
                POST   /admin/blahs(.:format)                      admin/blahs#create
 new_admin_blah GET    /admin/blahs/new(.:format)                  admin/blahs#new
edit_admin_blah GET    /admin/blahs/:id/edit(.:format)             admin/blahs#edit
     admin_blah GET    /admin/blahs/:id(.:format)                  admin/blahs#show
                PUT    /admin/blahs/:id(.:format)                  admin/blahs#update
                DELETE /admin/blahs/:id(.:format)                  admin/blahs#destroy

Upvotes: 1

Views: 192

Answers (1)

Stuart M
Stuart M

Reputation: 11588

It looks like your BlahsController is a namespaced controller, living under the Admin module (i.e., its fully-qualified name is Admin::BlahsController). If so, when constructing forms you must also provide the :admin namespace, using something like the following:

<%= simple_form_for [:admin, @blah] do |f| %>

See the Rails Guide to Form Helpers, under the "Dealing with Namespaces" section.

Upvotes: 1

Related Questions