Ariff Munshi

Joined

11,280 Experience
99 Lessons Completed
1 Question Solved

Activity

Hey Simon,

Thanks for your answer! Never actually thought of it that way. Come to think of it, it makes the most sense. Cheers!

Posted in Devise Secret Key was not set automatically

Opps my bad. Realised I didn't have the master.key in my cloned system. Added it all and all works with Devise 4.4.3. No edits needed.
Hey Chris, just wondering, have you designed the categories here in the forums to be its own Category model or a column list?

Posted in Devise Secret Key was not set automatically

Hey, I'm running into the same problem. It works on my main computer on a app i just started working on this week (Rails 5.2.0, Devise 4.4.3, Ruby 2.5.1) but when I cloned the repo onto my home computer, I am getting this error.

Running 

Rails.application.credentials.secret_key_base

Rails.application.secrets.secret_key_base

Rails.application.config.secret_key_base

They all return NIL. Should I be setting them manually now?
Hey guys, 

I have a question on database design. Say I have a Post model that has/belongs to a category (out of about 20 categories or so). I am thinking of two options that have their own merits.

1. Category as own resource with Post belong_to it
- this allows listing of all categories and posts under a category easy
- however, entries will most probably be done once at the start and then left to be

hence the other option

2. Categories as an attribute of Post
- scoping Post by categories is trivial still
- categories can be picked from a constant list

Would love to hear any of your comments on this how would you go about doing it.

Cheers!

Managed to solve it after much tinkering. If anyone has the same problem, I called a before_validation to associate the message with the project in user.rb.

private
    def set_project
      self.messages.first.project = self.projects.first
    end

Also changed messages to be a nested attribtue to user from projects

Hi,

Been going round in circles for a day or so but still can't seem to find a solution for this. I have project management chat app that on sign up, you create the project and the first chat message too. Here is my code:

user.rb

class User < ApplicationRecord
  has_many :projects, inverse_of: :user, dependent: :destroy
  has_many :messages, as: :message_owner, dependent: :destroy

  accepts_nested_attributes_for :projects

  validates :email, presence: true
  validates :password, presence: true
  validates_associated :projects
end

project.rb

class Project < ApplicationRecord

  has_many   :messages, inverse_of: :project, dependent: :destroy
  belongs_to :user, inverse_of: :projects

  accepts_nested_attributes_for :messages

  validates :name, presence: true
end

message.rb

class Message < ApplicationRecord
  belongs_to :project, inverse_of: :messages
  belongs_to :message_owner, polymorphic: true

  default_scope { order(:created_at) }

  validates :body, presence: true
end

registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  def new
    build_resource({})
    self.resource.build_user_profile
    project = self.resource.projects.new
    project.messages.new
    respond_with self.resource
  end

  def create
    super
  end

  private
    def sign_up_params
      params.require(resource_name).permit(:email,
                                            { user_profile_attributes: [:name],
                                              projects_attributes: [:name, :material, messages_attributes: [:body]]
                                            })
    end
end

new.html.haml

  = form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
    / Fields for project
    = f.fields_for :projects do |project|
      .field
        = project.label :name, "Project Name"
        = project.text_field :name
      / Fields for message
      = project.fields_for :messages do |message|
        .fields
          = message.label :body, "Your Message"
          = message.text_area :body
    / Fields for user registration
    .field
      = f.label :email
      = f.email_field :email
    .actions
      = f.submit "Sign up"

I get the error that there is no associated message_owner (user). I know that this is because the fields_for is with respect to the project and has no knowledge of the user.

The problem comes as I can only associate the message with either one (project) of the other (user) but not both. I have tried creating messages as a nested attribute of user but this results in the problem of no associated project. I've tried initialising the message with a user in the registrations_controller#new but this does not seem to register either.

Could this be solved with a before_save callback in message.rb? But this will need to ensure that both the user and project are already created.

Hope someone can help. Thanks!

Posted in Group Chat with ActionCable: Part 1 Discussion

Thanks Chris!

Posted in Group Chat with ActionCable: Part 1 Discussion

Hey has this up yet? @excid3:disqus

Posted in Using ActiveAdmin to Build an Admin UI Discussion

Hey @Marc Gayle you should be safe by now. It was back for 0.1.5.

You can check it out here: https://blog.sourceclear.co...

Posted in Using ActiveAdmin to Build an Admin UI Discussion

Thanks! Will most probably go with that then. Nothing too heavy handed. Had some experience in Active Admin for a project I helped in.. just felt too heavy.

Posted in Using ActiveAdmin to Build an Admin UI Discussion

Hey @excid3:disqus, thanks for the vid. How would you rate Active Admin vs Administrate which you covered in Episode 94? I'm looking for something lightweight for one of my bigger apps.

By the way for those reading this. There's been a vulnerability issue reported for the Rails_admin and Administrate gems for part versions. So be sure to update them if you're using those.

See:
https://blog.sourceclear.co...

Posted in Jekyll, MiddleMan or CMS for a blog site?

Thanks Alan for the tip! Yea Jekyll looks great especially with MD. Didn't know about the guest post feature. Will definitely look into it!

Posted in Jekyll, MiddleMan or CMS for a blog site?

Hi guys,

I've build quite a few production apps now with rails and I'm embarking on my latest project which is a niche blog site. I guess going with rails is a bit of an overkill with this one so I'm looking at some of the other options out there.

For static blog sites, Jekyll and Middleman seems to be leading the pack. Any advise from anyone who have tried either of this? For now it's a blog with info articles and a tagging and search system but I'm also considering to add forums and guest posting if there is enough viewership.

Besides these 2, has anyone worked with CMS systems like Radiant etc. How does it compare to just using Rails directly. I've looked at some of these but will at the setup involved, I end up just using Rails.

Any advice from experience would be great. Thanks!

Hey came across this thread as I was facing the same problem. I tried to stay away from them gems initially and downloaded Sweet Alerts 2 from source. All worked well until I had to the confirmation alert with a POST request callback after confirmation. As Chris mentioned, you will have to write your own Javascript and looking at the rails_ujs source, it required quite a bit of work.

Went the gem way and installed sweet-alerts and sweet-alerts-confirm. Did not work.

So here is my solution adapted from http://thelazylog.com/custom-dialog-for-data-confirm-in-rails/.

Basically overwrites the allowAction method call.

//Override the default confirm dialog by rails
$.rails.allowAction = function(link){
  if (link.data("confirm") == undefined){
    return true;
  }
  $.rails.showConfirmationDialog(link);
  return false;
}

//User click confirm button
$.rails.confirmed = function(link){
  link.data("confirm", null);
  link.trigger("click.rails");
}

//Display the confirmation dialog
$.rails.showConfirmationDialog = function(link){
  var message = link.data("confirm");
  swal({
    title: message,
    type: 'warning',
    confirmButtonText: 'Sure',
    confirmButtonColor: '#2acbb3',
    showCancelButton: true
  }).then(function(e){
    $.rails.confirmed(link);
  });
};

Posted in An other deployment question

Came across the same issue last night but i was deploying using dokku. I had previously deployed on another app following what Chris had wrote about and it worked well.

I suspect that the issue is with me not tracking my secrets.yml file with git. I'll try it again with the secrets file tracked as well as reverting to env variables. Will let you know if it works then.

Posted in In-App Navbar Notifications Discussion

Thanks @excid3:disqus! Works great!

Hey Chris! You're a beauty! After all that head banging it just took one change. Thank you so much!

Hey Chris, another question. My app is now using ajax authentication with devise. Upon authentication my js file updates some of the partials and also adds in the notifications ui elements. However I am not able to call the javascript to execute. Not too sure how this class/prototype thing works. Tried calling "new Notifications" but no no avail.

Hope you can help.

Cheers.

Posted in In-App Navbar Notifications Discussion

Hey Chris Oliver thanks again for another awesome tutorial! If noticed a bug after implementing this. If a user does an action and then the user is destroyed later, this will raise a error when the receiver loads his notifications. For example, it will give an 'undefined method for nil:NilClass' when loading the notifications as the user no longer exist.

I guess the question is how more on polymorphic associations and how do I implement a dependent destroy for the notifications. I tried:

has_many :notifications, foreign_key: :receiver_id, dependent: :destroy
has_many :notifications, foreign_key: :actor_id, dependent: :destroy

but both won't work.

Is there a quick way to implement this or can I just do an after delete callback to remove all notifications?

Cheers!

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.