vendredi 29 avril 2016

Questions on method return values and arguments after do

sorry for this noob question I am just not able to understand ruby/rails grammer,

In rails 2.x, I read the following code,

def localized_input_form_for(record_or_name_or_array, *args, &proc)
    options = args.extract_options!
    args << options.merge(:builder=>LocalizedFormBuilder)
    concat('<div class="inputbox">')
    form_for(record_or_name_or_array, *args, &proc)
    concat('</div>')
end

What does the above function return? Shouldn't it be the return value of the last line statement concat('</div>')?

In the views, I have,

<% localized_input_form_for(@customer) do |f| %>

What is the f in the above code, is it the same f as form_for(xx) do |f|?

The following code works without problem,

<%= f.text_field :name, :required => true, :size => 30,:class =>'ime_on' %>

In rails 4, I made the following modification,

def localized_input_form_for(record_or_name_or_array, *args, &proc)
    options = args.extract_options!
    args << options.merge(:builder=>LocalizedFormBuilder)
    concat('<div class="inputbox">'.html_safe)
    concat(form_for(record_or_name_or_array, *args, &proc))
    concat('</div>'.html_safe)
end

Without adding concat out of form_for, and without adding html_safe, the original code just doesnt work.

Now, everything still works, the

<% localized_input_form_for(@customer) do |f| %>

works without problem, the form is shown exactly as before. So what is the return value of this function now? and what is f above?

The only difference is, the original options in

<%= f.text_field :name, :required => true, :size => 30,:class =>'ime_on' %>

which are, required: true, size: 30, and class:'ime_on' don't show in the final html!

It generates the following,

<input type="text" value="abc" name="customer[name]" id="customer_name">

without size, class etc options. The html generated by rails 2.x do have these options showing up.

I am just confused about the difference. And I also don't understand why the original rails 2.x and rails 4 both worked (the |f| reflects the form generated by form_for, so f.text_field will get the right value from database).

Aucun commentaire:

Enregistrer un commentaire