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?
Rails is a development tool which gives web developers a framework, providing structure for all the code they write. The Rails framework helps developers to build websites and applications, because it abstracts and simplifies common repetitive tasks.
Rails combines the Ruby programming language with HTML, CSS, and JavaScript to create a web application that runs on a web server. Because it runs on a web server, Rails is considered a server-side, or “back end,” web application development platform (the web browser is the “front end”).
Ruby on Rails, sometimes known as "RoR" or just "Rails," is an open source framework for Web development in Ruby, an object-oriented programming (OOP) language similar to Perl and Python.
Versatile. Ruby on Rails is an extremely versatile framework. You can use RoR for a number of projects with different requirements. eCommerce – Ruby on Rails is perfect for developing eCommerce websites and web apps that are secure and scalable.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With