jrltgronic
jrltgronic

Reputation: 3

Rails Routing Error - very confused

So at the moment I am following a Tutorial for a Photoblog for Ruby on Rails (my version is 5.0.1)

And right now I have a constant routing error

My routes.rblooks as following, generated by Rails

Rails.application.routes.draw do


  get 'posts/new'

  get 'posts/create'

end

When I execute rake routes I get this

      Prefix Verb URI Pattern             Controller#Action
   posts_new GET  /posts/new(.:format)    posts#new
posts_create GET  /posts/create(.:format) posts#create

What makes me curious is, that when I access /posts/create manually, it is no problem at all like it should be. But in /posts/new I fill out a Form which will be redirected and here is where the error occurs

<%= form_tag({:action => :create}, :multipart => true) %> 
<fields to be filled in>
</form>

Anybody a clue?

My Routing Error looks like this:

No route matches [POST] "/posts/create"

Upvotes: 0

Views: 47

Answers (3)

KcUS_unico
KcUS_unico

Reputation: 513

Yes, the above answers are right. The question can be closed. An alternative would be to add this to your routes.rb

resources :posts this will give you all the common rails routes for a scaffold resource.

Upvotes: 0

Ursus
Ursus

Reputation: 30071

A route is identified by an HTTP verb and a path. Your route is

get 'posts/create'
  • HTTP verb: GET
  • Path: posts/create

A form by default has a POST method (the HTTP verb), so you should change your route from

get 'posts/create'

to

post 'posts/create'

Upvotes: 0

Pavan
Pavan

Reputation: 33542

No route matches [POST] "/posts/create"

Your route for create should be of type post

Change

get 'posts/create'

to

post 'posts/create'

Upvotes: 2

Related Questions