How do field input methods (text_area, text_field, etc.) get attribute values ​​from an entry in a form_for block?

I have a standard Rails 2.3.5 application with a Post model. Post has a url attribute, and the following getter is defined:

def url p = 'http://' u = self[:url] u.starts_with?(p) ? u : "#{p}#{u}" end 

If I load script/console , I can do Post.first.url and get the desired result (for example, it returns http://foo.com if the true value of the attribute is foo.com)

However, if I have a form_for block and do something like form.text_field :url , it will not return the url with http: // prefixed; rather, it just returns foo.com

How are access form_for attributes on ActiveRecord models? It seems that he walked around all the overloaded getters.

+1
overloading ruby-on-rails activerecord getter attributes
source share
2 answers
 def url_before_type_cast p = 'http://' u = self[:url].capitalize u.starts_with?(p) ? u : "#{p}#{u}" end 
+1
source share

By returning to you mean the multitude?

A text box places data in your controller. (Create or update depending on)

In the controller:

 @post = Post.new(params[:post]) or @post.update_attributes(params[:post]) 

where the field is given.

If you want to implement the installer to add http: //, you must write your function as def url= .

-one
source share

All Articles