Rob Sturcke

Joined

7,980 Experience
60 Lessons Completed
2 Questions Solved

Activity

I'll have to checkout React/Vue.js then. I've been messing with AngularJS on and off but not too satisfied with the lang.

Came across a tutorial for using Devise authority tokens in a rails API, however are there any other reasons to use AngularJS and Rails? Advantages or disadvantages?

The AngularJS with Rails tutorial:
https://medium.com/@avatsaev/angular-2-and-ruby-on-rails-user-authentication-fde230ddaed8#.64zce1mc2

New but familiar to AngularJS and looking to integrate it into my projects.

Posted in Could you make a ActiveAdmin episode?

Awesome! Can't wait to check it out!

Posted in Could you make a ActiveAdmin episode?

Was browsing and found out about ActiveAdmin and thought it would be pretty cool to work with.
http://activeadmin.info

Best,
-Rob

Hey Erik,

Yeah it seems as though it's not accounting for the post favorites for the user after checking rails console so I think I found my main issue in the favorites_controller.

I need to redo my favorites_controller to sort of mimic the GoRails Liking Posts controller, I see that I'm not doing a few things with the create method @post.likes.where(user_id: current_user.id).first_or_create to identify it with the user, and using a private set_post to get the :post_id. (Around the 10:00 mark of the gorails tutorial).

Unfortunately my favoriting technique I used didn't give the proper current_user I guess.

I did also try moving down the current_user.favorite_for(post) but still ran into a error with favorite_for on line 11 favorites.where(post_id: post.id).first

Thanks for your help I really appreciate it!

Hey Dmitry,

I'll give it a shot after I configure the favorites controller, I'm sure it'll work.

Thanks for the help!

Hey thanks for the help. I did get a bit further with your recommendation.

I did run into a new issue that I'm working on solving with the same task of displaying favorites despite the fix. I'm having trouble defining which post I would be sending to the users#show which resulted in nil while I'm calling the .id.

NoMethodError in Users#show

def favorite_for(post)
     **favorites.where(post_id: post.id).first
  end
(added ** next to the line)

I'm creating a forum and am trying to display a list of posts that have been favorited by the current user in the users/show.html.erb view.

When I favorite a post however, then go to my user profile show page, I get the following error in my app/views/favorites/_favorite.html.erb:

NameError in Users#show

undefined local variable or method `post'

<% if favorite = current_user.favorite_for(post) %>

Am I missing something in my favorites_controller that is preventing it from saving, to then be rendered as a list? Or am I rendering it in the users/show.html.erb view improperly?

Here's my favorites_controller.rb:

class FavoritesController < ApplicationController
  before_action :require_sign_in

  def create
    post = Post.find(params[:post_id])
    favorite = current_user.favorites.build(post: post)

    if favorite.save
      flash[:notice] = "Saved as favorite!"
    else
      flash[:alert] = "Favorite failed to save."
    end
    redirect_to [post.topic, post]
  end

  def destroy
     post = Post.find(params[:post_id])
     favorite = current_user.favorites.find(params[:id])

     if favorite.destroy
       flash[:notice] = "Post unfavorited."
     else
       flash[:alert] = "Unfavoriting failed."
     end
       redirect_to [post.topic, post]
   end
end

Here's how I rendered it in my users/show.html.erb:

<h2>Favorites</h2>
   <%= render @user.favorites %>

   <h2>Posts</h2>
   <%= render @user.posts %>

Also tried this for users/show.html.erb:

<h2>Favorites</h2>
   <%= render partial: @user.favorites %>

Here's my favorites/_favorite.html.erb (line #1 raised issue):

<% if favorite = current_user.favorite_for(post) %>
 <%= link_to [post, favorite], class: 'btn btn-danger', method: :delete do %>
   <i class="icon ion-ios-heart"> </i>&nbsp; Unfavorite
 <% end %>
<% else %>
 <%= link_to [post, Favorite.new], class: 'btn btn-primary', method: :post do %>
   <i class="icon ion-ios-heart-outline"> </i>&nbsp; Favorite
 <% end %>
<% end %>

Edit:
Tried a migration to AddUserToFavorites but ran into a migration error upon rake db:migrate
rails g migration AddUserToFavorites user:references

If needed here's my code:
https://github.com/robSturcke/forum

Thank you for your help.

Posted in Use .ENV instead of Secrets.yml for keys

Of course, just not a fan to reset my config/secrets.yml from the commits on projects/examples. I found .ENV a very useful tool with gems like Paperclip working along with S3 but Shrine seems to add that extra step to use .ENV. I'll definitely look into how to do that Heroku credentials manually to see how it works.

Posted in Use .ENV instead of Secrets.yml for keys

Regarding around time area 3min for Direct File Uploads to S3: Part 2, I was wondering if I could just use .ENV since I'm using this as an example for my Github and Heroku. Instead of putting my keys into secrets.yml and using shrine.rb pointing to them, could I just add .ENV gem and point to my keys from my application.rb?

Or do the Rails.application.secrets.aws_secret_key... have to be in Shrine.rb in order for Shrine to work properly with S3? I found Paperclip fairly easy to use with .ENV but a little confused with Shrine.

I actually got it to work later with the direct messages video which I forgot was part of the series after switching to the public_channel I believe.

I'll check out the direct messages commit, was looking through the fixes submitted by Shakycode but I believe I added this to my method already:
https://github.com/gorails-screencasts/chatrooms/commit/595c3cbc87a1b77576af178789c3dc30ef53313c

Went back in terminal to double check, looks like things went through:

$ rails g migration AddLastReadAtToChatroomUsers last_read_at:datetime
      invoke  active_record
      create    db/migrate/20161016055506_add_last_read_at_to_chatroom_users.rb
$ rake db:migrate
== 20161016055506 AddLastReadAtToChatroomUsers: migrating =====================
-- add_column(:chatroom_users, :last_read_at, :datetime)
   -> 0.0058s
== 20161016055506 AddLastReadAtToChatroomUsers: migrated (0.0059s) ============

$ rails g channel LastRead
      create  app/channels/last_read_channel.rb
   identical  app/assets/javascripts/cable.js
      create  app/assets/javascripts/channels/last_read.coffee

That's strange, I've restarted my rails server previously and checked the migration, here's the migration contents:

class AddLastReadAtToChatroomUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :chatroom_users, :last_read_at, :datetime
  end
end

Full Error:

NoMethodError in Chatrooms#show

undefined method `last_read_at' for nil:NilClass

<% if !unread_messages && @chatroom_user.last_read_at < message.created_at %>

Oh I thought that was completed with rails g migration AddLastReadAtToChatroomUsers last_read_at:datetime

Added the following to my chatroom_users_controller but keep running into a NoMethod Error.

def create
    @chatroom_user = @chatroom.chatroom_users.where(user_id: current_user.id).first_or_create
    @chatroom_user.update(last_read_at: Time.zone.now)
    redirect_to @chatroom
  end

Just a bit confused as to what else I may be missing since I keep getting the chatrooms/show.html.erb error

<% if !unread_messages && @chatroom_user.last_read_at < message.created_at %>

So I figured out what happened, but still a very strange occurrence that has happened to me once before.

After restarting redis server, rails s, cache clearing on Chrome I decided to just delete the chatroom, as well as create a new user entirely. It worked! So my conclusion is that for some reason Chrome may hold onto certain Coffeescript (or JS) being used on localhost:3000 and I had a similar occurrence of this happen once before about year ago in a AngularJS project.

Very strange but got it working! This project has been a lot of fun, can't thank you enough for making these tutorials!

Running into a strange error, everything works perfectly fine in Safari/Firefox. However, when launching in Chrome functionality completely halts after adding notifications.coffee. Is there something I'm missing or is this a security bug on chrome's part?

Only clue on Chrome's console: page is not secure under Security tab, and won't process console.log any longer

Edit: rails s still receives information, but message_relay seems to be delayed with Chrome and no significant changes are made. Safari still receives the notifications of messages sent from the Chrome browser.

Just looking to see if a similar issue has ever been experienced, I can compromise for using a different browser and finishing the process no problem, but will definitely revisit the project to see if the same error occurs.

Not sure what to make of it, triple checked spelling etc, very strange!
https://github.com/robSturcke/halibut/blob/master/app/assets/javascripts/notifications.coffee

Got ya! Didn't realize I had to install it through brew.

Yeah I keep getting the error even after restarting rails s showing up though even with my line is now just config.action_cable.allowed_request_origins

.rvm/gems/ruby-2.3.0/gems/redis-3.3.1/lib/redis/client.rb:345:in `rescue in establish_connection': Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError)

Saw in the comments the fix for (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket) however I added the following to my development.rb:

  config.action_cable.allowed_request_origins = ['http://localhost:3000/']

Is that correct or should I be going to the following when I launch rails s:

redis://localhost:6379/1

(Sorry if this was already answered)