Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails & Haml: cant get a form to work

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?

like image 551
Karan Avatar asked May 01 '12 20:05

Karan


People also ask

What is the use of Rails?

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.

What language does Rails use?

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”).

What is the meaning of Ruby on Rails?

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.

Is Rails a good framework?

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 Answers

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.

like image 137
Andrew Avatar answered Sep 24 '22 03:09

Andrew