Neil Patel

Joined

3,810 Experience
5 Lessons Completed
4 Questions Solved

Activity

Posted in Rails5 + JQuery progress bar not functioning

added the url for the src was incorrect

Posted in Rails5 + JQuery progress bar not functioning

Implementing a JQuery progress bar so when you scroll down it should show a green bar across the top. When I start scrolling the progress bar does not appear. I use inspect element on the bar div class . The element shows the width % going up . but no progress bar?

style="width: 8.0263232%;>
scroll.js $(document).on('scroll', function() { var pixelsFromTop = $(document).scrollTop() var documentHeight = $(document).height() var windowHeight = $(window).height() var difference = documentHeight - windowHeight var percentage = 100 * pixelsFromTop / difference $('.bar').css('width', percentage + '%') }) show.html.erb div class="progress">


application.scss

.progress {
position: fixed;
top: 0;
left: 0;
width: 100%;
}

.bar {
position: absolute;
top: 0;
left: 0;
width: 0;
height: 3px;
background-color: #7fdbff;
}

application.js

//= require jquery
//= require jquery-ui
//= require jquery_ujs
// require turbolinks
//= require_tree .

looking at the logs its not finding scroll but its showing the width element going up,

Started GET "/products/scroll.js" for 127.0.0.1 at 2017-08-14 12:27:17 +0100
Processing by ProductsController#show as JS
Parameters: {"id"=>"scroll"}
Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 0], ["LIMIT", 1]]
Completed 404 Not Found in 3ms (ActiveRecord: 0.4ms)

ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=scroll):

app/controllers/products_controller.rb:67:in `set_product'

Posted in how do i display search results

added to index.html.erb now showing results

<% @locations.each do |location| %>


<%= location.company_name %>
<%= location.address %>
<%= location.city %>
<%= location.postcode %>

<% end %>

Posted in how do i display search results

Following on from my prev question regarding implementing search using the geocoder gem [https://gorails.com/forum/how-do-i-implement-search-by-postcode-zipcode-using-the-geocoder-gem]

I have implemented a search feature on the index page. When i enter the postcode
it does not return any results . It gives me a url of http://localhost:3000/locations?utf8=%E2%9C%93&search=m20+2wz but nothing in the index view page.

locations_controller.rb

class LocationsController < ApplicationController
def index
if params[:search].present?
@locations = Location.near(params[:search], 10)
else
@locations = Location.all.order("created_at DESC")
end
end

** index.html.erb**

<div class="container">


SubContractor Postcode search


Use this postcode search to quickly find subcontractors in your area


<%= form_tag locations_path, :method => :get do %>
<%= text_field_tag :search, params[:search], placeholder: "e.g M20 2WZ" %>

    <%= submit_tag "Search Near", :name => nil %>
  </p>
<% end %>


location.rb

class Location < ApplicationRecord
geocoded_by :full_address
after_validation :geocode

def full_address
[address, city, postcode].compact.join(',')
end
end

Location.near("M20 2WZ", 15) now shows
id: 9, company_name: "Winton Flooring Ltd", address: "46 Bury Old Rd, Whitefield", city: "manchester", postcode: "M45 6TL", latitude: 53.53980319999999, longitude: -2.2820915, created_at: "2017-07-11 10:32:46", updated_at: "2017-07-11 10:32:46">]>

Thanks!

Building a small rails app that is able to search by postcode(zipcode) and radius, so it responds with contractors that are within that radius/postcode

schema.rb
create_table "locations", force: :cascade do |t|
t.string "company_name"
t.string "address"
t.string "city"
t.string "postcode"
t.float "latitude"
t.float "longitude"

Added the geocoder gem and updated the model

class Location < ApplicationRecord
geocoded_by :postcode
after_validation :geocode, :if => :address_changed?
end

when i add new locations it automatically gives me the latitude and longtitude of the location

in my index view i have added the search form

SubContractor Postcode search

  <p class="lead">Use this postcode search to quickly find subcontractors in your area</p>
  <p><%= form_tag locations_path, :method => :get do %>
    <%= text_field_tag :search, params[:search], placeholder: "e.g M20 2WZ" %>
    <%= submit_tag "Search Near", :name => nil %>
  </p>
  <% end %>

** updated routes**

Rails.application.routes.draw do

resources :locations, except: [:update, :edit, :destroy]
root 'locations#index'
end

and in my locations controller 

class LocationsController < ApplicationController

def index
@locations = Location.all
if params[:search].present?
@locations = Location.near(params[:search], 10, :order => :distance)
else
@locations = Location.all.order("created_at DESC")
end
end

when i run the app i search for m20 2wz and in my browser i get
http://localhost:3000/locations?utf8=%E2%9C%93&search=m20+2wz
but does not return any results ???

Also should my location.rb model be
geocoded_by :postcode or geocoded_by :address

or is it better to do
geocoded_by :full_address

def full_address
[address, city, postcode].compact.join(‘, ‘)
end

Hello

Folllowing on from the prev 2 posts , How do i add the default time of 645pm in the form. So when someone craates the form a default time of 645 is already setup. I am using simple form and datepicker.

At the moment I have this code which gives the current time and date (date is ok but want to change time to 645)

had a look at stackoveflow , simple form gem and datepicker but cannot resolve

i was thinking of using something like default: '645'

= f.input :started_at, as: :string, class: 'form-control', input_html: { value: l(@happening.started_at, format: :date_time_picker), class: 'date_time_picker' }, label: "Start at"

Posted in How do I add a default time of 6:45pm

its ok

in my area model i converted this to this usual_start_time.to_formatted_s(:time)
and is working

Posted in How do I add a default time of 6:45pm

Hi

Following on from the my previous post

I want to add a new time column with the default of 6.46pm

I have tried this

class AddUsualStartTimeToAreas < ActiveRecord::Migration
def change
add_column :areas, :usual_start_time, :time, default: "18:45"
end
end

but it gives me a date?? which i don't want??

t.time    "usual_start_time",                         default: '2000-01-01 18:45:00'

Hi Jack

thanks for getting back ( its not been deployed but have pushed via git to a shared repo)

so I have done rails db:rollback

== 20170120123129 AddUsualStartTimeToAreas: reverting =========================
-- remove_column(:areas, :usual_start_time, :time)
-> 0.5321s
== 20170120123129 AddUsualStartTimeToAreas: reverted (0.5422s) ================

and it has been removed from the areas table

can i now delete the incorrect migration from the db file which is below

class AddUsualStartTimeToAreas < ActiveRecord::Migration
  def change
    add_column :areas, :usual_start_time, :time
  end
end

and then create a new migration again but with the default time as 6:45 ?

or can i just update the incorrect db file and then do rake db:migrate

so it would be

def change
add_column :areas, :usual_start_time, :time, default: '6.45'
end
end

Hi

I have added a column ( t.time "usual_start_time") to an exisitng table called Areas. (create_table "areas",)

create_table "areas", force: :cascade do |t|
t.string "location", limit: 255
t.boolean "active", default: false
t.text "short_info"
t.string "twitter_name", limit: 255
t.boolean "proposed"
t.string "twitter_url", limit: 255
t.string "facebook_url", limit: 255
** t.time "usual_start_time"**

after creating the migiration i forgot to add a default time of 6:45 

how do i rollback and then change the column so it shows 646 as the default time.

I think its db:rollback and then do i have to add the column again with the default time e.g default: "6.45" (as its a time (t.time)

It can also be refactored as well , so it uses less code
by using the if/else statement in the interpolation

. #{@runner.first_name.capitalize} likes #{@runner.interests.present? ? @runner.interests.squish : 'running and voluntering'}

thanks Jacob!

hi

i am working on this interpolation where the runners has interests but lets say the runner has not interests , or has not entered any interests how would we interpolate this

Your runner is called #{@runner.first_name.capitalize}. #{@runner.first_name.capitalize}’s first run to you will be on #{format_date(@pairing.first_visit_date)} at #{@pairing.usual_visit_time}. #{@runner.first_name.capitalize}   #{@runner.first_name.capitalize} likes #{@runner.interests.squish}

    is something like if @runner.interests.squish.nil? = "no interests"

thanks chris!

I think its this
%p= f.submit "Submit", class: "btn btn-primary", data: { confirm: "Are you Sure"}

Hi

I want add a pop up button that says "Are you sure" when the user clicks on submit ? if it was a destroy mehod it would be something like this method: :delete, data: { confirm: "Are you sure?"} but not sure how to do it on the update method

def update
    if donation_plan.update(donation_plan_params)
      redirect_to :back, notice: "Thank you for your Gift Aid Donation"
    else
      render :show
    end
  end

or do i add this to the show page

 = simple_form_for donation_plan, url: my_donations_path do |f|
                  = f.input :giftaid, inline_label: " reclaim Gift Aid of 25p, in every £1 on past donations and all future donations I may make.", label: false
                  %p= f.submit "Submit", class: "btn btn-primary"

Posted in How do I send Prawn pdf via email

ok cool thanks Chris

how would I call the ReferralMailer method in the my controller?
with the new and create action?

Posted in How do I send Prawn pdf via email

Hello Everyone

how do i attaced a prawn pdf to email . I have implemented the prawn gem
so when you generate a new form and add the .pdf ot gives me the PDF , which works. but now when the form is created i want it to send an email with the PDF attached?


def new
    coach.build_referral(referrer: @current_referrer)

    respond_to do |format|
      format.html
      format.json
      format.pdf do
        pdf = BefriendingReferralForm.new(@current_referrer)
        send_data pdf.render, filename: 'referrer.pdf',
                              type: 'application/pdf',
                              disposition: :inline
      end
    end

but i would like to send an email when the form is submitted , i was thinking of doing this in the create method, This my current create method.


  def create
    coach.build_referral(referrer: @current_referrer)
    coach.assign_attributes(coach_params)

    if coach.save
        thinking adding code here?
      redirect_to referrers_root_path
    else
      render :new
    end
  end
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.