How can I format the value shown in a Rails edit field?
The best I have come up with so far is something like this:
<%= f.text_field :my_attribute, :value => number_with_precision(f.object.my_attribute) %>
Or my_attribute
could return the formatted value, like this:
def my_attribute
ApplicationController.helpers.number_with_precision(read_attribute(:my_attribute))
end
But you still have to use :value
<%= f.text_field :my_attribute, :value => f.object.my_attribute %>
This seems like a lot of work.
Rails change date format in form field attribute value
I guess you can specify the input value explicitely, but it not the best solution i think, since you need to do this for every date field
= f.date_field :valid_from, class: 'form-control', data: { provide: 'datepicker' }, value: f.object.valid_from.to_s
Rails formatting in edit field
I just had a 'duh' moment. To doesn't seem there's any easy, rails-ey way. But it can be done really easy with unobtrusive JavaScript, like:
$('.datepicker').each(function(i){
var date = new Date($(this).val());
$(this).val($.datepicker.formatDate('dd-M-yy', date));
});
Four lines in application.js
and you're done.
number format in rails form
<%= f.text_field :amount, :class => 'text_field', :value => f.object.amount.to_i %>
how to Edit a post in rails and display said post in the text_field
<h1>Edit message</h1>
<%= form_with(model: @post, local: true) do |form| %>
<p>
<%= form.label :message %><br>
<%= form.text_field :message, :value => Post.find(params[:id]).message %>
</p>
<p>
<%= form.submit %>
</p>
<%end%>
<%= link_to 'Back', posts_path %>
Rails edit adding fields issue
What does store_params
look like in your controller? If id
isn't one of the permitted values, then you can start to see the nested models created as new records each time the update
action occurs. You would want to have something like:
params.require(:store).permit(products_attributes: [:id, :name, :_destroy])
See the documentation on strong parameters for the nested_form
gem.
Best in Place Editing not working with Rails 4. Can see field but can't edit
I was using outdated versions of jquery,
My current Versions :
Using on_the_spot 1.0.5
Using jquery-turbolinks 2.1.0
Using jquery-ui-rails 6.0.1
Using rails 4.2.7.1
I changed the jquery links in my application.html.erb to include
<% javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" %>
<% javascript_include_tag "http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"
integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw="
crossorigin="anonymous"%>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<%= stylesheet_link_tag :on_the_spot %>
I'm not sure if the jquery links are optimal or not but it appears that everything Jquery related in the app is working OK. Updating the jquery libraries not only fixed best_in_place but also the same issue with on_the_spot.
Related Topics
How to Switch to Ruby 1.9.3 Installed Using Homebrew
Which Style of Ruby String Quoting Do You Favour
Why Use "Self" to Access Activerecord/Rails Model Properties
How to Read Lines of a File in Ruby
How to Check a Checkbox in Capybara
How to Use "Gets" and "Gets.Chomp" in Ruby
Convert Unicode Codepoint to String Character in Ruby
How to Test a Function With Gets.Chomp in It
How to Prompt for a Sudo Password Using Ruby
Rails Console: Reload! Not Reflecting Changes in Model Files? What Could Be Possible Reason
Uploading Multiple Files With Paperclip
How to Install Therubyracer Gem on 10.10 Yosemite
Undefined Method 'Visit' When Using Rspec and Capybara in Rails
Undefined Method Attr_Accessible