Rails & Haml: cant get a form to work
forms, haml, ruby-on-rails
Solution
1) You want to put the target of the form in the action:
%form{ :action => "/rotas", :method => "post" }
2) You want a submit button, not a link. Try this:
%input{ :type => "submit" } Finish!
Also, I'm not sure why you're putting a `/` after your inputs, that's not needed for anything. I don't think it hurts, but I see no reason to include it.
3) Lastly, the Rails convention is not to use haml elements but rather form helpers, which would look like this:
= form_tag '/rotas' do
= field_set_tag do
= label_tag :title, 'Title:'
= text_field_tag :title
= label_tag :notes, 'Notes:'
= text_field_tag :notes
= submit_tag 'Save Changes'
One reason for this is Rails is going to include a hidden Authenticity Token field in the form for you, and normally Rails controllers won't accept forms that are submitted without this authenticity token value. This is to prevent Cross-Site Request Forgery.
Try this and see what you get.
See the FormTagHelper API for reference.
Problem
I have a form: ``` %form{:action:method => "post"} %fieldset %label{:for => "title"} Title: %input{:name => "title", :type => "text", :value => ""}/ %label{:for => "notes"} Notes: %input{:name => "notes", :type => "text", :value => ""}/ %a.finish{:href => "/rotas", :method => "post"} Finish! ``` However, the link does not seem to want to work - maybe I am missing something basic in Haml, or in Rails. I have a :resource rotas in my routes.rb and my controller has a def create method. Any help is appreciated! Thanks! btw. I generated using scaffold - and it seems that the same form is used for edit a model and for a creation. How does it know whether to do a POST or a PUT?