I am trying to build a dropdown select input of months using simple_form. However, I am having trouble figuring out where to even begin. Currently, it is a text input area:
<%= f.input :start_month %>
I need to know what arguments to pass in order for this to be a dropdown of all 12 months. It is not important for it to return an integer value for the months but would be ideal in the event I use it for ordering later on.
I am still a beginner with rails and could really use the help on this one. I can provide any extra information necessary.
Edit:
I would like the dropdown to show the month names, not simply numbers.
Here is my solution:
<%= f.input :start_month, collection: (1..12).map{|i| [I18n.t("date.month_names")[i], i]} %>
What about this:
<%= f.input :start_month, :collection => 1..12 %>
or with month names:
<%= f.input :start_month, :collection => ['January','February',...,'December'] %>
<%= f.select :month, Date::MONTHNAMES.compact.each_with_index.collect{|m,i| [m,i]}, prompt: 'Month'
Related
I have a user, who wants to be able to take vacation days off from work. My view looks like this:
<h2>Request Days Off</h2>
<%= form_for(#user, :as => :user, :url => vacation_days_path) do |f| %>
<div><%= f.label "How many vacation days would you like to take?" %>
<%= f.number_field :vacation_days %></div>
<div><%= f.submit "Submit" %></div>
<% end %>
In my controller, I have new and create methods. In all examples of the 'create' method I see on the internet, there is a line of code similar to
#person = User.new(user_params) or whatever
My issue is that I don't have a vacation_days model. Only a controller. I want to edit the User database, but creating a new user cannot be the answer (right?).
How do I create a working create method?
This is not really RESTful... However, if you want to update an existing user, you can do so like this
#user = User.find(params[:id])
#user.update_attributes(params[:user])
where params[:id] would hold the id of the user you want to update and params[:user] would hold the attributes you want to update.
Since you are using form_for(#user) with its form builder, it should be fine.
It doesn't seem to me that you need your separate controller for vacation days. Simply have a vacation_days/edit view, which contains your form, and have it submit to users/update.
For clarity, your action should be editing and updating your user, rather than 'creating' one. So, your controller action to update your user should have the line:
#user.update_attributes(params[:user])
In my rails app, I have two models, a ClientPage and a ContentSection, where ClientPage has_many :content_sections. I'm using the nested_form gem to both models to be edited with the same form. This works fine as long as the ClientPage has at least one ContentSection, but if there are no associated ClientSections, the using nested_form's link_to_add method throws the following NoMethodError:
undefined method `values_at' for nil:NilClass
The form is structured as follows:
<%= nested_form_for page, form_options do |f| %>
# ClientPage fields
# ClientSections
<%= f.link_to_add "Add new section", :content_sections %>
<% end %>
As long as there is at least one ClientSection associated with the page, this works fine. As soon as there isn't, the error is thrown. Removing the link_to_add also stops the error from being thrown. (There's actually a second nested model under ContentSection, and the same issue arises if there are no associated models.)
Not sure what I'm fairly obvious thing I'm missing, but any pointers or suggestions would be greatly appreciated.
Finally worked this out -- the error was due to the fact that I was using the gem in a slightly non-standard way. Within the form, instead of rendering all of the content sections the standard way:
<%= f.fields_for :content_sections do |section_form| %>
# section fields
<% end %>
I put it inside a loop, as I needed the index of each item (which is not stored within the model itself):
<% page.content_sections.each_with_index do |section, index| %>
<%= f.fields_for :content_sections, section do |section_form| %>
# section fields
<% end %>
<% end %>
The issue doing it this way is that the fields_for method does not get called if the association is empty, and as such the gem cannot build the blueprint for the object (which is used to add in the extra item when link_to_add is called).
The solution was to make sure fields_for got called even if the association was empty:
<% if page.content_sections.empty? %>
<%= f.fields_for :content_sections do |section_form| %>
# section fields
<% end %>
<% end %>
I have a model called Note. Each note belongs_to :call_reason. And call_reason has_many :notes.
What I want to do in a view is display a list of call_reasons and a total count of each next to it so we can see what the most popular call reasons are.
Here's what I have so far:
dashboard_controller:
def index
#notes = Note.all
end
dashboard view:
<% #notes.each do |n| %>
<%= n.call_reason.reason %>
<% end %>
This lists all notes' call_reasons.
I'm stumbling on how to list each call_reason once with a total count next to it. What I have now just lists all the call_reasons per note which is a mess. I think I could scope this out somehow or change the instance variable but I'm having a hard time getting it right.
Any thoughts?
Since you want to list call reasons, you should start with that:
def index
#call_reasons = CallReason.all
end
Then in your view you can do this:
<% #call_reasons.each do |call_reason| %>
<%= call_reason.reason %> <%= call_reason.notes.count %>
<% end %>
Note that this will perform a query for every call reason in your database. To prevent this you can use a counter cache. Check out the section on counter cache on Rails Guides too.
My question is similar to this SO problem, but it does not answer my question:
Rails 3 default datetime format without UTC
I understand that I can add
Time::DATE_FORMATS[:default] = "%Y/%m/%d"
to my environment.rb and it will change my default time format accordingly.
So when I do Time.now.to_s I get the correct format.
However, my question is, how come this does not work with a form_builder. For example:
<%= f.text_field :date %>
will return a full UTC timestamp: 2013-01-25 07:45:21
I am aware that I can do this
<%= f.text_field :date, :value => #post.date.to_s %>
And it will give me the correct format.
But this solution seems hacky to me. Is this really the only way to do it?
I am not 100% sure. But the answer is most likely no. The rails formhelper just gives you whatever value that is in your database.
Here is where the value is retrieved for your reference:
http://api.rubyonrails.org/classes/ActionView/Helpers/InstanceTag.html#method-i-to_input_field_tag
There is no special processing of it, such as localize or to_s, which is where formatting is applied.
If the date looks like a timestamp (2013-01-25 07:45:21 UTC),
You could do something like the following to views you localize timestamps with:
<%= localize(#post.date, :format => :long) %>
According to your problem, you could try using date_select rather than text_field.
<%= f.date_select :date, :order => [:day, :month, :year] %>
I have an index where I'm showing a list of documents. I would like to implement a multiple select in order to do different actions to the documents the user has selected
I have created a
<%= check_box_tag 'id', 'document.id %>
for each document, inside a form_tag
But if I select multiple checkboxes, the params that are passed to the action are overwrited and I'm just receiving the id of the last checkbox I've selected in the id param.
¿Anyone knows how to implement multiple select?¿Any other approach?
I'm running Rails 3 and Ruby 1.8.7
Thanks in advance
You need to set :multiple => true
<%= check_box_tag 'id', document.id, :multitple => true %>
This will give you results in form of an array in params[:id]
Minor correction (plural):
<%= check_box_tag 'ids[]', document.id %>
ensure your model is properly set for attr_accessible something like :document_ids