I have a User model that has many galleries and categories(both separate models that belong_to :user). I would like to create a default category named "Everything" and a default gallery entitled "Everything".
I am adapting Mike Hartl's tutorial for my app, specifically the User creation and activation part. So, after a user is created using this code:
User.rb
def create
@user = User.new(user_params)
if @user.save
@user.send_activation_email
flash[:info] = 'Please check your email to activate your account.'
redirect_to root_url
else
render 'new'
end
end
An activation email is sent to the User and if they click on the link it will activate their account. At the point of activation, so the activated
column switches from false
to true
, I would like the "Everything" gallery and category to be created. In my mind that is the most logical place for this to happen because it is likely to only be triggered once. That way I dont' have to worry about an "Everything" category and gallery trying to be created everytime someone logs on to their account.
The way I am trying to create these is by inserting the following code immediately after the account is activated:
user.categories = user.categories.build(name: 'Everything')
So it looks like, this:
account_activations_controller.rb
class AccountActivationsController < ApplicationController
def edit
user = User.find_by(email: params[:email])
if user && !user.activated? && user.authenticated?(:activation, params[:id])
user.activate
user.categories = user.categories.build(name: 'Everything')
log_in user
flash[:success] = 'Account activated!'
redirect_to user
else
flash[:danger] = 'Invalid activation link'
redirect_to root_url
end
end
end
My problem is that while the User is created, the "Everything" category is not and I get the following error, which I can't reconcile:
NoMethodError at /account_activations/0hbg0T33tjQlleiwKqvUDg/edit
undefined method `each' for #<Category:0x00000004f95b60>
I don't know where 'each' would be called in this process.
Though I mentioned creating a gallery and category and user activation, the above code only address category. It seems to me that if I can get a category to work I should be able to get the gallery to work using a similar solution. This is what my create action looks like for category:
def new
@category = current_user.categories.build
end
def create
@category = current_user.categories.create(category_params)
if @category.save!
respond_to do |format|
format.html
format.js
end
end
end
I have a couple questions related to my issue:
The first is the the most obvious, why am I getting the nomethod error, there doesn't appear to be an 'each' method associated with categories?
Am I approaching this issue correctly? Should I trigger the creation of my default category and gallery from somewhere else in the code? I've read a bit about a
.change?
that I tried to use on theactivated
column on User, but I couln't figure out how to use it.
Any help you can offer is greatly appreciated.