justewe3
justewe3

Reputation: 73

No route matches [POST] form not working

I have the following in create_admin.html.erbwhich is located in the directory view/create_admin

<div id="page_wrapper">
    <p>Please insert the usernumber of the user that you want to make admin</p>
    <%= form_tag "/controllers/create_admin_controller" do %>
      <%= text_field_tag "account" %> <br/> <br/>
      <%= submit_tag "Make admin" %>
    <% end %>         
</div>

In the create_admin_controller.rb , I have the following:

def update 
end

In the routes.rb I have the following:

 match "/app/views/createAdmin/create_admin.html.erb" => "create_admin#create_admin", :via => :post, :as => :update

Yet I am getting routing error

No route matches [POST] "/controllers/create_admin_controller"

What am I doing wrong ?

Thanks for your time

Upvotes: 1

Views: 165

Answers (1)

jvillian
jvillian

Reputation: 20253

If you run rake routes in our console, you will see that this (in your routes.rb):

match "/app/views/createAdmin/create_admin.html.erb" => "create_admin#create_admin", :via => :post, :as => :update

generates this:

update POST /app/views/createAdmin/create_admin.html.erb(.:format) create_admin#create_admin

The as: :update part generated a name for your route which you can see here:

update POST /app/views/createAdmin/create_admin.html.erb(.:format) create_admin#create_admin
^^^^^^

But then you do this:

<%= form_tag "/controllers/create_admin_controller" do %>
  ...
<% end %>

which is trying to submit to (because that's what you told it to do):

POST "/controllers/create_admin_controller"

And you're getting your error, naturally, because you didn't define that route, did you?

Instead, if you did (in routes.rb):

post 'create_admin/create_admin', to: 'create_admin#create_admin', as: :create_admin

and ran rake routes (in your console), you would see:

create_admin POST   /create_admin/create_admin(.:format)  create_admin#create_admin

Where this part:

create_admin POST   /create_admin/create_admin(.:format)  create_admin#create_admin
^^^^^^^^^^^^

is your named route. So, you could do:

<%= form_tag create_admin_path do %>
  ...
<% end %>

Which would then submit to the create_admin action of your CreateAdminController.

This, by the way:

match "/app/views/createAdmin/create_admin.html.erb" => "create_admin#create_admin", :via => :post, :as => :update
       ^^^^^^^^^^             ^^^^^^^^^^^^^^^^^^^^^

is completely wrong and indicates a fundamental misunderstanding of how routes are defined. The file location of a view has nothing to do with defining routes. And yet, that's what you're matching, the file location of your view. And, you should NOT have a directory called createAdmin. It should be called create_admin. Because you stated that your controller file is create_admin_controller.rb.

Similarly, this:

<%= form_tag "/controllers/create_admin_controller" do %>
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

is also completely wrong and indicates a fundamental misunderstanding of how to form a valid url. If you're going to manually supply the url, then it should be in the form of controller_name/controller_action. Like:

<%= form_tag "create_admin/create_admin" do %>

But, again, why not just use your already-named route? I mean, you went through the trouble of saying 'as: :update' to give it a name. Use the name! Like this:

<%= form_tag update_path do %>

(You append _path to the end of the name shown in rake routes in order to get a valid named path.)

Finally, I don't know why you would name a controller CreateAdminController and then give it an action called create_admin. If, instead, you had a controller called AdminsController and then in your routes.rb did:

resources :admins

Then, if you ran rake routes (in your console) you would see:

    admins GET    /admins(.:format)             admins#index   
           POST   /admins(.:format)             admins#create
 new_admin GET    /admins/new(.:format)         admins#new
edit_admin GET    /admins/:id/edit(.:format)    admins#edit
     admin GET    /admins/:id(.:format)         admins#show
           PATCH  /admins/:id(.:format)         admins#update
           PUT    /admins/:id(.:format)         admins#update
           DELETE /admins/:id(.:format)         admins#destroy

And now you could do:

<%= form_tag admins_path do %>

and that would post to the create action of your AdminsController.

Which would be much more conventional.

Personally, I never use the scaffold generator. But, if you're just starting out, you might consider doing so in order to avoid all the mistakes you made. You should also read the ruby style guide. And buy some books and do some tutorials.

Upvotes: 1

Related Questions