shakycode

Joined

5,400 Experience
2 Lessons Completed
3 Questions Solved

Activity

Hey John, thanks for chiming in on this thread. You're right, I wasn't targeting the selected object. Doing $('select#location_id :selected').data('modifier'); gave me an output of the modifer in the console. I think I can take it from here and use the .change method to watch for the element changing then do some nifty logic to conditionally show/hide/populate my div.

Also thanks to Erik for helping me. You both are great, glad to have you onboard at Gorails. I love it when a community comes together and helps eachother out.

If you ever need a hand with anything ruby/rails I'm not a guru but I'd be happy to help :)

Cool, here's what I have so far. I should have been more clear that my selector id is prefixed with run. I apologize for not being more clear.

parentElement :select#run_location_id

option
accessKey
:
""
assignedSlot
:
null
attributes
:
NamedNodeMap
baseURI
:
"http://loopify.xyz:9001/runs/new"
childElementCount
:
0
childNodes
:
NodeList[1]
children
:
HTMLCollection[0]
classList
:
DOMTokenList[0]
className
:
""
clientHeight
:
0
clientLeft
:
0
clientTop
:
0
clientWidth
:
0
contentEditable
:
"inherit"
dataset
:
DOMStringMap
defaultSelected
:
false
dir
:
""
disabled
:
false
draggable
:
false
firstChild
:
text
firstElementChild
:
null
form
:
form#new_run.new_run
hidden
:
false
id
:
""
index
:
1
innerHTML
:
"Brookside Nursing Center"
innerText
:
"Brookside Nursing Center"
isConnected
:
true
isContentEditable
:
false
label
:
"Brookside Nursing Center"
lang
:
""
lastChild
:
text
lastElementChild
:
null
localName
:
"option"
namespaceURI
:
"http://www.w3.org/1999/xhtml"
nextElementSibling
:
option
nextSibling
:
text
nodeName
:
"OPTION"
nodeType
:
1
nodeValue
:
null
offsetHeight
:
0
offsetLeft
:
0
offsetParent
:
null
offsetTop
:
0
offsetWidth
:
0
onabort
:
null
onbeforecopy
:
null
onbeforecut
:
null
onbeforepaste
:
null
onblur
:
null
oncancel
:
null
oncanplay
:
null
oncanplaythrough
:
null
onchange
:
null
onclick
:
null
onclose
:
null
oncontextmenu
:
null
oncopy
:
null
oncuechange
:
null
oncut
:
null
ondblclick
:
null
ondrag
:
null
ondragend
:
null
ondragenter
:
null
ondragleave
:
null
ondragover
:
null
ondragstart
:
null
ondrop
:
null
ondurationchange
:
null
onemptied
:
null
onended
:
null
onerror
:
null
onfocus
:
null
oninput
:
null
oninvalid
:
null
onkeydown
:
null
onkeypress
:
null
onkeyup
:
null
onload
:
null
onloadeddata
:
null
onloadedmetadata
:
null
onloadstart
:
null
onmousedown
:
null
onmouseenter
:
null
onmouseleave
:
null
onmousemove
:
null
onmouseout
:
null
onmouseover
:
null
onmouseup
:
null
onmousewheel
:
null
onpaste
:
null
onpause
:
null
onplay
:
null
onplaying
:
null
onprogress
:
null
onratechange
:
null
onreset
:
null
onresize
:
null
onscroll
:
null
onsearch
:
null
onseeked
:
null
onseeking
:
null
onselect
:
null
onselectstart
:
null
onshow
:
null
onstalled
:
null
onsubmit
:
null
onsuspend
:
null
ontimeupdate
:
null
ontoggle
:
null
onvolumechange
:
null
onwaiting
:
null
onwebkitfullscreenchange
:
null
onwebkitfullscreenerror
:
null
onwheel
:
null
outerHTML
:
"<option value="1757" data-modifier="OT">Brookside Nursing Center</option>"
outerText
:
"Brookside Nursing Center"
ownerDocument
:
document
parentElement
:
select#run_location_id
parentNode
:
select#run_location_id
prefix
:
null
previousElementSibling
:
option
previousSibling
:
option
scrollHeight
:
0
scrollLeft
:
0
scrollTop
:
0
scrollWidth
:
0
selected
:
true
shadowRoot
:
null
slot
:
""
spellcheck
:
true
style
:
CSSStyleDeclaration
tabIndex
:
-1
tagName
:
"OPTION"
text
:
"Brookside Nursing Center"
textContent
:
"Brookside Nursing Center"
title
:
""
translate
:
true
value
:
"1757"
webkitdropzone
:
""

Sorry if this is a newb question me being seveal years in, but I'm assuming you want the element outputted in the console when I use Chrome's inspector. If so, it's going to be gigantic. Is there a certain way you want me to get this? Totally embarassing, but today I'm eating humble pie :)

Thanks, that's getting closer to what I need. Problem is when I inspect the element and call $('select#location_id').data('modifier'); I get an undefined method in the console even though the location_id is selected in the form. If I do a $('select#location_id').data it give me this:

(a,c){var d,e,g,h=null;if(typeof a=="undefined"){if(this.length){h=f.data(this[0]);if(this[0].nodeType===1&&!f._data(this[0],"parsedAttrs")){e=this[0].attributes;for(var i=0,j=e.length;i<j;i+…

Clearly I don't understand JS very well, so I'm not sure what I'm missing but I sure do appreciate your help.

Thanks for chiming in, Erik. The problem is the modifier is an attribute/column of the Location model and when selected it changes (it's not pre-loaded in the controller). I'm able to make it a data-attribute in the grouped select statement like this:

 <%= f.select :location_id, grouped_options_for_select(Region.order(:area).map{ |group| [group.area, group.locations.order('location_name asc').map{ |f| [f.location_name, f.id, {'data-modifier'=>f.location_modifier}] } ] }, {:include_blank => true, :class => 'select'}) %>

When I inspect via Chrome I see the data attribute of the selected location being set, but I'm unsure as to how to grab it from the html to evaluate it.

Thanks in advance for your help!

I am working on a Rails app and need some help with coffeescript which I'm honestly not very good at.

I have a form with a field called location_id using a collection in Rails.

<%= f.grouped_collection_select :location_id, Region.order(:area), :active_locations, :area, :id, :location_name_with_address, {include_blank: true}, class: 'select' %>

On the Location model there is a column named modifier which holds a value. Within my form I need to be able to grab the modifier value to evaluate it client-side to display/hide a div.

I'm starting to write my coffeescript and am able to get the id of the Location like this

$('#run_location_id').val()

But what I'm unsure of how to do is, how do I grab that id of the location, and evaluate the model attribute modifier? I'm not sure how to grab it on the client side.

I've googled and have read a lot of coffeescript snippets but can't seem to find the right direction to take.

I just need a bump in the right path so I can figure the rest out on my own.

Posted in What is your dream app?

If you could build anything (think outside of the box), what would it be?

Things I'm working on that are fun to build:

  • Whisper clone with material design UI for mobile and web
  • Anonymous Microblogging platform
  • Image sharing service similar to instagram but with imgur like features and more social component
  • A Simple bank clone (virtual currency, just an experiment in event driven architecture)
  • Yelp-like app to help people find mental health resources
  • Numerous projects that I've forgotten but intend to get back into

What are you building and what's your dream?

Posted in Pull data from another table in a lookup

@chrris sorry to hijack but the site looks great. love the editor, reactions, series, and more.

The only way I can think of that this sort of CPU/memory utilization spike would happen would be

  • Low memory VM
  • Gigantic set of assets that need to be precompiled
  • Passenger/Unicorn/Puma already maxing out the CPU/Memory

As a test I've spun up a 512mb droplet with Passenger/Postgres/Nginx and deployed an asset heavy application while Passenger was spun up.

Coming from an ops background I would definitely lean heavily on htop or top and sort the processes by niceness or CPU utilization. One (or both) of these tools should tell you what the offending process is.

By chance are you running any other sort of services, programs, scripts which may be proc intensive?

Would definitely like to help you get back on track. :-)

I deployed (which included asset compliation) 20x in a row and I could not spike the CPU to 100% (topped out at 44%) although memory did start to page (swap) during the app being spun up and used while capistrano was deploying.

Posted in Pull data from another table in a lookup

Chris is right, I just have a bad habit of hiding away complex queries into models. I think I wrongly assumed that this query was being used in multiple places as you suggested. Then yes, it's definitely of value to abstract it. But I do have a nasty habit of hiding things in models when I shouldn't. Got to work on that.

If you are pulling from the gorails chatrooms repo, I did a PR against this repo which fixes the nil issue. You can either clone the repo and use it as reference for your own iteration or clone it and just hack on the existing repo (suggest forking into your own github account thought :)

Posted in Pull data from another table in a lookup

To clean this up, you may want to create a scope on article which performs all the joins and association loading. Would allow you to clean up the controller a bit and just says Article.method (where method is your AR query. Good practice to try to keep as much code out of the controller as possible, but Chris' answer will definitely get you what you need. I'd just suggest moving this to the model.

A load average of 7.47, 7.58, 7.13 is not normal. What's interesting is it shows that your CPU core (initially) is only 35%. Can you please take a screenshot and upload it here using htop or top sorting my processor/CPU usage?

Posted in How to build REST APIs?

Looking forward to a video on this too. In the interim I've done a simple JSON API in Rails on my YouTube channel if you want to have a look, although I'm sure Chris' tutorial will be much better and robust.

Posted in Must Read / Must Watch

Designing Hypermedia APIs

A great book by Steve Klabnik on Restful Hypermedia API design

Posted in How to build REST APIs?

Another great resource is by Steve Klabnik Designing Hypermedia APIs Steve is a pro when it comes to API and architecture design. Definitely one book to read!

Posted in Ajax refresh while persisting params

I'm maintaining an old Rails app and am trying to have a my index action use a form_tag to pass the params of a region so I can scope the results of calls by region. This works fine and dandy, but I need to figure out how to do an ajax refresh of the partials while persisting the params that come from the view.

Here's some code

index action on calls controller

  def index
    if params[:region].present?
      @assigned = Call.where(region_id: params[:region][:area]).assigned_calls.until_end_of_day
      @unassigned = Call.where(region_id: params[:region][:area]).unassigned_calls.until_end_of_day
      @note = Note.new
    else
      @assigned = Call.assigned_calls.until_end_of_day
      @unassigned = Call.unassigned_calls.until_end_of_day
     @note = Note.new
   end
  end

reload actions on calls controller

def reload_active
    if params[:region].present?
      @assigned = Call.where(region_id: params[:region][:area]).assigned_calls.until_end_of_day
      @unassigned = Call.where(region_id: params[:region][:area]).unassigned_calls.until_end_of_day
      @note = Note.new
    else
      @assigned = Call.assigned_calls.until_end_of_day
      @unassigned = Call.unassigned_calls.until_end_of_day
     @note = Note.new
   end
    render :partial => "calls/assigned_calls"
  end

  def reload_inactive
    if params[:region].present?
      @assigned = Call.where(region_id: params[:region][:area]).assigned_calls.until_end_of_day
      @unassigned = Call.where(region_id: params[:region][:area]).unassigned_calls.until_end_of_day
      @note = Note.new
    else
      @assigned = Call.assigned_calls.until_end_of_day
      @unassigned = Call.unassigned_calls.until_end_of_day
     @note = Note.new
   end
    render :partial => "calls/unassigned_calls"
  end

index.html.erb

<div id="active">
  <%= render "assigned_calls" %>
</div>

<div id="inactive">
  <%= render "unassigned_calls" >
</div>


<script>
$(document).ready(
         function() {
          setInterval(function() {
            $('#active').load('/calls/reload_active');
            $('#inactive').load('/calls/reload_inactive');
        }, 30000);
    });
</script>

routes.rb

resources :calls do
    collection do
      get 'reload_active'
      get 'reload_inactive'
    end
end

_assigned_calls.html.erb (excerpt of form_tag

 <%= form_tag calls_path, :method => 'get' do %>
 <%= select_tag "region[area]", options_from_collection_for_select(Region.order(:area), :id, :area, selected: params[:region].try(:[], :area)), prompt: "Choose Region" %>
 <%= submit_tag "Select", :name => nil, :class => 'btn' %>

So when I do an initial page load of /calls and then select the region and choose submit/select it will pass the params and load the calls by the region no problem.

The problem springs up when I'm doing the ajax refresh, it will not persist the params from the URL and I'm stumbling on how to pass them. The partials should refresh every 30 seconds and load call data scoped by region where the params were previously passed.

I'm doing some googling and stack review but figured I'd open the question here if anyone had any advice on how to do this. I'm sure it's possible but to be honest my JS/jQuery game is a bit weak.

Thanks in advance guys!

+1 On this, I'm subbed to see what you come up with. If I can help, let me know

Posted in Nested form and models?

I think you're right. A has_many should do the trick. Just trying to figure out how to create new Narcotics from within the nested form. Seems like I have to do a bit of research to make this work. Unless you have a tutorial out there that explains it well. I've used nested forms for attributes already existing in the db, haven't used it for creating new associated objects.

Posted in Nested form and models?

I just took a look at the cocoon gem. That really makes nested forms easier. I'll give it a shot. My main concern was what's the best approach to take on the model side with relationships.

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.