f.input collect: is showing duplicates in the select - ruby-on-rails-4

I have the following simple_form input:
<%= f.input :user_id, collection: [options_for_select(User.all.map{ |u| [u.firstname, u.id]})] %>
There are 3 users in my local database. When I use the select in the form it shows the users twice like:
Tony
Johnny
Bill
Tony
Johnny
Bill
I'm not a pro with the map syntax above, so it may have to do with that.

I found this as a much better way to code the select. Also, the one I was trying above doesn't work on Heroku. This is what I used to make it work:
true) %>

Related

What is the shortest way of getting this?

What is the shortest/right/nice way to get this? Where f is the form alias for Customer model with customer_contract_id and belong_to CustomerContact model.
<%= f.object.customer_contact.blank? ? '':f.object.customer_contact.name %>
this is not working
<%= f.object.customer_contact.name ||= '' %>
I apologize upfront for any confusion/frustration. I appreciate any help that I can get!
Can you try it like this?
<%= f.object.customer_contact.present? ? f.object.customer_contact.name : '' %>
Please note the space between : and the two outputs. And .present? is the reverse of .blank?
For future references, I think this table would be of some help to you.
Credits for this table should go to Sibevin Wang
Hope this helps?

Rails: How to check for attributes with polymorphic? (Prints okay, won't allow conditional)

Going back to accessing user attributes (name, e-mail, avatar, etc) for the comments section ("identify user as commenter"), why does it let me print out some values but not use them in conditionals?
Why can I do this? (Prints the avatar next to the comment as it should)
<%= image_tag comment.user.avatar.url, size: "64" %>
But not this? (Won't let me check if there's an avatar or not first)
<% if comment.user.avatar.url.empty/blank/nil/present? %>
So if a commenter hasn't uploaded an avatar yet, it returns:
undefined method \'avatar\' for nil:NilClass
Is there a method I need to define in a controller, or a scope in a model, or is there another way of checking in this situation?
Thank you.

url_for in an email sent by a rake task in rails 4

I have a rake task that sends out daily digest emails of player activity during a day. (See example code below.) If I run PlayerActivityMailer.activity_report.deliver in my console, everything works just fine. However, when I try to invoke the rake task, I get the following error:
rake aborted!
ActionView::Template::Error: arguments passed to url_for can't be handled.
Please require routes or provide your own implementation
After doing some research, I found that in Rails 4, they totally nerfed ActionView::Helpers::UrlHelper.url_for (http://apidock.com/rails/v4.1.8/ActionView/Helpers/UrlHelper/url_for - notice the giant red minus sign under the 4.0.2). If you look at the source, you can see the error I'm seeing - it no longer takes options. As far as I can tell, that functionality still exists in other url_fors, such as the one in ActionDispatch::Routing::UrlFor. Also, the error message suggests including Rails.application.routes.url_helpers.
What I've tried
include ActionDispatch::Routing::UrlFor in both the rake task (inside the task) and the mailer (both at the same time, and each separately)
include Rails.application.routes.url_helpers in the same places and configurations, both with and without the UrlFor include.
The error still persists. My guess is that the page view is still insisting on using the ActionView::Helpers::UrlHelper version of url_for. I don't think I can include things actually in the views (which is sloppy looking and hacky even if I could).
Example Code
(heavily sanitized)
config/environtments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost:3000' }
lib/tasks/player.rake:
namespace :player do
task :activity => :environment do
PlayerActivityMailer.activity_report.deliver
end
end
app/mailers/player_activity_mailer.rb:
class PlayerActivityMailer < ActionMailer::Base
def activity_report
#activities = PlayerActivity.all
mail(to: 'foo#bar.com', subject: 'activity report')
end
end
app/views/player_activity_mailer/activity_report.html.erb:
<% #activites.each do |activity| %>
Player: <%= link_to activity.player.name, player_url(id: activity.player.id) %>
...
<% end %>
I also have a model Player, resources :players in my routes.rb file, and a PlayerActivity class with an association to Player.
I'm currently using the (really horrifying) workaround of #base_url = Rails.configuration.action_mailer.default_url_options[:host] in my mailer action and "http://#{#base_url}/players/#{activity.player.id}" in my view instead of the player_url part.
Help!
Have you tried passing just your player in the URL? Like this:
<% #activites.each do |activity| %>
Player: <%= link_to activity.player.name, player_url(activity.player) %>
<% end %>

Rails, filterrific with multiple parameters

I'm using the most recent ruby + rails, with the filterrific gem. It works great - but how can multiple parameters per scope be used? For single column filtering it is simple, but for the following scenario, how would it be handled?
Filter by X miles of zipcode Y
Scopes currently only have knowledge of the value being modified (EITHER miles, OR zipcode - two different scopes), but neither have knowledge of the other. This filter scenario requires knowledge of miles and zipcode. I have searched the documentation thoroughly and see no way. Has anyone done this before or am I missing something hidden in the documentation?
You can use Rails' fields_for form helper to submit a hash with multiple keys and values to a single filterrific enabled scope:
<%= f.fields_for :with_distance do |with_distance_fields| %>
<%= with_distance_fields.text_field :max_distance %>
<%= with_distance_fields.text_field :city %>
<% end %>
Then you can access the various values in the scope like so:
scope :with_distance, lambda { |distance_attrs|
# `distance_attrs` is a hash with two keys:
# {
# :max_distance => '10',
# :city => 'Vancouver',
# }
where(...)
}
This is tested with Filterrific 1.4.2 and Rails 4.1

Rails 4: tell simple_form to use input type "date" instead of showing different select boxes like Rails 4's f.date_field?

When I use Simple Form it shows three different select boxes for column type date. Instead of this I want Simple Form to show date attributes as HTML5 does. Specifically I want Simple Form to do the following:
<input type="date" name="org[established_at]" id="org_established_at">
For this I tried:
<%= f.input :established_at, as: :date %>
But this produces three different select boxes for date picking.
How do I tell Simple Form and Rails 4 to use input type "date"?
Recently I've done it like this:
f.input :established_at, as: :string, input_html: { class: :datepicker }
and used http://xdsoft.net/jqplugins/datetimepicker/ to show the calendar
In my project I used next:
= f.input_field :year, as: :datepicker, class: "string form-control teacher_kpk_year", readonly: true
Gemfile:
gem 'bootstrap-datepicker-rails'
applications.js
//= require bootstrap-datepicker
If you have a field defined as date, you only need add the parameter html5 like this:
f.input :established_at, html5: true
But, if you haven't defined your field like date or something like that, you only need to add the alias to the previous declaration, like this form:
f.input :established_at, as: :date, html5: true
And both forms will render a date or datetime picker like bootstrap-datepicker
best regards,