I am building a form using Ruby on Rails 4. My model has a dob attribute that is a date in the database. My _form.html.erb has an excerpt like this:
<div class="control-group">
<%= f.label :dob, :class => 'control-label' %>
<div class="controls">
<%= f.date_field :dob %>
</div>
</div>
For whatever reason, the HTML gets rendered as a regular input text box. Is there something being done wrong here?
Try
<%= f.date_select :dob %>
See more here on date fields.
Related
im trying to disyplay earnings of a customer in a hidden field. I just did it with E-Mail of users and it worked well. The code looks like this:
<div class="field">
<%= f.label :email, "E-Mail:" %>
<%= current_user.email %>
<%= f.hidden_field :email, value: current_user.email %>
</div>
Now I´m trying to display "earnings" which is a attribute of a customer. Thats my idea but it does not work.
<div class="field">
<%=f.label :earning, "earning" %>
<%= #customer.(:earning) %>
<%= f.hidden_field :earning %>
</div>
Any ideas?
#customer.earning should work if it is an attribute.
Just curious, you said
earnings was an "attribute" of a customer
Is there a typo? Does #customer.earnings work?
I made a very simple form using bootstrap_form_for:
<%= bootstrap_form_for(#ad) do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body %>
<%= f.text_field :image %>
<%= f.text_field :user_id %>
<%= f.submit %>
<% end %>
However, in the text_area for the 'body' field, this is the html generated:
<div class="form-group">
<label class="control-label" for="ad_body">Body</label>
<textarea class="form-control" name="ad[body]" id="ad_body" style="display: none !important;"></textarea>
</div>
For some reason, it's set to display:none. I searched anywhere in the application and couldn't find a place I set it to display:none.
Any clue?
I'm using Ruby 2.2.3 and Rails 4.2.4.
For anyone facing this issue, I managed to fix it just by changing the column name from body to ad_body. It's just that probably body is a protected term.
I found a custom form style i want to copy. however, it's hard for me to figure out how to integrate the rails into its style since it is much more complicated than examples I've been
able to find.
Specifically, I don't know how to call the specific classes the example uses in my rails app.
I would greatly appreciate some guidance on how to create my rails form in this style:
Basically, you need to do something like:
<%= form_for #listing, html: { class:'form-inline lead-big'} do |f| %>
<div class='form-group'>
</div>
<% end %>
and then in between the .form-group div, you can either use the same code that is provided on the Bootsnipp page, or you can use Rails Form Helpers.
So instead of this:
<div class="form-group">
<label for="name">Hello, I am <span>Your name</span></label>
<input type="text" id="name" placeholder="#maridlcrmn" required>
and
</div>
it could be:
<div class="form-group">
<%= f.label :name do %>
Hello, I am <span>Your name</span>
<% end %>
<%= f.text_field :name, id: "name", placeholder: "#maridlcrmn", required: true %>
and
</div>
See this page for more: http://guides.rubyonrails.org/form_helpers.html
Really cool form I must add!
Depends if you want to do the form for a model or just submitting info. If for mode, use form_for if to submit form_tag
<%= form_tag some_url_path, method: :get do %>
<div class="form-group">
<%= label_tag :name, raw("Hello, I am <span> Your name </span>")%>
<%= text_field_tag :name%>
and
</div>
...
<%end%>
I am following the Head first Rails book for creating an airline ticketing system although I am using version 3.2.13 of rails.
I have a hidden field for flight_id in a partial for adding new seats to a flight. This partial is then rendered on the flights show page. But when I view the outputted html the value for flight_id I get this,
<input type="hidden" value="flight_id" name="seat[flight_id]" id="seat_flight_id">
My code in the partial is
<%= form_for(seat) do |f| %>
<%= f.hidden_field :flight_id, value: :flight_id %>
<div class="field">
<%= f.label :baggage %><br />
<%= f.text_field :baggage %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In my models I have
Flight.rb
has_many :seats
Seats.rb
belongs_to :flight
How do I pass the flight Id from the flight_id to the hidden field?
The flight_id is not available directly.
You can change this line
<%= f.hidden_field :flight_id, value: :flight_id %>
To
<%= f.hidden_field :flight_id, value: seat.flight.id %>
So, I have a form that uses 'date-select', which renders three select boxes per used date-select (year, month and day).
Instead I want to use datepicker, and I've followed the instructions here. However, I don't have a clue how to actually implement the datepicker in the form in the view.
I'm using the following line in application.js...
$('.datepicker').datepicker()
...so I guess I need to give the select boxes the class .datepicker?
<%= form_for #project do |f| %>
<div class="text_field">
<%= f.label :title%>
<%= f.text_field :title%>
</div>
<div class="text_field">
<%= f.label :description%>
<%= f.text_field :description%>
</div>
<div class="dropdown">
<%= f.label :start_date%>
<%= date_select :start_date, :startdate %>
</div>
<div class="dropdown">
<%= f.label :end_date%>
<%= date_select :end_date, :enddate%>
</div>
<div class="select">
<%= f.label :title%>
</div>
<div class="submit">
<%= f.submit "Spara" %>
</div>
<% end %>
Try :
<%= date_select :start_date, :startdate , :class => 'datepicker' %>
EDIT: I have replicated your code on my machine and found the possible mistakes:
This gem selects the date in a text field, not in a date-select . The input should be text_field and it looks like this:
<%= f.text_field :date, :class => 'datepicker' %>
As described in the gem's install guide , you should be sure to require it both in your application.css and application.js . One more important thing : in your projects.js.coffee make sure that the DOM is loaded , like this :
jQuery ->
$('.datepicker').datepicker()