Simple form: set current_user.username as value for commenter - ruby-on-rails

I want to set the name of the current user as the value for the commenter.
when i use: <%= f.input :commenter, :input_html => { :value => '<% current_user.username %>' }, label: false, disabled: true %> I get a syntax error.
Actually, I don't want to display the commentor on adding the comment; only on the listing.
Besides Simple_form, I use Devise.

You're openning the ERB tag twice in the same expression, hence the syntax error. Try :
<%= f.input :commenter, :input_html => { :value => current_user.username }, label: false, disabled: true %>

Related

rails + simpleform: selected does not set to be enum's value

I have a enum definition for my User model.
class User < ActiveRecord::Base
enum program_of_study: [
:program_of_study_unknown, :program_of_study_cs, :program_of_study_ceg,
:program_of_study_is, :program_of_study_science,
:program_of_study_engineering, :program_of_study_fass,
:program_of_study_business, :program_of_study_others
]
end
And in simple_form I have:
<%= simple_form_for(locals[:user], wrapper: :horizontal_form, html: {class: 'form-horizontal'},
url: {action: (locals[:is_new] ? 'create' : 'update')}) do |f| %>
<%= f.error_notification %>
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: locals[:user].program_of_study %>
<%= f.button :submit, class: 'btn-success' %>
<% end %>
However, it seems that although the program_of_study of a user is 'program_of_study_science'(by checking in rails console), when I render the form, the shown select element still has 'program_of_study_unknown' as the displayed one. The correct one was not selected.
Instead of the enum I used the keys and it seems to work, have you tried that:
collection: User.program_of_studies.keys
I didn't have to specify a selected option. My code looks like this:
input :status, as: :select, collection: Venue.statuses.keys, include_blank: false
My solution in the end
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: User.program_of_studies[locals[:user].program_of_study] %>

Special attributes in helper input field tag in Rails view

How to add special attributes (like aria-label) into an input field tag function in a Rails view?
I tried these, but they did not work:
<%= radio_button_tag :name, "Name", aria: { label: 'Name' } %>
<%= radio_button_tag :name, "Name", 'aria-label' => 'Name' %>
Thanks in advance. I am using Rails 3.2.22.
Use this:
<%= radio_button_tag :name, "Name", false, aria: { label: 'Name' } %>
You also need to specify the 'checked' parameter to match the method interface.
Also for reference see the Rails API:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag
As suggested by Bryan Oemar, I checked the Rails API (http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-radio_button_tag) and found this:
radio_button_tag(name, value, checked = false, options = {})
I needed to specify the third parameter correctly. Here is the answer:
<%= radio_button_tag :name, "Name", false, 'aria-label' => 'Name' %>
NB: aria: { label: 'Name' } in the tag will not work.

Rails file field doesn't change the name when using multiple

When using multiple for my file field, it displays a name with an array [] appending at the end, but I'm trying to get rid of it. I'm not getting the correct name as I'm supposed to. I'm supposed to remove the [] from the name by hard coding it, the name still gives me business_photos[bizurl][].
<%= form_for BusinessPhoto.new, :html => {:class => "biz_image"} do |f| %>
<%= f.file_field :bizurl, multiple: true, name: "business_photos[bizurl]" %>
<%= f.hidden_field :business_id, :value => #biz.id %>
<%= f.submit %>
<% end %>
Is there something I'm missing?
With file_field it will automatically add the [] array in the name.
If you want to remove it you can use file_field_tag instead of f.file_field
f.file_field :foo, multiple: true, name: 'foo' # results in "name='foo[]'" (f is a form-helper)
file_field_tag :foo, multiple: true, name: 'foo' # results in "name='foo'"
Similar discussion here
You are not passing html option
:multipart => true
with form_for

Rails simple_form label for select

I'm trying to set a label for the following line but I keep getting an error if I add label_method anywhere. label: is just ignored. How can I add a label for f.select?
<%= f.select :state_identifier, Location::STATE, { prompt: 'State', id: 'state' } %>
I tried the following but it doesn't format properly in form-horizontal, leaving no gap between the label and data.
<%= f.label :state_identifier, label: 'State' %>
This is because f.select is not a simple_form method and does not support :label
Something like this should work for you w/ simple form.
<%= f.input :state_identifier, :label => "State", :collection => ["a","b"], :input_html => {:id=>"state" } %>
Hope this helps.

Setting class and value in simple form (Rails)

I am using simple form in my Rails application, i tried to set value and class to a specific input field just like below
<%= f.input :email, :required => true, :autofocus => true, :value => "Email", :class => "myclass" %>
Html output :
but i could not able to see value and class set in the actual html form.
What am i doing wrong?
I'm not using the simple_form plugin for Rails, but as the documentation says you should use the :input_html attribute for this.
Do the following:
<%= f.input :email, required: true, autofocus: true, input_html: { :value => "Email", class: "myclass" } %>

Resources