Jacob Montgomery

Joined

34,500 Experience
33 Lessons Completed
41 Questions Solved

Activity

Posted in How is routing done with a conditional in url?

In a link, you can do something like

<%= link_to "Put a param", show_series_path(autoplay: 1) %>

to pass the param. You can also manually type it when you're testing in your own application.

Posted in How is routing done with a conditional in url?

Hey Matt,

?old_content=true is a param that's being passed to the controller action possibly located in controllers/series_controller.rb. Inside, let's say the show action, there would be something like old_content = params[:old_content] which would be populated with it's value, so in this case, true.

Now Chris can do all sorts of things with this info...

def show
  old_content = params[:old_content]

    if old_content == true
      return something special if it's supposed to be the old content
    else 
      return soemthing else when old_content is false or not present at all
    end
end

You can also chain them: ?old_content=true&autoplay=1&this_is_cool=true

and access like:

old_content = params[:old_content]
autoplay = params[:autoplay]
this_is_cool = params[:this_is_cool]

if this_is_cool
  puts "you rock!"
end

Ah sweet, glad that was it! Anytime! :)

Hey Neil,

Any reason you can't just do a if ?

if @runner.interests.squish.nil? 
  "#{@runner.first_name.capitalize} has no interests"
else
 "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}"
end

Posted in What are your goals for 2017?

2017 goals:

  • learn more ruby!
  • learn JS
  • patiently wait for @Alan to finish his cluster so I can clone his SD cards :D

Posted in has_and_belongs_to_many associations

What @Szilard said is correct - your controllers / views could care less how your models are structured. You just need to know how to query the model from the controller to grab the results you want. They're not that tightly knit.

So in this case, User.first.projects would list all the first users projects, Project.first.users would list all the users associated with the first project. You can call these from any view/controller.

Posted in How to make form fields to display placeholder text?

As another thought - you might actually be up against the value, not placeholder - if my previous suggestion doesn't make a difference, you should be able to change the above to update the input value instead of placeholder... ie

$('#input_field_1').val("Text for tasks show view");

Again, this is very hacky and I wouldn't suggest it long term - but if you just need it to work for the time being, it should be alright.

Posted in What do you listen to while coding?

Some great lists going so far! I'm probably pretty close to what @shakycode listens to on most occasions.

Pandora is my DJ - I have a few stations that I cycle through depending on my mood or what I'm working on. I tend to go through some weird phases, everything from old school gangster rap to Sleeping with sirens crab core haha.

I'm going to attempt to share the stations, but no clue if this will really work as expected:

Trifonic
Piano Guys
Caravan Palace
FC Kahuna
Squarepusher
Parov Stelar

Posted in How to make form fields to display placeholder text?

Could you share how you tried to impliment it? In my minds eye, the implimentation would be rather hacky, but would work if you need a quick fix that you can later come back and improve as you have time.

something like this should work:

<%= content_for :footer_js do %>
  <script>
    <% if params[:controller] == 'projects' && params[:action] == 'show' %>
      $('#input_field_1').attr("placeholder", "Placeholder text for projects show view");
    <% elsif params[:controller] == 'tasks' && params[:action] == 'show' %>
      $('#input_field_1').attr("placeholder", "Placeholder text for tasks show view");
    <% end %>
  </script>    
<% end %>

You would then need to have a <%= yield :footer_js %> placed at the bottom of your main layout.

Oh, and you would need to place the content_for snippet above into the form partial that you call for your views.

Posted in How to make form fields to display placeholder text?

I'd have to tinker with the code to see what I could come up with... but if you just want a quick fix, you could always use jquery to replace the placeholder based on whatever controller / action / view you want...

$('#your_text_field').attr("placeholder", "Whatever you want based on this controller / action / view");

If you rolled your own authentication then really you're just about the only one that could answer that since it depends on how you implimented it and store the user session.

The best I can surmise, the point of the snippet is that the typical current_user object provided by devise isn't available due to channels not being able to access the current session (see notes) - so in order to check the current_user, we have to go directly to the warden middleware and snag the user object.

The WebSocket server doesn't have access to the session, but it has access to the cookies. This can be used when you need to handle authentication.

You may be able to do something with this method using cookies: http://www.rubytutorial.io/actioncable-devise-authentication/

Check out:
https://github.com/hassox/warden/wiki/Overview

http://stackoverflow.com/questions/32401697/how-does-warden-get-the-current-user-from-the-rack-session

https://github.com/hassox/warden/blob/master/lib/warden/proxy.rb#L184

https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L37

Just to +1 what Chris said - this is the perfect scenario for service objects! If you haven't already, check out his video on PORO's - https://gorails.com/episodes/wrapping-business-logic-with-plain-old-ruby-objects

There are a ton of articles on the interwebz (keywords: ruby poro or ruby service objects) that I would strongly suggest reading since the whole idea is more about organization of code more so than core functionality once you understand how to use them.

Good luck! :)

Sweet, I'm glad that really was the issue!

I've done that countless times, imagemagick gets me everytime!!

Unless I'm just reading it wrong, the error is just that, you don't have cmake installed...

ERROR: CMake is required to build Rugged.

sudo apt-get install cmake

http://stackoverflow.com/questions/27472234/an-error-occurred-while-installing-rugged

If you do have cmake, check the end of your error log, it says:

To see why this extension failed to compile, please check the mkmf.log which can be found here:
/var/app/ondeck/vendor/bundle/extensions/x86_64-linux/2.3.0-static/rugged-0.25.0b10/mkmf.log

So you can check the log there to see if it gives any additional details

Haha, making it harder is the easy part - making it easy is the hard part :)

Anytime!

Here you go - trick is to remember that even though it's nested, it can still be iterated over inside another iteration... it helps to use byebug, so when I got to the part of getting the year values to display, I just placed <% byebug %> right below <% item[:years].each do |year| %> so I could then see how the objects are being presented and adjust as necessary.

<table>
  <tr>
    <th>Month</th>
    <th>2017</th>
    <th>2018</th>
    <th>2019</th>
  </tr>
  <% @array.each do |item| %>
    <tr>
      <td><%= item[:month] %></td>
      <% item[:years].each do |year| %>
        <td><%= year[1] %></td>
      <% end %>
    </tr>
  <% end %>
</table>

Ah boo, markdown isn't parsing the table - either way I think you see what I mean... lol 1 moment let me re-work with the table method in mind.

Oh wait, are you needing them to be like this:

| Month | 2017 | 2018 | 2019 |
| --- | --- | --- | --- |
| January | 20 | 100 | 300 |
| February | 30 | 40 | 80 |

Hi Premila,

In your views controller, you'll want to do something like:

def index # or whatever method you're working with
  @array = [{:month=>"January", :years=>{2017=>20, 2018=>100, 2019=>300}}, {:month=>"February",
:years=>{2017=>30, 2018=>40, 2019=>80}}]
end

then in your view, you'll do something like:

<% @array.each do |item| %>
  <ul>
    <li><%= item[:month] %> <%= item[:years] %></li>
  </ul>
<% end %>

Posted in DRY Validations for Similar Models

Just to provide an alternative solution - If you're still early in the development cycle and haven't gone to production yet, you could also create a contact model that stores all the address info, then you'd only have one model to manage validations:

#models/restraunt.rb
has_one :contact, as: :contactable, dependent: :destroy

#models/location.rb
has_one :contact, as: :contactable, dependent: :destroy

#models/contact.rb
belongs_to :contactable, polymorphic: true

then your migration could be something like:

class CreateContacts < ActiveRecord::Migration
  def change
    create_table :contacts do |t|
      t.string :line_1
      t.string :line_2
      t.string :city
      t.integer :state, :default => 0
      t.string :zip
      t.string :email
      t.string :phone
      t.decimal :lat, :precision => 10, :scale => 6, :default => 0
      t.decimal :lng, :precision => 10, :scale => 6, :default => 0
      t.references :contactable, polymorphic: true, index: true

      t.timestamps null: false
    end
  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.