Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Difference between text_field_tag and text_field


  • Please log in to reply
2 replies to this topic

#1 irakesh710@gmail.com

irakesh710@gmail.com

    Passenger

  • Members
  • 4 posts

Posted 28 November 2013 - 06:34 AM

what is basic difference  between text_field_tag and text_field .



#2 Bharat Soni

Bharat Soni

    Dispatcher

  • Members
  • 54 posts
  • LocationAhmedabad

Posted 28 November 2013 - 06:47 AM

what is basic difference  between text_field_tag and text_field .

 

Both are for use a text input on the form of the view but the key difference is:

 

for use of the text_field the form_tag required means inside the form tag the text_field will work.

 

but many time we required text_field without the form_tag then we can use text_field_tag.


Fun with Ruby on Rails


#3 rayray

rayray

    Signalman

  • Members
  • 15 posts

Posted 29 November 2013 - 02:21 PM

The main difference is that "text_field" requires an existing column in the database and you always have to use it in combination with "form_for". "form_for" is a "model-backed" form.

For example: if you have a users table with a "name" attribute, we could do this:

<%= form_for @user do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

We use "text_field_tag" if we don't need to involve the model or database. An example? Let's say we want to write a form for password reset. The user has to insert his email-address and the app will send a notification to his address. No database involved:

<%= form_tag password_reset_path do %>
  <%= text_field_tag :email %>
  <%= submit_tag %>
<% end %>

And of course, it's always the same pattern for all of these tags:

 

file_field_tag - file_field

email_field_tag - email_field

text_area_tag - text_area


I'm Jack's complete lack of ruby knowledge.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users