Lauro

Joined

6,740 Experience
24 Lessons Completed
5 Questions Solved

Activity

Chris! How can I hide a project when a user is not subscribed to a project?

Posted in How can I subscribe a user when creating a new project?

Thanks Chris! It worked. :grinning: :raised_hands: Your explanation helped a lot.

I still need to work on understanding the logic for associations.

Posted in How can I subscribe a user when creating a new project?

How can I subscribe a user when creating a new project?

I'm having a hard time on the create method. Currently this is my code:

Projects controller

  def create
    @project = Project.new(project_params)

    @project = Project.find(params[:project_id])
    current_user.subscriptions.create(project: project)

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

Project Model

class Project < ApplicationRecord

  has_many :subscriptions
    has_many :users, through: :subscriptions

end

User Model

class User < ApplicationRecord

  has_many :subscriptions
  has_many :projects, through: :subscriptions

end

Jacob thanks! ProjectThread.find(current_user.project_posts.pluck(:project_thread_id).uniq) worked!

How would would I create a private method to prevent other users from accessing Project Threads that dont belong to them?

I currently have something like this:

  def require_permission
      if current_user != ProjectThread.friendly.find(params[:id]).user
        redirect_to root_path
      end
    end

But this only works if the user is the ProjectThread owner.

And currently I have my models setup like this. it seems to be working okay.

class User < ApplicationRecord
  has_many :project_threads
  has_many :project_posts
end

class ProjectThread < ApplicationRecord
  belongs_to :user, optional: true
  has_many :project_posts
  has_many :users, through: :project_posts
end

class ProjectPost < ApplicationRecord
  belongs_to :user, optional: true
  belongs_to :project_thread, optional: true
end

A user belogns to a ProjectThread. And a ProjectThread has_many Users through ProjectPosts.

On the Project Thread Index I want to display only threads where a given users has posted a Project Post.

How would I go about doing that?

class ProjectThreadsController < ApplicationController
  def index
        @project_thread = if current_user.project_posts.any?
                           ProjectThreads.all
                      end
  end

Posted in What do you listen to while coding?

Spodify Playlist: Hacker - music for programing

https://open.spotify.com/user/wutangbifi/playlist/0Jt2JW0NTIL6MvV9dSOnqZ

Posted in has_and_belongs_to_many associations

Thanks for the help Szilard and Jacob! I was having a hard time understanding exsactly what was going on with database and structure. Chris's series on building a forum really helped. Undestanding how to strcuture nested attributes. :)

If anyone want to check it out: https://gorails.com/series/how-to-build-a-forum

Posted in has_and_belongs_to_many associations

Thanks Szilard. Yes, a Project needs to have several Users. But Im not sure how to built controllers and views to make the associations. Should I create Controllers and views to the Collaboracion Model?

Posted in has_and_belongs_to_many associations

Im building an app where a User belogns to several projects. Is this a good approach to create a "join table" that just tracks which Projects are connected to which Users? Is the Collaboration model necessary?


class User < ApplicationRecord
  has_many :collaborations
  has_many :projects, through: :collaborations
end

class Project < ApplicationRecord
  has_many :collaborations
  has_many :users, through: :collaborations

end

class Collaboration < ApplicationRecord
  belongs_to :user
  belongs_to :project
end

Thanks Jacob! I apperently did have cmake installed on my local machine but not in AWS.

Being trying to deploy my rails app using EBS. I kepp getting the following error.

I've tried updating and installing rugged. But the problem seems to be cmake on EBS. CMake is an extensible the depends from the Rugged gem.

Any ideas on how to debug this?

Im using rails 5.0.1 and Ruby 2.3.1.

-------------------------------------
/var/log/eb-commandprocessor.log
-------------------------------------
  Installing coffee-script-source 1.12.2
  Installing crass 0.2.1
  Installing orm_adapter 0.5.0
  Installing unf_ext 0.0.7.2 with native extensions
  Installing escape_utils 1.1.1 with native extensions
  Installing gemoji 2.1.0
  Installing rugged 0.25.0b10 with native extensions

  Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

      current directory: /var/app/ondeck/vendor/bundle/gems/rugged-0.25.0b10/ext/rugged
  /opt/rubies/ruby-2.3.1/bin/ruby -r ./siteconf20161231-16631-uz4bvl.rb extconf.rb
  checking for gmake... yes
  checking for cmake... no
  ERROR: CMake is required to build Rugged.
  *** extconf.rb failed ***
  Could not create Makefile due to some reason, probably lack of necessary
  libraries and/or headers.  Check the mkmf.log file for more details.  You may
  need configuration options.

  Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/opt/rubies/ruby-2.3.1/bin/$(RUBY_BASE_NAME)
    --use-system-libraries

  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

I was hable to figure out how to add the html syntax highlighting.

In turn, the actual syntax highlighting is handled by pygments.rb. Pygments.rb can generate the styles. The following generates the CSS for HTML formatted code:

❯ irb
>> require 'pygments'
=> true
>> Pygments.css('.highlight')
=> ".highlight .hll { background-color: #ffffcc }\n.highlight  { background: #f8f8f8..."

Then just copy and past the code into your css file.

Here my formated style:

.highlight .hll {
     background-color: #ffffcc
 }
.highlight {
     background: #f8f8f8;
 }
.highlight .c {
     color: #408080;
     font-style: italic
 }
 /* Comment */

.highlight .err {
     border: 1px solid #FF0000
 }
 /* Error */

.highlight .k {
     color: #35bb71;
     font-weight: bold
 }
 /* Keyword */

.highlight .o {
     color: #666666
 }
 /* Operator */

.highlight .ch {
     color: #408080;
     font-style: italic
 }
 /* Comment.Hashbang */

.highlight .cm {
     color: #408080;
     font-style: italic
 }
 /* Comment.Multiline */

.highlight .cp {
     color: #BC7A00
 }
 /* Comment.Preproc */

.highlight .cpf {
     color: #408080;
     font-style: italic
 }
 /* Comment.PreprocFile */

.highlight .c1 {
     color: #408080;
     font-style: italic
 }
 /* Comment.Single */

.highlight .cs {
     color: #408080;
     font-style: italic
 }
 /* Comment.Special */

.highlight .gd {
     color: #A00000
 }
 /* Generic.Deleted */

.highlight .ge {
     font-style: italic
 }
 /* Generic.Emph */

.highlight .gr {
     color: #FF0000
 }
 /* Generic.Error */

.highlight .gh {
     color: #000080;
     font-weight: bold
 }
 /* Generic.Heading */

.highlight .gi {
     color: #00A000
 }
 /* Generic.Inserted */

.highlight .go {
     color: #888888
 }
 /* Generic.Output */

.highlight .gp {
     color: #000080;
     font-weight: bold
 }
 /* Generic.Prompt */

.highlight .gs {
     font-weight: bold
 }
 /* Generic.Strong */

.highlight .gu {
     color: #800080;
     font-weight: bold
 }
 /* Generic.Subheading */

.highlight .gt {
     color: #0044DD
 }
 /* Generic.Traceback */

.highlight .kc {
     color: #35bb71;
     font-weight: bold
 }
 /* Keyword.Constant */

.highlight .kd {
     color: #35bb71;
     font-weight: bold
 }
 /* Keyword.Declaration */

.highlight .kn {
     color: #35bb71;
     font-weight: bold
 }
 /* Keyword.Namespace */

.highlight .kp {
     color: #35bb71
 }
 /* Keyword.Pseudo */

.highlight .kr {
     color: #35bb71;
     font-weight: bold
 }
 /* Keyword.Reserved */

.highlight .kt {
     color: #B00040
 }
 /* Keyword.Type */

.highlight .m {
     color: #666666
 }
 /* Literal.Number */

.highlight .s {
     color: #f37171
 }
 /* Literal.String */

.highlight .na {
     color: #7D9029
 }
 /* Name.Attribute */

.highlight .nb {
     color: #35bb71
 }
 /* Name.Builtin */

.highlight .nc {
     color: #367af7;
     font-weight: bold
 }
 /* Name.Class */

.highlight .no {
     color: #e2854e
 }
 /* Name.Constant */

.highlight .nd {
     color: #AA22FF
 }
 /* Name.Decorator */

.highlight .ni {
     color: #999999;
     font-weight: bold
 }
 /* Name.Entity */

.highlight .ne {
     color: #D2413A;
     font-weight: bold
 }
 /* Name.Exception */

.highlight .nf {
     color: #367af7
 }
 /* Name.Function */

.highlight .nl {
     color: #A0A000
 }
 /* Name.Label */

.highlight .nn {
     color: #367af7;
     font-weight: bold
 }
 /* Name.Namespace */

.highlight .nt {
     color: #35bb71;
     font-weight: bold
 }
 /* Name.Tag */

.highlight .nv {
     color: #19177C
 }
 /* Name.Variable */

.highlight .ow {
     color: #AA22FF;
     font-weight: bold
 }
 /* Operator.Word */

.highlight .w {
     color: #bbbbbb
 }
 /* Text.Whitespace */

.highlight .mb {
     color: #666666
 }
 /* Literal.Number.Bin */

.highlight .mf {
     color: #666666
 }
 /* Literal.Number.Float */

.highlight .mh {
     color: #666666
 }
 /* Literal.Number.Hex */

.highlight .mi {
     color: #666666
 }
 /* Literal.Number.Integer */

.highlight .mo {
     color: #666666
 }
 /* Literal.Number.Oct */

.highlight .sa {
     color: #f37171
 }
 /* Literal.String.Affix */

.highlight .sb {
     color: #f37171
 }
 /* Literal.String.Backtick */

.highlight .sc {
     color: #f37171
 }
 /* Literal.String.Char */

.highlight .dl {
     color: #f37171
 }
 /* Literal.String.Delimiter */

.highlight .sd {
     color: #f37171;
     font-style: italic
 }
 /* Literal.String.Doc */

.highlight .s2 {
     color: #f37171
 }
 /* Literal.String.Double */

.highlight .se {
     color: #BB6622;
     font-weight: bold
 }
 /* Literal.String.Escape */

.highlight .sh {
     color: #f37171
 }
 /* Literal.String.Heredoc */

.highlight .si {
     color: #BB6688;
     font-weight: bold
 }
 /* Literal.String.Interpol */

.highlight .sx {
     color: #35bb71
 }
 /* Literal.String.Other */

.highlight .sr {
     color: #BB6688
 }
 /* Literal.String.Regex */

.highlight .s1 {
     color: #f37171
 }
 /* Literal.String.Single */

.highlight .ss {
     color: #19177C
 }
 /* Literal.String.Symbol */

.highlight .bp {
     color: #35bb71
 }
 /* Name.Builtin.Pseudo */

.highlight .fm {
     color: #367af7
 }
 /* Name.Function.Magic */

.highlight .vc {
     color: #19177C
 }
 /* Name.Variable.Class */

.highlight .vg {
     color: #19177C
 }
 /* Name.Variable.Global */

.highlight .vi {
     color: #19177C
 }
 /* Name.Variable.Instance */

.highlight .vm {
     color: #19177C
 }
 /* Name.Variable.Magic */

.highlight .il {
     color: #666666
 }
 /* Literal.Number.Integer.Long */

Jay did you get html-pipeline/SyntaxHighlightFilter gem to work with Rails 5?

Posted in How to link the liked posts by each user?

That totally makes sense. Once more, thanks Chris! :)

Posted in How to link the liked posts by each user?

I just finished with the Liking Posts screencast. All good. I managed to create a User Profile. And now I'm trying to display the liked post by each user.

I currently have this inside my user/show :


<% @user.likes.each do |like| %>

  <h3> <%= link_to like.post.title %></h3>


<% end %>

Its listing each title liked by the user, but its now linking them to path of the post.

Any suggestions on how to solve this? Am I missing something inside the Like controllers?

Thanks (:

I had to add those variable into the Post Models and not the Post Controllers.

def next
    Post.where("id > ?", id).order(id: :asc).limit(1).first
end

def prev
     Post.where("id < ?", id).order(id: :desc).limit(1).first
end

In my blog application I want to have a "Previous post" link and a "Next post" link in the bottom of my show view. But I'm having a hard time getting it to work. What am I missing?

This is what I currently have:

Post Controllers

def next
    Post.where("id > ?", id).order(id: :asc).limit(1).first
end

def prev
     Post.where("id < ?", id).order(id: :desc).limit(1).first
end

Post Show


<%= link_to "Prev Post", @post.prev %>
<%= link_to "Next Post", @post.next %>