Steps to produce this:
prompt>rails test_app
prompt>cd test_app
prompt>script/generate scaffold date_test my_date:datetime
prompt>rake db:migrate
now edit your app/views/date_tests/edit.html.erb:
<h1>Editing date_test</h1>
<% form_for(#date_test) do |f| %>
<%= f.error_messages %>
<p>
RIGHT!<br/>
<%= text_field_tag #date_test, f.object.my_date %>
</p>
<p>
WRONG!<br />
<%= f.text_field :my_date %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', #date_test %> |
<%= link_to 'Back', date_tests_path %>
now edit your config/environment.rb:
#add this
config.time_zone = 'Central Time (US & Canada)'
This recreates the problem I am having in my actual app. The problem with my app is that I'm storing a date in a hidden field and rendering a "user friendly" version. Creating a resource works fine but as soon as I try to edit it the time changes (it adds the difference between my current time zone configuration and UTC). go to http://localhost:3000/date_tests/new and save the time then go to reedit it and you will have two different representations of the date/time one which will save incorrectly and the other that will.
EDIT: One might ask why not just use the one that works. The problem with this is that I am using a nested attribute so i can't exactly do this. I tried doing this:
# from my real app:
<% appt.fields_for :time_slot do |ts| %>
<%=h ts.object.start_at.strftime('%T') %>
<%= hidden_field ts.object.start_at, :start_at %>
<%= hidden_field ts.object.end_at, :end_at %>
<% end %>
but the html results as:
<div>
10:00:00
<input id="2010-05-30_10:00:00_-0500_start_at" name="2010-05-30 10:00:00 -0500[start_at]" type="hidden">
<input id="2010-05-30_10:10:00_-0500_end_at" name="2010-05-30 10:10:00 -0500[end_at]" type="hidden">
<input id="appointment_block_appointments_attributes_0_time_slot_attributes_id" name="appointment_block[appointments_attributes][0][time_slot_attributes][id]" type="hidden" value="95">
</div>
ok it turns out that i need to override the "convention" and have something like:
<%= ts.hidden_field :start_at, :value => ts.object.start_at %>
using :value will give me the timezone difference as well which fixed my problem!
Related
I have restaurants where user can make reservations. For reservations, I set my time attribute as integer. I then used a helper to display time selection(radio button) such as 14 to 2:00pm.
def new_time(h)
if h > 12
(h-12).to_s + ":00 PM"
elsif h == 12
h.to_s + ":00 PM"
else
h.to_s + ":00 AM"
end
end
Reservation time options are displayed on Restaurant page:
<%= form_for([#restaurant, #reservation], :html => {:class => 'reserve-form'}) do |f| %>
<div class="times">
<% #restaurant.hours_open.each do |h| %>
<%= f.radio_button :time, h %>
<%= f.label "time_#{h}", new_time(h), class: "reserve" %>
<% end %>
</div>
<div class="align">
<%= f.label :party_size,'Please enter party size' %>
<%= f.text_field :party_size %>
</div>
<div class="align">
<%= f.submit %>
</div>
Now when reservation is done, I redirect to User Profile to display that reservation. Everything works fine on development side but after deploying to heroku, I get error "We're sorry, but something went wrong." when making a reservation and I think it has something to do with reservation time displayed on user page shown below.
When I check heroku logs this is the error:
ActionView::Template::Error (comparison of ActiveSupport::TimeWithZone with 12 failed):
<% #user.reservations.each do |reservation| %>
<div class="user-reservation align">
<p>Restaurant: <%= reservation.restaurant.name %></p>
<p>Reserved Time: <%= new_time(reservation.time) %></p>
<p>Party Size: <%= reservation.party_size %></p>
<p>Added on: <%= reservation.created_at.strftime("%B %d, %Y") %></p>
app/helpers/application_helper.rb:3:in `>'
app/helpers/application_helper.rb:3:in `new_time'
app/views/users/show.html.erb:19:in `block in _app_views_users_show_html_erb__1649677434053595894_70057403913200'
app/views/users/show.html.erb:16:in `_app_views_users_show_html_erb__1649677434053595894_70057403913200'
You are comparing the attribute reservation.time which is a ActiveSupport::TimeWithZone with an integer.
If your datatype is a time, why don't use just format it with strftime ?
<p>Reserved Time: <%= reservation.time.strftime("%I:%M%p") %></p>
I have a code where a user can select a specific file to be deleted or analyzed.
<% if #files%>
<%= form_tag what_to_do_files_path, method: :get do %>
<%= submit_tag "Delete selected", :name => 'delete' %>
<%= submit_tag "Analyse", :name => 'analyse' %>
<% #files.each do |file| %>
<% if (file.analyzed=="no") %>
<p><td> <%= check_box_tag "files[]", file.id %></td><%= file.name %></p>
<% else %>
<div class="my_profile_info">
<p><td> <%= check_box_tag "files[]", file.id %></td> <%= file.name %></p>
<td class="Info">
Info
</td>
</div>
<% end %>
<%end%>
<%end%>
<%else%>
<%end%>
I need to be able to give a name to every analysis.
For example: user selects 3 files, enters a name in the text field "Analysis of annual profit" and clicks on the button "Analyse".
The name "Analysis of the annual profit" and the names of the files that were selected have to be saved into the table group_analysis.
I have tried something like this after submit_tag "Analyse":
<%= form_for #groupanalysis do |f| %>
<div class="field_label">
<%= f.label :group_name, "Type group name hier" %>
</div>
<br class="clear" />
<br />
<% end %>
but it tells me undefined method model name
Thanks in advance.
I think you may need to take a step back and think of how this form represents the model that you're trying to create or update. Generally speaking the first argument to form_for and form_tag is an object and symbol, respectively, which represent the model that you're working with. The form fields map to each attribute of the object.
According to conventions and/or the :url argument, this will get routed to the appropriate controller and call an action according to the HTTP verb (again, part of many conventions in rails).
Going back to your code examples, you are using the form_tag helper incorrectly and the example using form_for may not be the right implementation. For example you're just displaying a label, with no input nor submit.
I hate to just post a link here and just tell you to read the docs, but in this case I think this is the best first step.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
I am working in rails 2, I have a form containing data of two models, In one model I am validating fields and entering error using self.errors.add_to_base , but this is not displaying error on form and submitting that request successfully.
This is my Model validation\
validate :checkPunchingEntries
def checkPunchingEntries
if self.punch_in_time.blank? && self.punch_out_time.blank?
self.errors.add_to_base("Both 'Punch in' and 'Punch out' time can not be blank")
end
end
This is my Form
<% form_tag '/punching_requests/create', :method => :post, :class=>"punching_request_form" do %>
<% #punching_request.errors.full_messages.each do |msg| %>
<p class="error"><%=msg%></p>
<% end %>
<p>
<label>Date </label>
<%= text_field_tag 'date'%> <%= calendar_for("date") %>
</p>
<p>
<label> </label>
<input type="checkbox" onclick="punch_in_toggle()">Punch In</input>
<input type="checkbox" onclick="punch_out_toggle()">Punch Out</input>
</p>
<div id="punch_in">
<p>
<label>Punch In Time </label>
<%= text_field_tag 'punch_in_time'%>
</p>
</div>
<div id="punch_out">
<p>
<label>Punch Out Time </label>
<%= text_field_tag 'punch_out_time'%>
</p>
</div>
<p>
<label>Assign To</label>
<%= select_tag(:approved_by, options_from_collection_for_select(#human_resource_persons, "id", "login")) %>
</p>
<p>
<label>Reason </label>
<%=text_area_tag 'reason'%>
</p>
<p class="actions"><label></label><%= submit_tag 'Save' %></p>
<% end %>
It comes into validation, but error is not shown on validation fails.
Have you tried the IRB console? If not, take a look what is going on there, just to be sure, that #punching_request.errors contains something.
try this
def checkPunchingEntries
if self.punch_in_time.blank? && self.punch_out_time.blank?
errors[:base]<< "Both 'Punch in' and 'Punch out' time can not be blank"
end
end
and use form_for and #punching_request
for example
# in your controller
def new
#punching_request = PunchingRequest.new
end
def create
...
...
end
your form look like in your new.html.erb
form_for(#punching_request)
...
...
end
In this you're generating the form using form_tag and the errors you're expecting would be stored in the form builder,form_for.
Instead of form_tag use form_for(#punching_request). This should do the trick.
On my site, the user can watch his profile. In his profile he can have a look at his data (i.e. signature).
Now I want my users being able to edit this data while watching it.
So I coded the following in my view:
<div id="profile-signature">
<p>
<b>Signature:</b>
<%=h #user.signature %>
</p>
<%= form_remote_tag(:update => "signature",:url => { :action => :update_signature }) %>
<%= text_area(:signature,:class=>"form-textarea") %>
<%= submit_tag "Save Signature" %>
</div>
in my user controller, I created a new action update_signature
def update_signature
puts 'in function!'
#user = current_user
puts #user.login
puts params[:signature]
#user.signature = params[:signature]
#user.save
puts 'saved'
end
Now, submitting the form, puts params[:signature] will output: classform-textareasfsffsfs
where sfsffsfs is the text I entered.
Reloading and my page and output the signature on page (<%=h #user.signature %>), I get:
"--- !map:HashWithIndifferentAccess classform-textarea: sfsffsfs "
Why do I get this strange string instead of just sfsffsfs (in this case)?
What to do, to update the data (<%=h #user.signature %>) automatic without page reload?
Use text_area_tag for getting the text_area field values . With reloading the page there is a mismatch in the div id it should be signature not profile-signature .
<div id="profile-signature">
<p>
<b>Signature:</b>
<%=h #user.signature %>
</p>
<%= form_remote_tag(:update => "signature",:url => { :action => :update_signature }) %>
<%= text_area(:signature,:class=>"form-textarea") %>
<%= submit_tag "Save Signature" %>
</div>
Make the following changes
<div id="signature">
<p>
<b>Signature:</b>
<%=h #user.signature %>
</p>
<%= form_remote_tag(:update => "signature",:url => { :action => :update_signature }) %>
<%= text_area_tag(:signature,:class=>"form-textarea") %>
<%= submit_tag "Save Signature" %>
</div>
Hope this helps !
It looks like your text_area call isn't quite right, looking at the docs it should be like this:
text_area(object_name, method, options = {})
so your css class is being set as the method, instead you should use text_area_tag:
<%= text_area_tag(:signature, #user.signature, :class=>"form-textarea") %>
Then the correct value (the text in the text area) should be submitted as the params you're expecting.
Ok this is my first app in rails so hopefully this is a simple problem.
Here is my object:
- !ruby/object:ProductImage
attributes:
image_id:
product_id:
created_at:
updated_at:
attributes_cache: {}
This works:
<%= image_form.text_field :product_id %>
But I get undefined method `image_id' for:
<%= image_form.text_field :image_id %>
I just don't get it...
Cheers for any help on this.
This is the actual partial:
<div class="image">
<% new_or_existing = product_image.new_record? ? 'new' : 'existing' %>
<% prefix = "product[#{new_or_existing}_product_image_attributes][]" %>
<% fields_for prefix, product_image do |i| -%>
<div class="input select">
<%= i.text_field :image_id %>
<%= link_to_function "remove", "$(this).up('.image').remove()" %>
</div>
<% end -%>
</div>
P.S a text field is being used just as an example
Can you post the entire form? I have a hunch that maybe you did:
<% form_for :image do |image_form| %>
instead of:
<% form_for :product_image do |product_image_form| %>
Of course, I'd rethink displaying a form where people are manually entering id values in order to join an image to a product, but I understand you're in learning mode right now.
ID fields should not be text_fields, they should be checkboxes or drop-down select boxes. Why would your use know what the Rails ID is?