Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails / Haml: How to create a post form?

I'm trying to make a simple form, but it's working not so fine. This is my current form code:

%form{ :controller => 'tool', :action => 'activation', :method => 'post' }
  %table{ :border => 0, :width => "100%", :height => "100%" }
    %tr{ :align => "center", :valign => "center" }
      %td
        %input{ :type => "text", :name => "accountName" }
        %input{ :type => "submit", :name => "submit", :value => "login" }

I am getting this url when trying to send data via form: 10.0.0.2:3000/activation. I know that I can make route tool#activation to activation, but it's a wrong way, I want to sent post query to 10.0.0.2:3000/tool/activation, but :action => 'tool/activation' also is a bad way as far as I understand.

Can you give me advice ?

like image 626
alterpub Avatar asked Jun 14 '12 16:06

alterpub


People also ask

How does HAML work?

In Haml, we write a tag by using the percent sign and then the name of the tag. This works for %strong , %div , %body , %html ; any tag you want. Then, after the name of the tag is = , which tells Haml to evaluate Ruby code to the right and then print out the return value as the contents of the tag.

What is Form_for in Ruby?

This is the name used to generate the input's name (and the params' names), example: = form_for Admin.new, as: :user do |f| #^^^^ = f.input :username # will generate an input like this: <input type='text' name='user[username]' #... /> #

What is form tag in Ruby on Rails?

The form_tag Rails helper generates a form withe the POST method by default, and it automatically renders the HTML that we were writing by hand before. Note: We can explicitly specify what HTTP verb to use for the form_tag if we want something other than POST .


1 Answers

You should use the rails helper tags.

= form_tag tool_activation_path, :method => :post do
    # The table
        # The row
            # The data
                = text_field_tag "accountName", ""
                = submit_tag "Submit"

See more here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

Also, you should try to avoid unnecessary tables to style your layout. Instead, look to using CSS.

like image 132
MrDanA Avatar answered Oct 20 '22 01:10

MrDanA