Save 36% for Black Friday! Learn more

John Meehan

Joined

3,710 Experience
15 Lessons Completed
3 Questions Solved

Activity

save will run the validations before it persists, so there is no need to call valid? if you are calling save. The call to valid? in the else block is just to populate the errors on the object so your form can show them.

Posted in How do I display my uploaded picture?

<div class="inputfield">
  <%= f.label :avatar %>
  <% if @store.avatar %>
    <%= image_tag(@store.avatar, class: "avatar-bg", alt: "avatar image", id:  "avatar-background", type: "file") %>
  <% end %>
  <%= f.file_field :avatar, label:  "Avatar" %>
</div>

Posted in API

This will do it.

distance_of_time_in_words_to_now(DateTime.parse(car['date']))
#=> "1 day, 8 hours, and 32 minutes"
Did you get this working? 
https://planilha.tramitacaointeligente.com.br/planilhas/ED2sUXz32-R9CJKdkmtf8Q

It appears to me to be working for you.
Change your class name from `Completed_lesson` to 
CompletedLesson
Its a naming convention in Rails.
Lets deal with the first problem first, that version of Yarn is pretty old.  The current version is v1.7.0.
If you installed it with brew update it with: 
brew upgrade yarn

That will either work or give you another error message to look up.

Posted in Devise User Follow System

From what I remember Michael Hartl's Ruby on Rails Tutorial does a very good step by step guide to the following/followed via ajax part.

You are close! looking at the above the 'dataset' is empty which means your jquery targeting is slightly off.

I am assuming you want the value of modifier for the location that is the selected .

$('select#location_id :selected').data('modifier');

if you look this in the console:

$('select#location_id :selected')

the dataset shouldn't be empty and you should see modifier listed.

hope that helps.

Posted in Muliple has_one's on a polymorphic association

I wish to add a billing_address and a delivery_address to my Client's.
So i thought i would make my Addresses polymorphic.

class Client < ActiveRecord::Base
  has_many :addresses, as: :addressable
  has_one :delivery_address, class_name: 'Address', as: :addressable
  has_one :billing_address, class_name: 'Address', as: :addressable

  def delivery_address
    super || NullAddress.new
  end

  def billing_address
    super || NullAddress.new
  end
end

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true

  def full_address
    [line1, line2, town, county, postcode].compact.join(', ')
  end
end

How ever when i try to update the billing address @client.billing_address
both the @client.billing_address and @client.delivery_address get set to the same address. And the previous addresses :addressable_id gets set to nil.

  [1] #<Address:0x007f8bb1acdf20> {
      :addressable_id => nil,
    :addressable_type => "Client",
             :country => "Ireland",
              :county => "Limerick",
          :created_at => Sat, 25 Jun 2016 17:43:48 UTC +00:00,
                  :id => 2,
               :line1 => "123 New Street",
               :line2 => "College Court",
            :postcode => "DTW1234",
                :town => "Limerick",
          :updated_at => Sat, 25 Jun 2016 17:44:46 UTC +00:00
  },
  [2] #<Address:0x007f8bb1ace060> {
      :addressable_id => 25,
    :addressable_type => "Client",
             :country => "Ireland",
              :county => "Limerick",
          :created_at => Sat, 25 Jun 2016 17:44:46 UTC +00:00,
                  :id => 3,
               :line1 => "126 New Street",
               :line2 => "College Court",
            :postcode => "DTW1234",
                :town => "Limerick",
          :updated_at => Sat, 25 Jun 2016 17:58:14 UTC +00:00
  }

]

Obviously I can still see all the addresses doing @client.addresses. But how do I implement and maintain reference to separate billing and delivery address for a client.

Any tips/pointers welcome,
Thanks :)