Philip Benton

Joined

940 Experience
7 Lessons Completed
0 Questions Solved

Activity

Posted in Check a user is author of post before edit or delete

Thanks Jacob, that makes more sense.

Posted in Check a user is author of post before edit or delete

Hi all,

What is the best way to check a user (current_user) owns a post before edit or delete?

I have a Post model with a has_one relationship to the User model, user_id is set. The logged in user is stored within current_user.

Is it as simple as writing a method such as:

def is_author?
    redirect_to root_path unless @post.user == current_user
end

and using a before_action:

before_action :is_author?, only: [:edit, :update, :destroy]

I've just been looking to add this to my app on a User model. Couple of questions:

  1. Can nested data be stored / accessed? If not what would be a better way to handle that?
  2. Does SQLite & MySQL support the JSON column type?

Posted in Rails 5.2 Credentials

I've just successfully added an admin user to credentials and referenced it in my seed.rb file so I guess adding anything to it will work the same.

Posted in Rails 5.2 Credentials

Me again :)

Question about the new credentials in 5.2...

I've upgraded to 5.2 and actually really like credentials but wondering. Is it now the place to store all sensitive information including database access, etc?

Currently I store db access within bash environment variables. It seems now that the only thing needed in the bash file is the RAILS_MASTER_KEY

Posted in Bundle Package and Deployment

Chris, you beat me to it, from digging into the migration tasks I found the rake helper and did this:

desc 'Runs rake db:seed'
task :seed do
on roles :all do
within current_path do
execute :rake, 'db:seed RAILS_ENV=production'
end
end
end

I need to tidy it up to fetch the environment as per your example.

Also, I did find that when capistrano runs bundler:install it appears to be installed gems into the shared/bundle folder:

~/.rvm/bin/rvm 2.5.0 do bundle install --path /var/www/deploy/APP/shared/bundle --without deveā€¦

Posted in Bundle Package and Deployment

Thanks Thomas, yes it makes sense. I'm using it essentialy to create my user account. I'm only building a simple blog for myself at this point and don't want to allow registrations so only 1 user is required.

I'd like to see if I can write a task to do it, I'll probably remove the task but for learning purposes I'd like to write one.

Having trouble though, maybe you can help. Running RAILS_ENV=production bundle exec rake db:seed within the 'current' directory works fine but it I write a task such as:

desc "reload the database with seed data"
task :seed do
on roles :all do
within current_path do
execute "RAILS_ENV=production bundle exec rake db:seed"
end
end
end

I'm getting the error: bash: bundle: command not found

I'm not sure why.

Posted in Bundle Package and Deployment

This has also helped answer another question I had, I wasn't able to run rake db:seed but running RAILS_ENV=production bundle exec rake db:seed worked a treat.

Now to work out how to write a Capistrano task to run that.

Posted in Bundle Package and Deployment

I guess what I am saying is, when deploying gems aren't being installed in the same fashion as on the development machine.

I use RVM on both development and production. In development the gems are installed into .rvm/....

I presumed when running capistrano it would run bundle install which would install the gems into .rvm/.... on the production server. This doesn't seem to be the case.

Posted in Bundle Package and Deployment

Thanks for the answers guys (although) a little over my head I'm afraid, so I'll dig around.

Everything is working fine (except the console side of things, but I think your answer Thomas is getting me a step closer to that working).

I was just wondering how the Rails app was running because as far as I can tell my gems are just in vendor/cache and nowhere else. I'm presuming Rails references them from there somehow?

Thomas, yes I am using 'capistrano-bundler' and it is in my Capfile.

My linked_dirs line is actually commented out: append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"

Chris, I do have a bundle folder in my shared folder on the server which then contains ruby/2.5.0/gems/...

If working in development, i.e on your local machine you don't need the secrets or other environment variables set within your bash_profile.

As the error states, you can set it within the config/secrets.yml file:

development:
secret_key_base: my-secret-key

The way environment variables work in the production environment would be...

You'd have this in your config/secrets.yml file:

production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

And in your bash_profile on your production server you'd have the line:

export SECRET_KEY_BASE="my-secret-key-here"

So, when rails reads the secrets.yml file you're basically saying "you can find my secret key in the environment variable SECRET_KEY_BASE"

Posted in Bundle Package and Deployment

Hi all,

New user to the Gorails site, what a fantastic resource! I'll be upgrading to Pro shortly.

I have a first question (probably of many)...

I am deploying my app with Capistrano and it works perfectly, I'm very happy with the setup. However, I'm unsure of how Bundle / Gems are working on the production server.

I ran Bundle Package on my development machine when I first deployed and so gems are in Vendor/Cache. Now everytime I deploy gems don't seem to get installed on the production server, which is fine but I wondered if someone can tell me in more detail how Bundle Package works and how the production app is referencing the bundled gems?

For example, if I login to the production server and try running rails c, I receive the "The program 'rails' is currently not installed." message. So my app must be using gems within the vendor/cache folder.

Thanks.