What is the shortest way of getting this? - ruby-on-rails-4

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?

Related

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.

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

f.input collect: is showing duplicates in the select

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) %>

Addling Line Break in Vim with Substitution

There is a really helpful article on Vim's wiki here that is nearly exactly what I want to do, I think I'm just missing something small.
I would like to take this line:
<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
and make it into:
<%= simple_form_for(resource,
as: resource_name,
url: session_path(resource_name)
) do |f| %>
However when I run:
/[(,)]
:s//\r&/g
I get:
<%= simple_form_for
(resource
, as: resource_name
, url: session_path
(resource_name
)
) do |f| %>
I need the linebreaks to happen AFTER the commmas and I'm unsure the regex to provide to make that happen. Thoughts?
Here is how you would replace every comma by a comma followed by a newline.
:s/,/&\r/g
If you also want to separate every pair of two )) by a newline, you can do this.
%s/))/)\r)/g
Thanks #FDinoff ! I figured it out, it was simply rearranging my search & replace with:
:s//&\r/g

How to use activemodel to validates date_time_select

I am using rails 4.0
I wonder how to validate date_select with activemodel
let's suppose I have the code as follow
app/models/book.rb
class Book
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :title, :written_on
validates :title, presence: true, allow_blank: false
# How validates :written_on if I use date_select Please look in my view below
end
app/controllers/books.rb
class BooksController < ApplicationController
#...
def new
#book = Book.new
end
def create
#book = Book.new(book_params)
if #book.valid?
#Do something
end
end
#...
end
app/views/books/new.html.erb
<% form_for #book do |f| %>
...
<%= f.date_select :written_on %>
...
<% end %>
I have also try adding
attr_accessor 'written_on(1i)'
to my book model but I got the error invalid attribute name 'written_on(1i)'
Really appreciated for the help here.
Just to clarify, I think you're asking why you're not even able to set the written_on attribute, let alone validate it-- when I used your exact code locally and tried to create a new book, on submit I got undefined method `written_on(1i)=' for Book.
This is because the Book model isn't inheriting from ActiveRecord::Base; you're just including ActiveModel::Model and ActiveModel::Validations. The Rails guide on form helpers says "when Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type."
So I started looking through the Rails source to see where this functionality was implemented, and it's currently in ActiveRecord::AttributeAssignment. There is currently an open pull request that moves this functionality to ActiveModel so that in cases like yours, you'd be able to use it by including ActiveModel::AttributeAssignment.
I'm not sure what you can do until that gets merged in and released. I tried including ActiveRecord::AttributeAssignment and still got the same error, and looking at the pull request, it doesn't seem to be that straightforward. You could fork Rails and apply that pull request, but you'd have to maintain your own Rails for a while until that lands, then get back on a released version.