Formatting Date with Simple Form - ruby-on-rails-3

I'm using the Simple Form gem in my Rails app. In my sign-up form, I'm have a field for birth date. However, the fields are showing in this order: Year/Month/Day.
What's the best way to get the field order Month - Day - Year?
My form looks like this:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.input :created_at %>
<%= f.button :submit, "Sign up" %>
<% end %>

Are you looking for this?
<%= f.input :birthday, :order => [:month, :day, :year] %>

I don't know if it will work with simple_form, but I suspect it will... Add the following to config/locals/en.yml (or similar):
en:
date:
order:
- :month
- :day
- :year

Related

How to save to two tables from one form in Rails

I have a regular form for User information which starts like so:
<%= form_for(#user, :html => { :class => "form-horizontal" }) do |f| %>
<%= f.text_field :last_name %>
...
<%= f.submit "Submit", class: "btn btn-primary", :name => "submit_button" if #user.last_step? %>
<% end %>
I also have some non User fields in the form. Here's an example:
<%= f.label "When is your birthday?" %>
<%= select_tag "month" %>
<%= select_tag "day" %>
<%= select_tag "year" %>
How would I save this to a different table than User?
If you want to update a model other than User, you should probably use accepts_nested_attributes_for and fields_for. The alternative is to just do the work in your create action of your UsersController, ie:
def create
#user = User.create params[:user]
#foo = Foo.find params[:foo_id]
#foo.date = Date.new(params[:foo_year], params[:foo_month], params[:foo_day])
...
end

Rails simple_form grouped collection not working

In my Rails app, I'm using simple_form.
I'm trying to use grouped collection selects.
Without simple_form, this works:
<%= f.label :employee_id, "Lead3:" %>
<%= f.grouped_collection_select :employee_id, Workgroup.order(:id) , :employees, :group, :id, :employee_full_name %>
But, my simple_form attempt doesn't - the dropdown is empty:
<%= f.input :employee, collection: #workgroups, as: :grouped_select, group_method: :employees, :label => 'Lead2:' %>
OR
<%= f.association :employee, collection: #workgroups, as: :grouped_select, group_method: :employees, :label => 'Lead2:' %>
I would suggest to check your #workgroups definition in the controller. It probably returns nil or is not specified. Depending on what action calls your form, you should have something like this:
#workgroups = Workgroup.all(order: id)

Passing Rails Parameters into Form

I'm trying to pass in two variables for a Nested Model form to use, but I'm getting an error. It's probably an easy syntax error that someone experienced could see right away, but I don't see it.
I have a template showing users, if you click one, it should take the user_id and community _id for use in the form. Both community and user are properly declared in the controller.
link to form:
<%= link_to "award badge", award_badge_badges_path, :user_id => user.id, :community_id => #community.id %>
The form uses two models - badge and badge winners. The user_id and community_id are needed for badge_winners which is nested in badges. The error I'm getting is "undefined local variable or method 'user_id' for #<#<Class:0x77544c0>:0x7714230>" so I think that something is wrong with the 2nd and 3rd lines in the form. Here's the form:
<%= form_for(#badge) do |f| %>
<%= f.hidden_field :user_id ,:value => user_id %>
<%= f.hidden_field :community_id ,:value => community_id %>
<%= f.label :Description %>
<%= f.text_area :description %>
<%= f.fields_for :badge_winners do |builder| %>
<%= render "badge_winner", :f => builder, :locals => {:user_id => user_id, :community_id => community_id} %>
<% end %>
<%= f.submit "Give Badge" %>
<% end %>
the show template in the controller:
def award_badge
#badge = Badge.new
badge_winners = #badge.badge_winners.build
end
the badge winner partial
<%= f.hidden_field :user_id ,:value => user_id %>
<%= f.hidden_field :community_id ,:value => community_id %>

In Rails 3, using Formtastic and Devise, generic routes producing errors when adding a new entry for a model

I have a very simple rails 3 program with 2 models: a user model for Devise and a writing model that captures a text field and the user's id.
My routes file is pretty basic:
devise_for :users
resources :users, :writings
root :to => "users#index"
And my form for writings, using Formtastic, is as well:
<% semantic_form_for(#writing, :html => {:method => :put}) do |f| %>
<%= f.input :main %>
<%= f.input :user_id, :collection => current_user, :as => :hidden %>
<%= f.buttons %>
<% end %>
When I try to create a new writing, the form looks great, but then when I hit submit, I get the following error:
No route matches "/writings"
I've run rake routes, and everything else seems to be working on, and I am using the default generate scaffold from rails, so the controller is the out of the box controller.
Any ideas on where I went astray?
Chris, try putting the declaration of the form like this
<% semantic_form_for #writing do |f| %>
<%= f.input :main %>
<%= f.input :user_id, :collection => current_user, :as => :hidden %>
<%= f.buttons %>
<% end %>
I've the idea that when you specify the :html parameter, you "override" some defaults in formtastic. Sorry, I'm not an expert on formtastic. I've used a bit and then decided to go for simple_form :).

how do I get a default date+time entry in my formtastic form

I hope you can help me. Many thanks.
I want to get a default value for both inputs.
<% semantic_form_for #post do |form| %>
<% form.inputs do %>
<%= form.input :date %>
<%= form.input :time, :hint => 'enter time' %>
:start_year or :default doesn't work here.
In your controller you can do:
#post.date = Date.today
#post.time = Time.now