Rails text_field object_name with arguments - ruby-on-rails

How to pass arguments through text_field form helper.
Eg.:
<%= f.text_field :title(#language), class: 'form-control', placeholder: 'Item Title' %>
Result:
syntax error, unexpected '(',

You can try this code:
<%= f.text_field :title, class: 'form-control', placeholder: 'Item Title' %>

If you are trying to pass data to make your text_field display it in the placeholder, you can:
<%= f.text_field :title, class: 'form-control', placeholder: #language.your_class_method %>
Note here that the first parameter of the form helper is the attribute of the model you want to fill.
If you want to display the placeholder in a different language, you need to use as I mentioned, and make sure your_class_method properly transform the placeholder in the language needed.
If you are trying to pass additional information through the form to the controller, you can use an additional hidden field like:
<%= f.hidden_field :title, #language.your_class_method %>
Hope this helps!
If not, provide more information about "passing arguments through text_field form helper" because I'm not sure if I got that right.

Related

Rails pass predefined id to text_field_tag

In my Rails 5 app I want to have already filed text_field_tag by current_login.user.full_name. Additionally I want to pass current_login.user.id inside this text_field as params[:physician_id]. What I did is pretty simple:
<%= text_field_tag "physician_id", current_login.user.id, class: 'form-control', value: current_login.user.full_name, disabled: true %>
With this code I've got:
pry> params['physician_id']
=> nil
If I add <%= text_field_tag "physician_id", id: current_login.user.id (...) I've got:
pry> params['physician_id']
=> {:id=>70, :class=>\"form-control\", :value=>\"Gary KZM JohnsonR\", :disabled=>true}
How to pass this current_login.user.id as a params['physician_id'] inside text_field_tag? should I use something else?
For reference, the method signature is
text_field_tag(name, value = nil, options = {})
You cannot have both current_login.user.id and value: specified, they both map to value attribute of the input. Also your input is disabled so it is not being submitted with the form.
<%= text_field_tag "physician_id", current_login.user.id, class: "form-control",
value: current_login.user.full_name, disabled: true %>
Either you're looking for a select_field_tag or have separate physician_name input and a physician_id as hidden input
<%= text_field_tag "physician_name", current_login.user.full_name, class: "form-control" %>
<%= hidden_field_tag "physician_id", current_login.user.id, class: "form-control" %>
This will submit as params
{"physician_id"=>"1", "physician_name"=>"name"}
However I suggest you don't do that, as this is not a secure way to deal with current_user attributes. I could submit any id as physician_id and probably get access to records that don't belong to me. You should assign these attributes inside of your controller instead.

Rails text_field_tag values are not getting properly

Hi in Rails how to display text_field values. In my index file I am trying display my values in text_field_tag but here is i am getting values in this format
considering bellow code
1) {:value=>2}
2) {:value=>0.3e2}
But I just want to display in text_field_tag values as
considering bellow code
1) 2
2) 300
How should i reformat it?
is there any other text_field(I don't want to use it in form field this is just index file for display values I don't want to submit it)
<div class="col-xs-4 col-sm-3"><%= text_field_tag :amount, value: 2 %></div>
//or
<div class="col-xs-4 col-sm-3"><%= text_field_tag :amount, value: expense.amount %></div>
Thanks a lot for your valuable answer :)
As mentioned here, text_field_tag accepts the second argument as the value to field. So, pass the value directly (instead of passing it as a hash):
<%= text_field_tag :amount, 2 %>
Because you are getting hash value as {:value=>2}
also you can convert it to integer
expense[:value].to_i
2.3.4 :003 > 0.3e2.to_i
=> 30
<%= text_field_tag :amount, expense[:value].to_i, class: "your_class", placeholder: 'some placeholder' %>
In case if you want static value
<%= text_field_tag :amount, 2, class: "your_class", placeholder: 'some placeholder' %>
In case if you want to put it in form but not to be submitted make is disable
<%= text_field_tag :amount, expense[:value].to_i, disabled: true, class: "your_class", placeholder: 'some placeholder' %>
Note: disabled field are not to be subjected to submit with form data, however :readonly => true will be wrapped with form datas
in other case just put this field outside the form

Model-independent Rails 4 file upload in dynamically loaded (nested) form field

I'm looking for a way for simple image upload onto the file system so that the image is not stored in the database. I understand this step is rather easy, just using a file_input tag, however I have problems making it happen because of how my form is built.
I have a project where I create static campaign pages by having a user specify a page layout, or a template, for the page. Once the user selects which template they want to load for their page, a form is loaded that contains a section for each of the widgets that the specified template contains. The user inputs their information, presses submit, and a static page is created.
One of the widgets is an image widget, where the user should select a file from their system and that should get uploaded to the server on submitting the form. The problem is that because of how I'm using the form helpers, I'm facing the issue, where file upload gives filename as a string instead of the IO object (https://www.ruby-forum.com/topic/125637). I need help on trying to work around this.
The form for inputting data onto the page (which is stored in the table campaign_pages) looks like this:
= form_for(#campaign_page, class: 'form-horizontal') do |f|
fieldset
legend Basic page information
.form-group
= f.label :featured, class: 'control-label col-lg-2'
.col-lg-10
= f.check_box :featured, class: 'form-control'
= f.label :title, class: 'control-label col-lg-2'
.col-lg-10
= f.text_field :title, placeholder: 'Unique and really catchy page title!', class: 'form-control'
= f.label :campaign_id, 'Campaign: ', class: 'control-label col-lg-2'
.col-lg-10
= f.select :campaign_id, options_from_collection_for_select(#campaigns, 'id', 'campaign_name', #campaign), {}, html_options= {:class => 'form-control'}
= f.label :language_id, 'Language: ', class: 'control-label col-lg-2'
.col-lg-10
= f.select :language_id, options_from_collection_for_select(Language.all,'id', 'language_name'), {}, html_options= {:class => 'form-control'}
label for='template_id' class='control-label col-lg-2' Template:
.col-lg-10
= select_tag 'template_id', options_from_collection_for_select(#templates, 'id', 'template_name', #template), class: 'form-control'
| data-no-turbolink="true"
This is where forms for content for each widget on the page gets loaded once a template has been selected:
#widget_location
= render :file => 'templates/show_form', layout: false
.form-group
= f.submit 'Save Page', class: 'btn btn-sm btn-primary'
The form for the image widget (which creates the problem) looks like so:
#image_widget_form.form-group
= label_tag 'widgets[image][image_url]', 'Image URL:', class: 'control-label col-lg-2'
.col-lg-10
= text_field_tag 'widgets[image][image_url]', options['image_url'], class: 'form-control'
= hidden_field_tag 'widgets[image][widget_type]', WidgetType.find_by(:widget_name => 'image').id
= file_field_tag 'widgets[image][image_upload]'
After reading the documentation (http://guides.rubyonrails.org/form_helpers.html#uploading-files) and other issue reports, my understanding is that the problem is "multipart => true", which you have to specify if you want your file field tag to work. That shouldn't be a problem, because I'm using form_for #campaign_page instead of form_tag. However, the widgets and their content do not belong to the campaign page table, and so they aren't specified in the model for a campaign page. Thus, I can't simply use f.file_field for the file input.
I'm looking for a solution for this. Would using nested attributes and then using field_for for the image upload work?
Another issue is that specifications and content for the widgets are stored in a Postgres JSONB field, and I'm not sure how nested attributes would work for those.
I assumed that the form had multipart set, because specifying multipart => true for a form_for helper is not necessary according to Rails documentation. The fix turned out to be pretty simple, however - it works after setting multipart: true in a html options hash for the form tag:
= form_for(#campaign_page, class: 'form-horizontal', html: {multipart: true}) do |f|
...

Rails: Simple Form Custom Label Not Working

I'd like to create a custom label for a simple form field. For some reason, the below code isn't creating that label. It's still using the default label. I must be missing something easy.
Simple Form 3.1
<%= simple_form_for "#" do |f| %>
<%= f.input :street, label: "Custom Label" %>
...
<% end %>
How can I create a custom label for my inputs in Simple Form?
You need to use a label helper along with your input helper:
<%= simple_form_for "#" do |f| %>
<%= f.label :street, 'Custom label' %>
<%= f.input :street, as: :string, label: false %>
<% end %>
You can also directly specify input types ie. f.text_field - More info : http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html
You can now pass a label as argument. So the syntax as shown in the question (from 2015) now works:
<%= f.input :street, label: "Custom Label" %>
See the "Usage" section of the readme.

Ruby on Rails: How do I add placeholder text to a f.text_field?

How can I add placeholder text to my f.text_field fields so that the text comes pre-written by default, and when a user click inside the fields, the text goes away - allowing the user to type in the new text?
With rails >= 3.0, you can simply use the placeholder option.
f.text_field :attr, placeholder: "placeholder text"
In Rails 4(Using HAML):
=f.text_field :first_name, class: 'form-control', autofocus: true, placeholder: 'First Name'
For those using Rails(4.2) Internationalization (I18n):
Set the placeholder attribute to true:
f.text_field :attr, placeholder: true
and in your local file (ie. en.yml):
en:
helpers:
placeholder:
model_name:
attr: "some placeholder text"
I tried the solutions above and it looks like on rails 5.* the second agument by default is the value of the input form, what worked for me was:
text_field_tag :attr, "", placeholder: "placeholder text"
Here is a much cleaner syntax if using rails 4+
<%= f.text_field :attr, placeholder: "placeholder text" %>
So rails 4+ can now use this syntax instead of the hash syntax
In your view template, set a default value:
f.text_field :password, :value => "password"
In your Javascript (assuming jquery here):
$(document).ready(function() {
//add a handler to remove the text
});
This way works to me.
<%= form_for #product do |f| %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Drill hammer" %>
<% end %>
As you can see, to implement a placeholder, you just can add the "placeholder: "text here", after your text_field name.
Hope my answer can be understood!

Resources