Everytime I upload a file, I would like to get the filename of this file in a hidden input instead of playing with params in the controller... is it possible ?
<%= f.input :file, label:false, input_html: {class: "mdl-textfield__input"} %>
<% f.input :filename, input_html: {value: #{file.filename (?)}} %>
Related
I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).
I am trying to remove the label that is auto included when we use '.association' of the simple_form_for. But, regardless of what I do, the title and its <hr> continue to be displayed.
I tried:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: false %>
and
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: "" %>
But it keeps showing :/
What can I do to remove it?
You can try this:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label_method: "" %>
Has anybody found a tip how to translate a simple_form input field to upload files ? I have a pretty simple form like that (in slim):
= simple_form_for(#some_object) do |f|
= f.error_notification
= f.input :text
= f.input :photo, as: :file
= f.button :submit
And the generated HTML always has a button 'Choose a file' with a label on the right 'No file chosen'. Googling gave nothing working, most of them proposing tricks in JS. Any other ideas ?
Apparently, there is no easy way to do this. According to the StackOverflow answer, this is not as Rails issue it's an HTML issue.
The way I solved the issue in Simple Form was by adding a label, and hiding the input field.
<%= simple_form_for(#user) do |f| %>
<%= f.label :photo, "Your label here", class: "btn btn-default" %>
<%= f.input :photo, as: :file, label: false, input_html: { style: "display: none" }%>
<%= f.button :submit %>
<% end %>
When tou click the text in the label it will trigger the upload file, opening the window to select the file.
You can then add extra JavaScript to change the label text by the name of the selected file.
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] %>
I have a hidden field as such:
<%= f.input :authentication, label: 'Authentication Password', as: :hidden %>
I want to show the hidden field when the admin radio button is clicked:
<%= f.input :account_type , as: :radio_buttons, :checked => 'Student', collection: ['Student', 'Admin'], wrapper: :vertical_radio_and_checkboxes %>
How would I do so?
I have tried using
$('#user_authentication').show();
But this does not work.
Remove as: hidden and use
$('#user_authentication').hide(); when DOM is loaded.