I am trying to convert all my html.erb files into .slim in my new project. I am missing last one inside of my form - a select input.
The form.html.erb looks
<%= form_for #user do |f| %>
<%= f.hidden_field :document_id, value: #doc.id %>
<%= f.label :Obor %>
<%= select(:user, :job, [
["Choose work"],
["Admin"],
["Other"]
], {disabled: "Choose work", selected: "Choose work"}, class: "form") %>
<%= f.button "Submit" %>
<% end%>
Everything works just in the way I want. I tried to make it in a .slim in this way:
= form_for #user do |f|
= f.hidden_field :document_id, value: #doc.id
= f.label :Obor
= select(:user, :job, [
["Choose work"],
["Admin"],
["Other"]
], {disabled: "Choose work", selected: "Choose work"}, class: "form")
= f.button "Submit"
It does not work with an error: Unknown line indicator ->
new.slim, Line xx, Column xx
["Choose work"]
How I can convert the select from html.erb into .slim? Thank you.
EDIT - ANSWER: Thanks to erb_to_slim gem that Ricardo mentioned I figured out the solution. I simply missed the rails indicator on each row of the selector. The answer is:
= form_for #user do |f|
= f.hidden_field :document_id, value: #doc.id
= f.label :Obor
= select(:user, :job, [
= ["Choose work"],
= ["Admin"],
= ["Other"]
= ], {disabled: "Choose work", selected: "Choose work"}, class: "form")
= f.button "Submit"
Do you tryed to use the gem erb_to_slim?
In your terminal type:
gem install erb_to_slim
And inside your project folder, use:
erb_to_slim
All erb files will be converted to slim and the erb files will be renamed.
Maybe this can help.
Related
I'm having issues with a form in my Rails 6 application.
I have a remote form which enters new data into the database via a JSON request, which works but only with one parameter. This is the form:
<%= form_with(method: :post, remote: true, model: Meal.new, url: meals_path, class: "editMealsForm", data: { type: "json" }) do |f| %>
<div class="field">
<%= f.label :description, 'Beschreibung' %><br>
<%= f.text_area :description, rows: "4", placeholder: "Noch keine Mahlzeit für diesen Tag vorhanden, bitte eintragen!", class: "form-control" %>
</div>
<div class="field">
<%= f.label :allergene_ids_add, 'Allergene auswählen', class: "card-text" %><br>
<%= select_tag :allergene_ids_add, options_from_collection_for_select(#allergenes, :id, :name), multiple: true, class: "selectpicker", data: { style: "btn-success", width: "fit", live_search: "true", size: "5", title: "Allergien wählen" } %>
</div>
<% f.hidden_field :day, :value => local_assigns[:mealDay] %>
<% f.hidden_field :tip, :value => "Vollkost" %>
<%= f.submit "Speichern", class: "btn btn-primary mt-2 btn-block" %>
<% end %>
And these are my permitted parameters:
def meal_params
params.require(:meal).permit(:day, :tip, :description, allergene_ids_add: [])
end
And this is my controller action:
def create
#meal = Meal.new(meal_params)
byebug
if #meal.save
if params[:allergene_ids_add].present?
#allergenes_to_add = Allergene.find(params[:allergene_ids_add])
#allergenes_to_add.each do |allergene|
#meal.allergenes << allergene
end
end
respond_to do |format|
format.html { redirect_to meals_path }
format.json { render json: #meal }
end
end
end
The problem is, that if I hit the create action, just the description parameter is permitted, the other ones are just "ignored", so if I fire the submit button I get the following output in the console if I hit my byebug breakpoint:
And if I look at the params:
<ActionController::Parameters {"authenticity_token"=>"derBZeirq0bwr/FWoYRr97qUZ5p66vQc+uT+UMf5xjXXTSFEp+XOepJtGrckguGh+skWXTZ9ibHWfFTt3p80Cg==", "meal"=><ActionController::Parameters {"description"=>"test"} permitted: false>, "commit"=>"Speichern", "controller"=>"meals", "action"=>"create"} permitted: false>
Or just at the meal params:
<ActionController::Parameters {"description"=>"test"} permitted: true>
If I run #meal.valid? it returns true, so I don't see where the issue is.
Also if I check the values for the hidden fields in the form, they are filled and not nil.
So why does this one parameter work, but the rest just isn't permitted even if I got them in my meal_params method?
Okay, I am dumb.
I just forgot the = for the hidden fields.
So instead of:
<% f.hidden_field :day, :value => local_assigns[:mealDay] %>
<% f.hidden_field :tip, :value => "Vollkost" %>
it should be:
<%= f.hidden_field :day, :value => local_assigns[:mealDay] %>
<%= f.hidden_field :tip, :value => "Vollkost" %>
Then everything is working.
I have a form
<%= form_for #user, url: contact_path do |form| %>
<%= form.select(:email, User.all.map(&:email), {}, { class: 'my-form' }) %>
<% end %>
which works well but has placeholder "Undefined" in start position.
I tried to get rid of that with
<%= form.select(:email, User.all.map(&:email), {placeholder: "Select email"}, { class: 'my-form' }) %>
or
<%= form.select(:email, User.all.map(&:email), {prompt: "Select email"}, { class: 'my-form' }) %>
but still same. Any ideas?
<%= form.select :email, options_for_select(User.all.map(&:email)), include_blank: "whatever your prompt says" , class: 'my-form' %>
Experienced same issues today and this is what I did for one of our projects recently.
<%= f.select :category, options_for_select(Category.all.collect { |c| [c.name, c.id] }), { include_blank: 'Select category' }, { class: 'custom-select' } %>
Try changing the values according to your values.
This is how you add class and placeholder in a form.select element in Rails.
I'm attempting to a create a simple form that allows me to run a new search on the existing 'results' page using simple form.
The exact same code works fine when performing the search from another (landing) page. However, when I try to insert it into the results page I'm getting
undefined method 'vtype' for #
<Venue::ActiveRecord_Relation:0x007fd6860eb730>
vtype is both a table column and also the name of the param i'm passing to search on.
The simple_form looks like this:
<%= simple_form_for :results, html: { class: "form-inline justify-content-center" } do |f| %>
<%= f.error_notification %>
<%= f.select :vtype, options_for_select([['Happy Hours Anywhere','Anywhere'],['After Work Drinks','After Work Drinks'],['Somewhere to Relax with Friends', 'Relaxing with Friends'], ['A Club Night','Club Night'], ['Somewhere for Date Night','Date Night'], ['A Place to Watch Sports', 'Watching Sports']]),{} ,:class => "form-control select-box font-lightweight" %>
starting
<%= f.select :date, options_for_select(dates_for_search_select.each_with_index.map{|d, i| [d[1],d[0]]}), {}, :class => "form-control select-box font-lightweight" %>
at
<%= f.select :time, options_for_select(times_for_search_select.each_with_index.map{|d, i| [d[1],d[0]]}), {}, :class => "form-control select-box font-lightweight" %>
</h4>
<%= f.button :submit, 'Discover', :class => 'btn btn-block btn-danger btn-embossed top-margin ' %>
<%end%>
My controller looks like this:
def results
if params.has_key?(:results)
##results = Venue.joins(:offers).where("offers.offertype = '2-4-1'")
#finddate = (params[:results][:date]).to_date
#findtime = (params[:results][:time]).to_time
#resultsdate = DateTime.new(#finddate.year,#finddate.month,#finddate.day,#findtime.hour)
#results = Venue.joins(:offers).where(["venues.vtype = ? and offers.start >= ?", params[:results][:vtype], #resultsdate])
else
#results = Venue.joins(:offers).where(["offers.start = ?", Date.today])
end
end
What's wrong with this form running on the 'results' page?
In Rails, when you have a form like this:
<% form_for #post do |f| %>
<%= f.submit %>
<% end %>
You can have a specific translation like this for when the button is clicked:
en:
helpers:
submit:
create: "Create a {{model}}"
update: "Confirm changes to {{model}}"
But I have a form_tag, which submits a GET to an index path to set a filter:
= form_tag admin_dashboard_index_path, method: :get, class: "table_filter" do
= select_tag :company, options_for_select(#filter_companies.map{ |c| [c.name, c.id] }, params[:company]), include_blank: true, class: "selected"
= submit_tag :submit, value: I18n.t('.general.filter'), class: 'btn'
How can I get a specific translation for this situation?
Add this to the method so that the button label stays the same after pressing it.
= submit_tag :submit, value: t('.general.filter'), class: 'btn', data: { disable_with: t('.general.filter') }
submit_tag method
i have a select tag which i am generating by looping the records and creating options manually. Here is the code:
<%= f.select (:book_id_eq_any) do %>
<%= content_tag(:option, "Choose your option", value: "", disabled: '', selected: '') %>
<% #books.each do |book| %>
<%= content_tag(:option, book.book_title, value: book.id) %>
<% end %>
<% end %>
When i add {multiple: true}, it does not work. Can anyone give me hint about it. Thanks
Just use the built-in select form helper:
<%= f.collection_select :book_id_eq_any, #books, :id, :book_title, { include_blank: "Choose your option" }, { multiple: true } %>