Changing dropdown content in spree form (Rails) - ruby-on-rails

I am working on a project that is built on spree and am trying to concatenate state abbreviations to the state's name in the dropdown when selecting shipping address.
Example: "New York - NY" instead of "New York"
The shipping address form can be found here with the code making the state selection dropdown below:
<% if Spree::Config[:address_requires_state] %>
<p class="form-group" id=<%="#{address_id}state" %>>
<% have_states = !address.country.states.empty? %>
<%= form.label :state do %>
<%= Spree.t(:state) %><abbr class='required' title="required" id=<%="#{address_id}state-required"%>>*</abbr>
<% end %>
<%== state_elements = [
form.collection_select(:state_id, address.country.states,
:id, :name,
{include_blank: true},
{class: have_states ? 'form-control required' : 'form-control hidden',
disabled: !have_states}) +
form.text_field(:state_name,
class: !have_states ? 'form-control required' : 'form-control hidden',
disabled: have_states)
].join.gsub('"', "'").gsub("\n", "")
%>
</p>
<noscript>
<%= form.text_field :state_name, class: 'form-control required' %>
</noscript>
After looking at this post, I thought that :name is the method being called to generate the text in each option, but when I changed it to :abbr (which should be a property that it has) everything stayed exactly the same. I am pretty sure that my changes are running since when I make other changes it works. How would you change the option text and managing that, how would you get both the name and abbreviation and concatenate them together with a hyphen.
Just as a note: I am using Deface::Override to make changes to the code.

Related

Rails 6.0.1 - Trix Editor Parameters not transmitted from form

The added or changed data from a Trix editor field is not submitted/ transmitted.
<%= form_with(model: #comment, local: true) do |form| %>
<div class="form-group">
<h4>Titel</h4>
<%= form.text_field :header, class: "form-control border-top-0 border border-right-0 border-left-0 rounded-0 p-0" %>
<br/>
<h4>Inhalt</h4>
<%= form.trix_editor :comment %>
<%= form.text_field :tonie_id, value: tonie_id, hidden: true %>
<%= form.text_field :user_id, value: current_user.id, hidden: true %>
<%= form.check_box :private %> privater Kommentar
<br/>
<%= form.submit "speichern ",class: 'btn btn-success' %>
</div>
<% end %>
looking into my logs I get the following:
Parameters: {"authenticity_token"=>"xxxx", "tcomment"=>{"header"=>"title", "comment"=>"", "tonie_id"=>"49", "user_id"=>"1", "private"=>"0"}, "commit"=>"speichern "}
Any idea, what could be wrong?
This has been discussed here: https://github.com/rails/rails/issues/37399
You can fix by manually assigning ids to each of the rich text fields. Like this below:
form.rich_text_area :first_description, id: 'trix_first_description'
form.rich_text_area :second_description, id: 'trix_second_description'
form.rich_text_area :third_description, id: 'trix_third_description'
Hope this helps.
This may happen if there are multiple forms of the same model type on your page. I also encountered the same problem. I think it may be caused by duplicate ID.
When multiple rich text editors of the same model type appear on the same page, only the first one can be used normally. After other windows input content, they will be filled into the hidden tags of the first window, so when submitting , The data is null
Sorry i don't know how to express my thoughts in english, hope you understand my speech

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

text_field with default value can not save to db in rails

I want to save the text_field to database with defaut value ,but it's not work.
<p>
<%= f.label :用户id %><br>
<%= f.text_field :user_id ,:value => "#{current_user.try :id}", disabled: true %>
</p>
<p>
<%= f.label :用户昵称 %><br>
<% user = User.find current_user.id%>
<%= f.text_field :name ,:value => user.name , disabled: true%>
</p>
Change disabled: true to readonly: true if you want the field to be un-editable but still submit a value.
"READONLY and DISABLED both remove the functionality of the input field, but to different degrees. READONLY locks the field: the user cannot change the value. DISABLED does the same thing but takes it further: the user cannot use the field in any way, not to highlight the text for copying, not to select the checkbox, not to submit the form. In fact, a disabled field is not even sent if the form is submitted."
Reference:
http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.html
http://www.w3.org/TR/html4/interact/forms.html#h-17.12
Also see this answer: https://stackoverflow.com/a/7730719/2113461

Rails link models in a form DRY

I am trying to link multiple rails models in my app. I am trying to let users create reviews on a product using a form. I am trying to use the rails DRY principle.
I first made a bat table with bat name, model year, and an image. Then I created a manufacturer table which only lists the names of the manufacturers of bats. My bats model belongs_to :manufacturer and my manufacturer model has_many :bats.
Instead of creating multiple tables using manufacturer, (listing the name of the manufacturer at least 3 times for each bat) how can I link my two models together?
My form is submitted to the review model. In the form I already have <%= f.collection_select :bat_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>, which lists all of the possible manufacturers in a drop down menu. HOWEVER, nothing is submitted to the :bat_id parameter in the review form when submitted.
--One guess is to have the manufacturer_id integer stored in the bat model as an integer under the column manufacturer_id(Note: already done this, but I don't know how to submit that in a form?)
--Another guess is to have the bat model inherit from the manufacturer model
Any help is greatly appreciated
My full form:
<%= form_for(#review) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field" align= "center">
<h3>Select bat</h3>
<%= f.collection_select :bat_id, Manufacturer.all, :id, :manufacturer, include_blank: true %>
<h3>What do you like about this bat?</h3>
<%= f.text_area :pros, placeholder: "Enter what you like..." %>
<h3>What do you not like about this bat?</h3>
<%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br>
</div>
<div align="center">
<%= f.submit "Add Review", class: "btn btn-large btn-info" %>
</div>
<% end %>
according to the documentation
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
the params in the collection_select is supposed to be:
f.collection_select(:post, :author_id, Author.all, :id,
:name_with_initial, prompt: true)
where post is the Model,author_id is the attribute
or you could try using:
=f.select( :bat_id, options_from_collection_for_select(#manufacturers,"id","manufacturer",
f.object. bat_id),{})
and put the
#manufacturers = Manufacturer.all
inside the controller

In rails, how to save multiple UI fields to one nested attribute?

When user clicks AddDate in my event planning app, I add a row as seen below so user can add a date and start/end time, and AM/PM.
But in my form using nested attributes, it seems my select controls need a reference to the model field they set (:start_time and :end_time). But the start and end times are create from TWO select controls, one for the hours and one for the minutes. So I'm not sure how the values chosen in the two selects will be combined to form the start and end times.
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :start_time, "Start", class: 'info_inline_control info_label five_margin_right' %>
<%= f.select :start_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(#user_event, :start_time) + 'info_inline_control') %>
<%= f.select :start_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(#user_event, :start_time) + 'info_inline_control') %>
<%= f.select :start_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(#user_event, :start_am_pm) + 'info_inline_control') %>
</div>
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :end_time, "End", class: 'info_inline_control info_label five_margin_right' %>
<%= f.select :end_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(#user_event, :end_time) + 'info_inline_control') %>
<%= f.select :end_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(#user_event, :end_time) + 'info_inline_control') %>
<%= f.select :end_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(#user_event, :end_am_pm) + 'info_inline_control') %>
</div>
Why don't you use a time_select instead?
Doing it this way, you'll need to merge them manually in your controller using params[] values and Date.parse().
You can also try using f.time_select, which should function as a normal field after you submit.
Might find this useful: Where is the Rails method that converts data from `datetime_select` into a DateTime object?
Personally, I write custom interfaces for my date fields using jQuery plugins and then use javascript to merge all the fields into a hidden field that actually gets submitted.

Resources