bhanu
bhanu

Reputation: 2460

HTML Form not working in rails

I have a form to create a new Category(new.html.erb) in my rails app, which has GET link in another view(panel.html.erb).

POST is working fine when i directly enter the url in browsers address bar to create a new category, but whenever i try to access the new category form(new.html.erb) from panel.html.erb through a link, the form is rendered but data is not submitting.
I have defined the routes as

get '/catagories/new'

for new category form and

post 'catagories/create'

for creating a new category. I am new to Rails and really don't understand what is wrong with my code

Upvotes: 0

Views: 72

Answers (1)

BroiSatse
BroiSatse

Reputation: 44675

By default rails form submits to /categories rather than to categories/create (Restfull routes). You should have your routes to be defined as a resources:

resources :categories

If you want to override the default (not recommended for a beginner), you can do this in your form_tag:

<%= form_for @category, url: '/categories/create' %>

Note however that this will make it harder to change this url later (as there are now two places to change, routes and form). You should then use named route:

post 'catagories/create', as: :create_category

and then:

<%= form_for @category, url: create_category_path %>

Upvotes: 3

Related Questions