Asterisk character " * " in simple form Rails 5 [duplicate] - ruby-on-rails

This question already has answers here:
Rails: How to disable asterisk on form's required fields?
(11 answers)
Closed 5 years ago.
i am using the simple form gem and the inputs labels shows an Asterisk " * " before text and i do not need that
something like:
*Year
*Month
*Week
*Quantity
simple form view:
<%= simple_form_for #tech_evaluation do |f| %>
<%= f.input :year, label: "Año", collection: 2017..2020 %>
<%= f.association :project, label: "Proyecto" %>
<%= f.association :countable_account, label: "Cuenta contable" %>
<%= f.association :item %>
<%= f.input :unit_cost, label: "Costo unidario" %>
<%= f.input :unit, label: "Unidad de medida", collection: [ "C/U" ] %>

This is a possible duplicate of How to disable asterisk on form's required fields?
If you want to disable asterisks for that form only, pass defaults: { required: false } into simple_form_for:
<%= simple_form_for(#tech_evaluation, defaults: { required: false }) do |f| %>
# ...
Or you can disable it even for just a single input element:
<%= f.input :year, label: "Año", collection: 2017..2020, required: false %>
Read more at Simple Form: Usage
Lastly, if you want to disable the asterisk mark for ALL simple forms, you can do so by creating the file simple_form.en.yml under config/locales/ with this content:
# config/locales/simple_form.en.yml
en:
simple_form:
required:
text: 'required'
mark: '*'
# This will overwrite required text and mark for all simple forms.
# When using html, text and mark won't be used.
# Comment out the line below to return to default settings.
html: ''
See more configuration options at simple_form.en.yml on Gituhb

Related

Rails simple_form element with data-... attributes for stimulus?

I am trying to hook up a stimulus controller to a form element which is inside a form generated by the simple form gem. Whatever I do - the corresponding "data-controller" attribute doesn't appear in the final rendered page (and hence, the controller won't react)...
Here's the code in question:
<%= simple_form_for book do |f| %>
<%= render 'shared/form_errors', form: f %>
...
<%= f.association :book_format, label: false, 'data-controller': 'book-format-select' %>
...
< % end %>
I also tried
<%= f.association :book_format, label: false, data_controller: 'book-format-select' %>
and
<%= f.association :book_format, label: false, html: {data-controller: 'book-format-select' } %>
None of that works - the output is always the same:
<select class="select required" name="book[book_format_id]" id="book_book_format_id">
Not sure what I am doing wrong?
You need to use input_html key
It is also possible to pass any html attribute straight to the input, by using the :input_html option
<%= f.association :book_format, label: false, input_html: { data: { controller: 'book-format-select' } } %>

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] %>

Rails, Check if url already exists

I have a simple_form that has a bunch of urls that need to be inputted into the database.
Now i'm wanting to have a new record matches existing record page where you can compare the issues?
For instance. If a record has google.com/search/stackoverflow
And if someone types in the same url to be inputted into the same column name. It will alert the user stating that the field already exists and display the ID number of the field that exists. It then wont save the new field and will redirect the user back to the dashboard.
Heres the code i have at the moment.
Controller:
def create
create_params = params[:newevent].permit(:eventname, :eventshortdesc, :eventvenuename, :eventdesc, :eventdatetime, :eventimage, :1link, :2link, :3link, :4link, :5link, :6link, :event_type, :eventready, :eventcomplete)
#newevent = Newevent.new(create_params)
#newevent.save!
end
view
<%= f.input :eventname, label: "Event Name", required: true %>
<%= f.input :event_type %>
<%= f.input :eventdesc, label: "Description", required: true %>
<%= f.input :eventshortdesc, label: "Short Description", required: true %>
<%= f.input :eventvenuename, label: "Venue Name / Location", required: true %>
<%= f.input :eventdatetime, type: "datetime", label: "Date Of Event: (Enter as YYYY-MM-DD)", required: true %>
<%= f.input :eventimage, :label => "Image Name Here (exact)"%>
<%= f.input :1link, label: "1 URL of Event" %>
<%= f.input :2link, label: "2 URL of Event" %>
<%= f.input :3link, label: "3 URL of Event" %>
<%= f.input :4link, label: "4 URL of Event" %>
<%= f.input :5link, label: "5 URL of Event" %>
<%= f.input :6link, label: "6 URL of Event" %>
This is all i have at the moment.
Thanks for helping if you can.
Sam
You just need a simple uniqueness validation.
class NewEvent < ActiveRecord::Base
validate_uniqueness_of :1link, :2link, :3link # whatever attribute you want to validate
end

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.

undefined method `name' for ["Yes", true]:Array

I acctual don't know where Array ["Yes",true] came from. I have two models like this:
GeneralExam has_many topic_questions
TopicQuestion belongs to general_exam, belongs_to topic
In TopicQuestion, I have columns: general_exam_id, topic_id, number_question, to store a number question for each topic in general exam.
I built a nested form for create new general exam, on form I built a dynamic select, when user choose a course from a dropdown list, I have other dropdown lists to display topics of course chosen by user.
This is my javascript code for dynamic select:
<% content_for :head do %>
<script type="text/javascript">
$(document).ready(function() {
$('#general_exam_course_id').change(function () {
$.ajax({
type: "POST",
url: "<%= update_topics_general_exams_path %>",
data: { course_id: $('#general_exam_course_id').val() },
dataType: "script"
});
});
</script>
<% end %>
#general_exam_course_id is ID of dropdown list for select course. update_topics_general_exams_path is route to my update action in general exam controller, this is my update action:
def update_topics
#course = Course.find(params[:course_id])
#topics = #course.topics
end
I have a update_topics.js.erb also:
$('div#topic_questions select').html("<%= j options_from_collection_for_select(#topics, 'id', 'name') %>");
Dropdown lists in div#topic_questions will get #topics value from update_topics action (I think so) and display it. When I create new general exam, it's ok. But when I press Edit link, it show me an error which I don't know why and where it comes:
NoMethodError in General_exams#edit
undefined method `name' for ["Yes", true]:Array
Extracted source (around line #3):
1: <div class="row">
2: <div class="span6"><%= remove_child_link "Remove below topic", f %></div><br><br>
3: <div class="span3"><%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %></div>
4: <%= f.input :number_question, placeholder: 'Number of questions', label: false, style: 'display: inline' %>
5: </div>
My edit action only have: #general_exam = GeneralExam.find(params[:id]). I don't know why topic association field of general exam display an Array ["Yes", true]. I don't have it, don't define it any where, why it come when I edit general exam?
Update
If I remove label_method: :name, value_method: :id in:
<%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
The page is not error, but Dropdown lists have value Yes, No in select, not the name of topic I have created for exam. Form can get the number question of each topic, but it can not get name of topic.
When I create:
When I edit:
I found this in config/locales/simple_form.en.yml:
en:
simple_form:
"yes": 'Yes'
"no": 'No'
Is this a problem?
Update: Don't pass #topics in new action but it's still have on new page When course even is not selected yet
My new action:
def new
#general_exam = GeneralExam.new
5.times { #general_exam.topic_questions.build }
##topics = Topic.all
end
As in my extract source above, I have below code in new, edit too (use same form):
<%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
So I don't pass #topics, but when I go to new page, dropdown lists for topic still display Yes, No value.
Ok , our time for comments chat-like is over :)(the System suggested to move to the chat room ) . The solution it seems to be passing the collection inline in the association of the simple_form :
<%= f.association :topic, collection: #topics, label_method: :name, value_method: :id, prompt: "Choose a topic", label: false %>
becomes :
<%= f.association :topic, collection: Topic.limit(1), prompt: "Choose a topic", label: false %>
It would work for now , I think .
You've got the right idea by assigning your query to #topics and passing it to your view... but, you're putting it in the wrong actions and not passing them to the right views:
def update_topics
#course = Course.find(params[:course_id])
#topics = #course.topics
end
-
What you should do:
There is NO update view for your update action, so, you must place the following 2 lines:
#course = Course.find(params[:course_id])
#topics = #course.topics
in both your new action and your edit action
Then you can go ahead and use your original simple_form code, which IS/WAS ORIGINALLY correct:
<%= f.association :topic, collection: #topics,
label_method: :name, value_method: :id,
prompt: "Choose a topic", label: false
%>
Alternatively, you could SKIP the #topics in your controllers and simply put the following in your views:
new view
<%= f.association :topic, collection: Topics.all,
label_method: :name, value_method: :id,
prompt: "Choose a topic", label: false
%>
edit view
<%= f.association :topic, collection: Course.find(params[:course_id]).topics,
label_method: :name, value_method: :id,
prompt: "Choose a topic", label: false
%>
NOTE the difference:
I would think that in your new view you would want the user to see ALL possible topics.
While in your edit view you just want them to see the topics for this course, correct?
-
Why it happened:
Since, you were not passing ANY collection earlier, there was no 'name' method/column for simple_form to use as the labels/display values in the dropdown and thus the error message you got regarding Array ["Yes",true]
If/when you removed the label_method: :name, value_method: :id, options you probably would have seen as many choices as you had expected rows in the dropdown, but instead of a list of the course topic names you would have seen a list of Topic objects (something like this):
#<Topic::xxxx>
#<Topic::xxxx>
...
#<Topic::xxxx>
And, when you don't supply the collection, you get only 2 yes/no values which is the simple_form default per the config file you found (config/locales/simple_form.en.yml).
So, as you might have guessed by now, this is why what you finally used...
<%= f.association :topic, collection:
Topic.limit(1),
prompt: "Choose a topic",
label: false
%>
...kind of "worked" as a workaround -- because it *did* execute a proper query and supplied it to the simple_form, but it limited the results to only the *first* topic in the table (without any ordering/sort)
At least, this is what I have learned/gone through. HTH!

Resources