shakycode

Joined

5,390 Experience
2 Lessons Completed
3 Questions Solved

Activity

I'm maintaining an app and need to add a pop-up window or modal that launches from the current view but loads a separate controller/view. So far I have a link_to setup to open the controller/view in a new window/tab. But I'd really like this in a modal or pop-up window that will open on link_to and then a button on the view to close the window.

Here's my code:

calls/index.html.erb

 <%= link_to 'Notes', call_notes_path(call.id), :target => "_blank", :class => 'btn btn-warning btn-small' %>

notes/index.html.erb

    Notes:</br>
    <% @notes.each do |n| %>
      <%= n.created_at.strftime("%m/%d/%y-%H:%M") %> <%= n.message %> posted by <%= n.user.username %></br>
    <% end %>

    <%= form_for @note, :url => url_for(:controller => 'notes', :action => 'create') do |f| %>
    <%= f.text_field :message %>
    <%= f.submit %>
    <% end %>

controllers/notes_controller.rb

class NotesController < ApplicationController

  def index
    @call = Call.find(params[:call_id])
    @note = @call.notes.new
    @notes = Call.find(params[:call_id]).notes.order('created_at asc')
  end

  def create
    @call = Call.find(params[:call_id])
    @note = @call.notes.new(params[:note])
    @note.user_id = current_user.id

      if @note.save
          @call.send_mail(:call_note)
       redirect_to call_notes_path(@call), :notice => "Note has been posted to #{@call.incident_number}"
     else
       flash.alert = "Note was not saved or no data entered."
       redirect_to call_notes_path(@call), :notice => "Note has was not posted"
     end
  end
end

I've done some research regarding modals, but all the solutions I've found want your view code to be included in the origin controller/view space. I want to be able to call the notes#index view from within a pop-up window or modal. Then add a link to close the window after notes are done being entered.

Posted in Who is using Hashicorp Otto?

My workflow is git -> github -> capistrano -> bare metal (work) git -> github -> capistrano -> digital ocean (personal projects). I am working on setting up CircleCI at work, but that would require us to actually have test coverage, which we don't.

I'm a bit confused on your statements about AWS and the pub key. Are you trying to deploy from your Otto VM instance to somewhere? If so, you'll need to generate a new ssh key on the VM and add that to github or whatever repo you're using so that it can auth and pull the repo/make changes.

If you can clarify a bit, I'll be happy to help where I can.

Posted in validate presence of specific domain in email

The posted regex should work so long as you are passing in the whitelisted FQDN into the regex pattern.

When in doubt, practice your regex patterns on Rubular

Posted in Who is using Hashicorp Otto?

I'm using Otto and also Docker for spinning up dev environments. Besides what you've listed, what are your major roadblocks?

Posted in custom method in class to perfom simple calculation

Glad it worked out for you! (thumbsup)

Posted in custom method in class to perfom simple calculation

You can simplify this by adding scopes to your attendances model.

scope :present, -> {where(present: true)}

Then in your view or controller you can call AttendanceSheet.find(0).attendances.present.count

The problem you are running into is that you're trying to find attendances for each attendance sheet without passing it an argument for find. You can iterate through this in a block in your view then do objectvar.attendances.present.count where objectvar is the block variable you pass in the view.

Also your method(s) total_absent_agent are defined in the model but you are not calling them in the controller or the view, so it's basically not doing anything.

It might be a good idea to scope what you are actually trying to constrain, in this case attendances. Then use activerecord's relationship of has many attendances to extend that object from your primary object, attendance_sheet.

Posted in Ajax Search Remote True across the site

Way late on a response here. I figured out the problem why it was forcing a HTML response on hitting search box. I had the search_form_for wrapped in a <form> tag in the application.html.erb. Removing that allowed a JS/Ajax action to fire properly.

Posted in Ajax Search Remote True across the site

I even just added jquery-turbolinks to see if that might be an issue. No change in behavior. (smashes head) lol

Posted in Ajax Search Remote True across the site

@chris

I commented out turbolinks, and underscore which is the only JS stuff that's being required. Still the same problem. Index view will fire JS/Ajax, application layout will fire HTML. It's a mystery :)

Posted in Ajax Search Remote True across the site

@chris

Absolutely, I've already inspected the console/logs and I'm not throwing out any errors. Here's the same params/action being passed. 1st from the index view as js, then from the application.html.erb which is forced as html. This is from development.log

Started GET "/resources?utf8=%E2%9C%93&location=houston&commit=Find" for 127.0.0.1 at 2015-08-09 09:34:01 -0500
Processing by ResourcesController#index as JS
  Parameters: {"utf8"=>"✓", "location"=>"houston", "commit"=>"Find"}
  Resource Load (2.1ms)  SELECT DISTINCT resources.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((29.7604267 - resources.latitude) * PI() / 180 / 2), 2) + COS(29.7604267 * PI() / 180) * COS(resources.latitude * PI() / 180) * POWER(SIN((-95.3698028 - resources.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((resources.longitude - -95.3698028) / 57.2957795), ((resources.latitude - 29.7604267) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "resources" WHERE (resources.latitude BETWEEN 28.313108868891522 AND 31.207744531108478 AND resources.longitude BETWEEN -97.03701125791528 AND -93.70259434208472 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((29.7604267 - resources.latitude) * PI() / 180 / 2), 2) + COS(29.7604267 * PI() / 180) * COS(resources.latitude * PI() / 180) * POWER(SIN((-95.3698028 - resources.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 100)  ORDER BY distance ASC
  Rendered resources/_search.html.erb (0.6ms)
  Rendered resources/_map.html.erb (1.9ms)
  Rendered resources/_results.html.erb (0.6ms)
  Rendered resources/index.js.erb (5.4ms)
Completed 200 OK in 582ms (Views: 25.0ms | ActiveRecord: 2.1ms)
Started GET "/resources?utf8=%E2%9C%93&location=houston&commit=Find" for 127.0.0.1 at 2015-08-09 09:34:24 -0500
Processing by ResourcesController#index as HTML
  Parameters: {"utf8"=>"✓", "location"=>"houston", "commit"=>"Find"}
  Resource Load (5.6ms)  SELECT DISTINCT resources.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((29.7604267 - resources.latitude) * PI() / 180 / 2), 2) + COS(29.7604267 * PI() / 180) * COS(resources.latitude * PI() / 180) * POWER(SIN((-95.3698028 - resources.longitude) * PI() / 180 / 2), 2))) AS distance, MOD(CAST((ATAN2( ((resources.longitude - -95.3698028) / 57.2957795), ((resources.latitude - 29.7604267) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "resources" WHERE (resources.latitude BETWEEN 28.313108868891522 AND 31.207744531108478 AND resources.longitude BETWEEN -97.03701125791528 AND -93.70259434208472 AND (3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((29.7604267 - resources.latitude) * PI() / 180 / 2), 2) + COS(29.7604267 * PI() / 180) * COS(resources.latitude * PI() / 180) * POWER(SIN((-95.3698028 - resources.longitude) * PI() / 180 / 2), 2)))) BETWEEN 0.0 AND 100)  ORDER BY distance ASC
  Rendered resources/_search.html.erb (1.0ms)
  Rendered resources/_map.html.erb (3.5ms)
  Rendered resources/_results.html.erb (2.3ms)
  Rendered resources/index.html.erb within layouts/application (19.4ms)

Posted in Ajax Search Remote True across the site

I'm building an app in Rails 4.2.x and I'm using Ransack + Geocoder for my searches. Originally I setup a Remote: True search using Ajax in my index action and I can search no problem with the response of JS in the controller from the index view like so:

index.html.erb

<div id="mapdisplay">
    <%= render 'map' %>
</div>
<div id="results">
    <%= render 'results' %>
</div>

index.js.erb

$("#mapdisplay").html("<%= escape_javascript render("map") %>");
$("#results").html("<%= escape_javascript render("results") %>");

_map.html.erb

<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<h4>Resources</h4>
<div class="form-group">
  <div class="pull-right">
  <%= search_form_for(@search, :id => "resource_search", remote: true) do |f| %>
    <%= text_field_tag :location, params[:location], placeholder: "Houston, TX", class: "input-sm form-control" %>
    <%= f.submit "Find", class:'btn btn-sm btn-info' %>
<% end %>
  </div>
</div>
<div class="pull-right">
    <%= link_to "Add", new_resource_path, class: 'btn btn-info btn-medium' %>
</div>
<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script>
handler = Gmaps.build('Google');
handler.buildMap({
    provider: {
      disableDefaultUI: true
      // pass in other Google Maps API options here
    },
    internal: {
      id: 'map'
    }
  },
  function(){
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
  }
);
</script>

_results.html.erb


  <table class="table table-striped">
    <thead>
      <tr>
        <th>Name</th>
        <th>Address</th>
        <th>Notes</th>
      </tr>
    </thead>
    <tbody>
  <% @resources.each do |r| %>
      <tr>
        <td><%= link_to "#{r.name}", r %></td><td><%= r.address %></td><td><%= r.notes %></td>
      </tr>
  <% end %>
    </tbody>
  </table>

This works fine and dandy in the index view. But I see a use case for moving the search to the application.html.erb layout file. So I did that by doing this.

application.html.erb

<%= search_form_for(@search, url:"/resources", :method => :get, remote: true) do |f| %>
              <%= text_field_tag :location, params[:location], placeholder: "Houston, TX", class: "input-sm form-control" %>
              <%= f.submit "Find", class:'btn btn-sm btn-info' %>
            <% end %>

application_controller.rb

before_action :build_search
private
  def build_search
    if params[:location].present?
        @search = Resource.near(params[:location], 100).search(params[:q])
      else
            @search = Resource.search(params[:q])
      end
  end

So i can search from any view in the app as long as there's an @resources instantiated in the respective controller action and a partial rendered. So I have this setup in my new and show actions respectively.

My main question would be, how to get the ajax search to work site-wide to where it doesn't do a page load and change the URL? This works in the index view if I keep the search box inside of the index view, but when I abstract it out to the application layout file it does a full html request, pageload, and changes the URL.

I'm sure I'm missing something simple so any advice would be greatly appreciated.

Cheers,
ShakyCode

Posted in Getting data from one Rails app to another

I'll try to keep this as brief as possible.

I have a Rails app which is a static codebase and runs on 9 different servers all the same db schema but of course with different values.

I wrote some SQL to query some dollar totals and will be putting this into either a rake task or a sidekiq worker and have it fire once a week to generate the data. Initially I was thinking of just throwing the resulting data from each server into a mailer and mailing it to whoever needs the data. This is pretty straight forward.

But there's a kink in this, we need to see metrics over time in highcharts or some other charting engine.

So here's my thought.

  • Create the sidekiq worker and fire it on a schedule
  • Take the resulting data from each server and populate it on a target server via Postgres (not sure how to do this)
  • The target server will have a very simple Rails app built that will have a model with metrics and an association for each server (ie server 1 server 2 etc), after populating the data via postgres (somehow) from the source servers, read the data in HighCharts and present the view

So that's my thought process so far. I'm not sure on how to get the data from the source servers via a live postgres call when the sidekiq worker fires. So that's problem #1. Problem #2 or more like question #2 is, would this be a better use case for creating some sort of consumable API on the target Rails server? If so, what's the best place to start.

If my question and thought process is unclear, please let me know so I can clarify and explain in better detail.

Cheers!

Posted in CSV Model method and Summing Hours cleanly.

With some help this morning from Chris and me reading APIDocs on filtering arrays we came to this method which works right. :)

def self.to_csv(records = [], options = {})
      CSV.generate(options) do |csv|
        csv << ["Employee", "Clock-In", "Clock-Out", "Station", "Comment", "Total Shift Hours"]
          records.each do |ce|
          csv << [ce.user.try(:username), ce.formatted_clock_in, ce.formatted_clock_out, ce.station.try(:station_name), ce.comment, TimeFormatter.format_time(ce.total_hours)]
        end
       records.map(&:user).uniq.each do |user|
         csv << ["Total Hours for: #{user.username}"]
         csv << [TimeFormatter.format_time(records.select{ |r| r.user == user}.sum(&:total_hours))]
       end

        csv << ["Total Payroll Hours"]
        csv << [TimeFormatter.format_time(records.sum(&:total_hours))]
      end
    end

Posted in CSV Model method and Summing Hours cleanly.

Ok so after looking at this, I realized I was in error.

Getting the users's time totals like this: csv << [TimeFormatter.format_time(ce.user.clock_events.sum(&:total_hours))] is wrong.

Because I'm going back up the association in reverse and pulling all of their clock_events and then summing, instead of using what the records array is passing in from the controller.

So it's time to figure this out.

Posted in CSV Model method and Summing Hours cleanly.

Ok, I've partially figured this out.

def self.to_csv(records = [], options = {})
    CSV.generate(options) do |csv|
      csv << ["Employee", "Clock-In", "Clock-Out", "Station", "Comment", "Total Shift Hours"]
      records.each do |ce|
        csv << [ce.user.try(:username), ce.formatted_clock_in, ce.formatted_clock_out, ce.station.try(:station_name), ce.comment, TimeFormatter.format_time(ce.total_hours)] 
        csv << [TimeFormatter.format_time(ce.user.clock_events.sum(&:total_hours))]      
      end
      csv << [TimeFormatter.format_time(records.sum(&:total_hours))]
    end
  end

I shovel the user's clock_events based off of the ce object and sum the total hours into the csv object. This gives me the total time per employee in CSV, but since it's in the block and iterating it repeats it every other line. I have a feeling I'm very close, but for some reason doing this inside of a block doesn't seem right to me. Back to the drawing board.

Posted in CSV Model method and Summing Hours cleanly.

Ok, now I want to figure out how to show a total hours for each user inside of the block on this CSV method. Since it's iterating over every ce object and calling different fields, that's fine. But I'd like to figure out how to export to csv, sort by user.username (done), and after each user's clock events add a total for each user.

I'm thinking I can run another block inside of this method but unsure on how to wire it up. Thoughts?

Posted in jQuery and Turbolinks not playing nicely

Ok, so I'll try to make this brief.

I have a new Rails 4.2.1 app using Ruby 2.2.2 and I'm trying to do a live search on a basic crud model. I have ransack installed and it's working great. But I'm having problems with the jQuery ready/Dom binding in my jQuery to get the live search working.

I've installed jquery-turbolinks and now things like select2 work properly, which is great. But when I go to search using keyup in the JS the first two letters will get parsed in params and no other results will show up as i continue to type. Also if I backspace the search text it doesn't scope to super.

Here's my view:

index.js.erb

$("#search").html("<%= escape_javascript render("search") %>");
$("#results").html("<%= escape_javascript render("results") %>");

_search.html.erb

  <div class="pull-right" style="margin-right: 25px">
    <%= search_form_for(@q, url: "/inventory", method: :get, :id => "inventory_search", :remote => true) do |f| %>
      <%= f.search_field :asset_number_or_asset_name_or_asset_serial_cont, placeholder: 'Search...', class: 'form-control'%>
    <% end %>
  </div>

_results.html.erb

<table class="table table-striped">
  <thead>
    <tr>
      <th>Asset Number</th>
      <th>Asset Name</th>
      <th>Serial Number</th>
      <th>Model</th>
      <th>Manufacturer</th>
      <th>Asset Type</th>
      <th>Actions</th>
    </tr>
  </thead>

  <tbody>
   <% @assets.each do |a| %>      
      <tr>
        <td><%= a.asset_number %></td>
        <td><%= a.asset_name %></td>
        <td><%= a.asset_serial %></td>
        <td><%= a.asset_model %></td>
        <td><%= a.asset_manuf %></td>
        <td><%= a.asset_type.try(:name) %></td>
        <td><%= link_to 'View', inventory_path(a), :class => 'btn btn-mini btn-info' %> <%= link_to 'Edit', edit_inventory_path(a), :class => 'btn btn-mini btn-warning' %></td>

      </tr>
  </tbody>
  <% end %>
<%= will_paginate @assets %>

Here's my JS

application.js

 $(function(){
    $("#inventory_search input").keyup(function() {
   $.get($("#inventory_search").attr("action"), $("#inventory_search").serialize(), null, "script");
    return false;
    });
    });
});

I think I'm calling jQuery ready properly by declaring the function to load the dom and bind. But honestly my jQuery sucks so I'm probably missing something. Anyone have any idea what's happening here? This code worked great in Rails 3.2.x when I wrote it originally, but it's not really usable in 4.2.x with Turbolinks.

Posted in CSV Model method and Summing Hours cleanly.

Lol thanks for the tip, but I already figured that out and got it working. Sorry I didn't post a reply, been swamped. Here's what my method looks like now.

 def self.to_csv(records = [], options = {})
    CSV.generate(options) do |csv|
      csv << ["Employee", "Clock-In", "Clock-Out", "Station", "Comment", "Total Shift Hours"]
      records.each do |ce|
        csv << [ce.user.try(:username), ce.formatted_clock_in, ce.formatted_clock_out, ce.station.try(:station_name), ce.comment, TimeFormatter.format_time(ce.total_hours)]
      end
      csv << [TimeFormatter.format_time(records.sum(&:total_hours))]
    end
  end

Works like a champ :P

Posted in Ransack undefined method path in Rails 4.2.1

Ok, I figured this out. Similar to issues I was seeing with breaking Rails naming conventions in my other forms, I had to pass the url and method in the search_form_for helper method. It's working!

Here's my view for the search now:

    <%= search_form_for(@q, url: "/inventory", method: :get) do |f| %>
      <%= f.search_field :asset_name_cont, placeholder: 'Search...' %>
    <% end %>

Posted in Ransack undefined method path in Rails 4.2.1

I'm working on a simple CRUD Asset Management app and I want to use Ransack. I think I have this setup properly in my view and controller but each time the index view is hit I get the exception:

undefined methodassets_path' for #<#:0x007fa5f353e9e0>`

Here is my view:

index.html.erb

    <div class="pull-right">
    <%= search_form_for @q do |f| %>
      <%= f.search_field :asset_name_cont, placeholder: 'Search...' %>
    <% end %>
  <table class="table table-striped">
  <thead>
    <tr>
      <th>Asset Number</th>
      <th>Asset Name</th>
      <th>Serial Number</th>
      <th>Model</th>
      <th>Manufacturer</th>
      <th>Asset Type</th>
      <th>Actions</th>
    </tr>
  </thead>

  <tbody>
   <% @assets.each do |a| %>      
      <tr>
        <td><%= a.asset_number %></td>
        <td><%= a.asset_name %></td>
        <td><%= a.asset_serial %></td>
        <td><%= a.asset_model %></td>
        <td><%= a.asset_manuf %></td>
        <td><%= a.asset_type.try(:name) %></td>
        <td><%= link_to 'View', inventory_path(a), :class => 'btn btn-mini btn-info' %> <%= link_to 'Edit', edit_inventory_path(a), :class => 'btn btn-mini btn-warning' %></td>

      </tr>
  </tbody>
  <% end %>
<%= will_paginate @assets %>

Here is my controller excerpt: inventory_controller.rb

  def index
    @q = Asset.ransack(params[:q])
    @assets = @q.result.order("asset_number ASC").paginate(:page => params[:page], :per_page => 10)
  end

And here is my model (with annotation of the fields)

asset.rb

 id              :integer          not null, primary key
 asset_number    :string
 asset_shortname :string
 asset_name      :string
 asset_desc      :text
 asset_serial    :string
 asset_model     :string
 asset_manuf     :string
 asset_type_id   :integer
 created_at      :datetime         not null
 updated_at      :datetime         not null
 asset_location  :string


class Asset < ActiveRecord::Base
    extend FriendlyId
        friendly_id :asset_name, use: :slugged
        validates :asset_name, :asset_serial, :asset_type_id, :asset_model, :asset_manuf, :presence => true
        belongs_to :asset_type
    end

I think I have the controller wired up fine, I was having issues before with forms and routes by having a controller called assets which is an issue as a reserved name in Rails 4.x.x so I rebuilt the controller as inventory and call the Asset class from within.

My question is, I the search_form_for field to look at the Asset model's field for asset_name but when I write the view as I've laid out I constantly get that path error.

Is there a way to pass a different path into the Ransack search_field method so that I can get past what seems to be an issue with conventions?

If you need more information or if my question is not clear enough, please do not hesitate to edit or ask for more information. Thanks all!

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.