0

I'm a frontend + PHP dev, trying to fix [] in a project built in Rails.

[] = Fetch color, show a slightly darker color.

This row:

<%= f.text_field attribute %>

creates an input field with a value that can be translated into a color. I'm at loss as to where to look for how it adds that value. I'm trying to use the value that this input field generates.

this is code from the file select_a_color_input.html.erb inside the app/views/shared folder. Any ideas on where to continue my treasure hunt? :)

update: I found this!

def app_text_field(attribute, args = {})
    render_field 'text_field', field_locals(attribute, args)
end

Does that help? ^__^

update: The form builder

class AppFormBuilder < ActionView::Helpers::FormBuilder
  def form_fields(partial = nil , options = {})
    partial ||= 'form'
    fields = ''
    unless options.delete(:without_error_messages)
      fields << @template.render('shared/error_messages', :target => Array(@object).last)
    end
    fields << @template.render(partial, options.merge(:f => self))
  end


  def app_text_field(attribute, args = {})
    render_field 'text_field', field_locals(attribute, args)
  end

  def app_file_field(attribute, args = {})
    render_field 'file_field', field_locals(attribute, args)
  end

  private

  def render_field(name, locals)
    @template.render field_path(name), locals
  end

  def field_locals(attribute, args = {})
    help_options = args[:help_options] || {}
    field_options = args[:field_options] || {}
    html_options = args[:html_options] || {}
    { :f => self, :attribute => attribute, :help_options => help_options, :field_options => field_options, :html_options => html_options, :object => object }
  end

  def field_path(value)
    "shared/app_form/#{value}"
  end
end

update: When I tried to add

<%= content_tag(:p, attribute) %>

It does not give me the values, but instead the id/name of the item, not the colour.

Alisso
  • 1,861
  • 1
  • 17
  • 32
  • Do you want to use the value client-side (e.g., JavaScript) or server-side? – zeantsoi Jan 17 '14 at 22:12
  • My plan is to add a div in front of this input field with the extracted colour as a background value, and then the converted only slightly darker value as a border when the page is generated. There are many different colour inputs and so I'd like to generate a colour preview on page load. – Alisso Jan 17 '14 at 22:14
  • If the user changes the colour it'll update the preview with js. – Alisso Jan 17 '14 at 22:15
  • Please post the entire form code, beginning with the opening `form_for` tag and ending with the `end` tag. – zeantsoi Jan 17 '14 at 22:20
  • You can also run this site in a web browser, use View Source, copy out the HTML which that `f.text_field` generated, and paste it into the source, commenting out the field with `<%#= f.text_field attribute %>`. Then put in code that you're familiar with. Cracking `f.text_field` will be easy if you then read a few tutorials on all of Rails so it soaks in... – Phlip Jan 17 '14 at 22:28
  • Philip I'm into cracking f.text_field. – Alisso Jan 17 '14 at 22:30
  • Also the colour is generated depending on each user's setting, so the colour is different for each user, hardcoding a colour for them would mess this up. :) – Alisso Jan 17 '14 at 22:31
  • Your specific `f.text_field` line at the beginning of your question doesn't appear to be using anything special from the posted `FormBuilder`, and that helper method for `app_text_field` appears to be equally unrelated. – nzifnab Jan 17 '14 at 23:20
  • @nzifnab ok! *having a look at your answer* – Alisso Jan 18 '14 at 11:21

1 Answers1

1
<%= f.text_field attribute %>

This by itself is not very useful to help us gather context. What's the surrounding markup look like? attribute is a ruby variable in this instance. If it were f.text_field :attribute, then :attribute is now a symbol instead of a variable and this would indicate that it maps to the attribute method on X model. This all depends on what your form_for looks like though. I'll give an example:

<%= form_for @user do |f| %>
  <%= f.text_field :attribute %>

In this case, we have a form for the User model, and our text_field maps to @user.attribute. The field itself looks something like this:

<input type='text' name='user[attribute]'>

And in the controller's #update or #create action (depending on if this is a new record or an existing record you're editing) the value would be accessible in this fashion:

params[:user][:attribute]

However, it's impossible to say what exactly the params will look like in your particular case. What action is being run? What's the name of the file that this is being loaded from? "app/views/users/new" would indicate the #new action handles this page, and the #create action will handle the form submission.

Things we need to know to fully solve your problem:

  • Name and relevant code of the controller that's handling this action.

  • Full view path that this is being rendered from

  • The rest of the markup starting at form_for and ending at this field attribute

  • What value does attribute hold? It's a variable, so it must be holding a symbol value or something that will indicate which field is being mapped to this input.

nzifnab
  • 15,876
  • 3
  • 50
  • 65