Rails 4 Formatting DateTime field in partial - datetime

I'm storing a datetime field in UTC, but the user can enter it in various time zones. I configure the time zones by setting a cookie via JavaScript. Here is my ApplicationController code:
before_filter :set_timezone
def set_timezone
if !cookies["time_zone"].to_s.blank?
Time.zone = cookies["time_zone"].to_s
end
end
I'm storing the times using nested attributes on another controller's form. Here is the partial that renders to fields via simple_form and simple_fields_for, passing in the form as 'f':
<div class="row fieldset">
<div class="col-sm-3">
<%= f.input :day, collection: days, selected: f.object.day %>
</div>
<div class="col-sm-3">
<%= f.input :open, as: :string %>
</div>
<div class="col-sm-3">
<%= f.input :close, as: :string %>
</div>
<div class="col-sm-3">
<%= f.input :_destroy, as: :hidden %>
<%= link_to "remove", '#', class: "remove_fields" %>
</div>
</div>
The time zone conversion is working correctly. I can enter times in the time zone that JavaScript detects, and then it will store them in UTC. It also displays them in the detected time zone, but prints out the full format:
2014-07-22 05:00:00 -0700
I believe the fields are strings by the time they get to the view, and that's why setting the value of the input field to the following doesn't work:
<%= f.input :close, as: :string, input_html: { value: f.object.close.to_s(:short) } %>
I'm just testing the 'short' formatting option. Figured I can add a custom one later.
Do I need to format the times in the controller for the controller with the nested attributes? Or can I format them in the model that handles storing the times? Let me know if you need more information or code.

I'm not sure if these are the best options, but here are the two I found:
1) Pass an instance variable to the partial with the fields value. Instance variables allowed me to format the datetime field using to_s(:custom_format).
2) I'm using Simple Form, and found that using their time format worked well. For example:
<%= f.input :close, as: :time, ampm: true, minute_step: 15 %>
You can pass through any of the default datetime_select parameters to Simple Form.
The second option adds the meridian option (AM/PM) to the hour field, which was a little strange to me. But it seemed to be the simplest solution of the two, so I went with that for now.

Related

Rails 7 using hotwire to replace a form element

Context: a form has a collection_select, but without a value that interests the user.
A second form allows to create a new entry that would populate the collection_select with a desired value.
Class Article has_many :tags
The create turbostream does render the object, but the form does not display the change & I suspect it is due to the atomicity of the form. the IDed div tag was tried both outside and inside the form_fields tag to the same neutered effect.
<%= form.fields_for :tags do |tag| %>
<div id='tags'>
<%= f.collection_select(:tag, #tags, :id, :name) %>
</div>
<% end %>
The turbo_stream file tris to replace the target. If f.collection_select is used, this generates an error as rails, handling the snippet does not know of the form's existence. Using select_tag, a tag is rendered and delivered but the div is not refreshed
<%= turbo_stream.replace "tags" do %>
<div class='fade-in-div'>
<%= select_tag :tag, options_from_collection_for_select(#tags, :id, :name) %>
</div>
<% end %>
How can these options for select be updated with hotwire?
Functional answer that does not answer the question:
• the new data needs its own field
• make that an allowed attribute with the relevant accessor atrr_accessor
• process the new value
• update the record
• choose preferred path: redirect or turbo_stream a partial as a replacement of entire form.

Stimulus Reflex drop down with selectize.js

I have a search form for an index page following the tabular demo. It works perfectly. The list I have that I want to filter by is quite long and I wanted to make it searchable so I added selectize.js, that piece also works just fine.
The problem is they don't work together. After selecting an item now it doesn't fire off the call to TabularReflex#search to filter the results.
#app/views/foo.index.html
<%= form_with scope: :search, url: '/search', html: { "data-reflex" =>"debounced:change->TabularReflex#search"} do |f| %>
<%= f.select :foo, options_for_select(#search.foo), {include_blank: 'Select a Foo'}, {class: 'selectize'} %>
<% end %>

Using CSS To Style Posts in Ruby on Rails

I'm trying to create a feed of posts for my bloglike application. I'm trying to arrange it so posts are grouped by month and there are 3 posts displayed per line.
This is my code for my feed partial:
<% #posts_by_month.each do |monthname, posts| %>
<%= monthname %>
<% posts.each do |post| %>
<div style="width:100%">
<div style="float:left;width:33%"><%= post.created_at %></div>
</div>
<% end %>
<% end %>
& this is the #posts_by_month in my controller:
#posts_by_month = current_user.feed.group_by { |post| post.created_at.strftime("%B") }
This currently styles it like in this screenshot:
When the code reaches a new month I want the month to be on a new line like the arrow demonstrates, rather than part of the same block like it is currently. Does anyone have any suggestions on how to do this?
You could wrap the month name in:
<div style="clear:left"><%= monthname %></div>
This will make the monthname 'clear' anything that is floated left.
You could make it easier on yourself though by using a grid system or using flexbox like this: https://codepen.io/anon/pen/JNZqaM.
You shouldn't have to use floats for this purpose

Ruby on Rails - Bootstrap / Form_for vs input_groups - How to stop styles butting heads

I'm playing around with some of the input boxes for my rails project.
I'm a bit taken with the first example given here. I like the little '#' notch and could use that for a formatting prompt to our users.
Their code looks like this:
<div class="input-group">
<span class="input-group-addon">#</span>
<input type="text" class="form-control" placeholder="Username">
</div>
If i put that into my rails view it works perfectly and looks great.
But my form setup is running with a form_for function, and I'm having difficutly getting the two to play nicely.
My rails html.erb code looks like this below:
<div class="row">
<div class="col-md-6 col-md-offset-4">
<%= form_for #request do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :user_id%>
<%= f.text_field :user_id%>
<%= f.submit "Begin my request", class: "btn btn-primary" %>
<% end %>
</div>
</div>
I've tried wrapping the two pieces of code together in various ways but to no effect!
General constants is that my rails code always comes out with hard rectangular edges (compared to the bootstrap smooth curves), I cannot get the input-group-addon to sit comfortably on the end of the input field, it variously rides above and below or sits awkwardly over the larger form block etc... so fail on my part. Anyone got any insight?
In terms of gems pertinent to bootstrap I'm using:
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.0.beta1'
The problem you are running into is form_for is using the rails FormBuilder to generate your form and the method text_field is translating to a specific piece of html. You can have it create a different piece of html by creating your own form builder. If you opt to use another gem like simple_form it gets even easier as you can just create additional types of fields. You can look over the documentation for that gem for more information.
class CustomFormBuilder < ActionView::Helpers::FormBuilder
def text_field_with_addon(name, addon, *args)
#template.content_tag(:div,
#template.content_tag(:span, addon, class: 'input-group-addon') +
#template.text_field(name, *args),
class: 'input-group')
end
end
Then use it like:
<%= form_for #request, builder: CustomFormBuilder do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :user_id%>
<%= f.text_field_with_addon :user_id, '#' %>
<%= f.submit "Begin my request", class: "btn btn-primary" %>
<% end %>

Can I assign HTML values in MVC view using Html Helpers?

Is this right? I am trying to display value in input box dynamically?
can anyone advice me is this corect approach? but still I am getting here only + + in input box?
Html.DisplayFor will render a label in this case. If you want to write in this way just use <%= Model.Date.ToString() %> for the value attribute of the input.
These HTML helpers will render the markup for you, don't try and use them as methods to return data. You can get the data by just using <%=Model.MyProperty%> as long as it is a strongy-typed view.
Try just using <%= Html.EditorFor(m => m.Date) %>
OR
<%= Html.TextBoxFor(m => m.Date) %> (the EditorFor will automatically render a textbox anyway)
OR
<%= Html.TextBox("Date", Model.Date) %> (this is not a strongly-typed helper, you're doing the data binding yourself with the second argument)
Maybe do you want this?
<input
type="text"
id="Date-<%=Model.ID%>"
value= " <%= "+" + Model.Date.ToString() + "+" %>" />
I don't know what Model is for you, but something like this might help you, if it is an object of a class that has some property Date.

Resources