modify single user info in rails - ruby-on-rails

I'm using devise. I modified the user fields with no problem. I need to access certain users, and modify their info. For example, let's say that the user "pete" should have the field "type" changed to "active". How do I do that? it should be just accessing the database and modify that field, but I can't make it work. I mean, there are tools to modify databases, but what is the rails way to do it?
I don't need a tool or script or modify the code, it's a small database and very few changes. Once I understand this, I may do something more general.

The typical rails way to operate on your data like this is to write custom rake tasks. The great Ryan Bates has a great screencast on the topic as well: http://railscasts.com/episodes/66-custom-rake-tasks.
If you do not want to do that, and the fixes you are making are really small, then you can do them via the rails console. More on that here: http://railsonedge.blogspot.in/2008/05/intro-to-rails-console.html

If you need the actual code of how to do that, it may look like this:
pete = User.find_by_name('pete')
pete.update_attributes(:type, 'active')

Related

Ruby on Rails database workflow

I'm a bit of a Rails beginner, so I'm sorry if this question is easy. I'm wondering how I am supposed to modify the Rails database as a developer without letting users of the site modify data.
More information:
Let's say I am trying to create a website that lists books along with their information. I want users to be able to see these books, but not add/delete them. As a developer, I want a way to add books without using the command line (hard to edit mistakes). What's the easiest way for me to do this? Would there be any differences between the development database and a live hosted one?
I think there are a few approaches to do this.
different user roles. This technically breaks the rule of without letting users of the site modify data, but being able to differentiate between normal and admin users means you can limit who actually can add data into the database. I used cancancan as a way to authorize requests but I know there are others.
I agree doing it using the command line isn't ideal, but rails do support rake tasks. You can create a task that will handle most of the logic and all you need to do is something like
rake create_book["name here"]
This will make the process less error-prone.
Create data using rails migrations. Rails can generate the skeleton file for you, and you just ran any ActiveRecord methods. However, the main purpose of migration is to update the database schema, but they can seed the database as well. here's the example from the dcos
Would there be any differences between the development database and a live-hosted one?
Yea, they should be totally separate database instances. You don't want to have your development database be the same as the live one. This would cause major problems. Rails have a concept of environments where you can use different configurations, so you can pick and choose what database URL to use.
like #davidhu said here The best approach really is the use of authorization. If only admin can see a page to CRUD the books then you don't have to worry about normal users doing same plus it makes it easy for you as the admin to make changes or add to the collection. Add the gem (or reinvent the wheel) then Rails will take care of the rest for you.

Access Control List in ActiveAdmin?

I was wondering if there is any gem that implements an active admin control list in ActiveAdmin?
If none, what's the best way or approach to simply do this?
Reading up on pundit and still active admin whether or not I should really write one from scratch.
Thanks!
Edit:
I currently worked on this yesterday and have my access control list together with my group model. the form looks roughly something like this:
its rendered partial in my activeadmin group.rb
So yeah I guess the correct word is managing permissions on my activeadmin. I'm reading up if there is any way I can integrate activeadmin roles in my current setup. I'm kinda seeing this kind of setup is tedious?
I made it like this because eventually if there needs to be a lot of different roles, they don't have to ask the devs to code it everytime.
I think you might be looking for ActiveAdminRole

Rails Active_Admin VS. my own backend

I've been thinking of writing my own backend, because I feel active_admin might not support all the requirements.
I wanted to ask if Active_Admin supports any of these just to be sure:
I have a has_and_belongs_to_many relationship between my ad model
and tag model. In the new ad page I would like to have the form for
the ads, as well as all available tags so the admin can choose which
tags to associate with the ad. I was able to do that normally in my
application, but can I do that with active_admin?
Can I add custom buttons.. Like one to convert to PDF for example,
or one to send an e-mail..
Could I add some sort of before_filter, so the admin can only view a
model, but not edit or delete it for example?
Thank you.
All of those things can be done via Active Admin, but as it was pointed out, it can be quite a nightmare actually implementing certain things depending on the amount of flexibility you need it to have. For that exact reason, I decided to start rolling my own administration panels.
I have tried an implemented almost all robust gems for admin panels. I have also sweated over several hand-made ones.
Active-Admin is very usability centred, but it is not configuration centred.
As you rightly aniticipated, some of the more complex modifications can be tedious.
In my experience, rails_admin is the best middle ground I could find.
Take a look at it, it is highly functional, completely modular (made as a Rails 3 Engine) and simpler to modify.
If you can live without some details when customizing this is definitely the way to go. However, if you need to have everything just right, then there is not substitute for hand-made.

Create rails form to modify database values - but not with a model

I'm trying to create a universal settings page for my rails app and I have no idea where to start. I know there are several gems that make it simple, and I've read the docs. They enable you to do something like Settings.color = "red" and your settings for color will be red! But, how do I turn this into a form, connected to the database, that the users can then change the values?
Ideally, we need a few settings, I know one of them will be a select box where they have numerous options but only one can be chosen at a time (think Active, Suspended, Disabled, etc).
This isn't really something that a model should be used for because its not a thing, right? I'm totally lost. I can make a database table, but without a model or a controller I have no idea how to just "make a form" that saves into the database - and then how do I get those values out?
Edit: The plug ins I'm referring to are similar to https://github.com/Squeegy/rails-settings and its variants. They show you how to hard code settings which is great, but don't go into how to create a form or any of the back end stuff to make it work. It's not a model so I'm totally clueless here. There's no scaffolding to work off of.
rails-settings is model based, as the readme specifically states that.
So you'd just do as per any other model-based form.

Rails "Badge" type plugin / tutorial?

Does anyone know if there is a Rails gem/plugin/tutorial that shows how to create a Badge/Achievement system similar to what stackoverflow uses.
Thanks.
You might also want to try the achievements gem: https://github.com/mrb/achievements
It's based on Redis, so you'll need to get that working first. Basically, you define a bunch of achievement contexts (pages viewed, messages sent, etc.) along with multiple levels if necessary. Then, you increment your value appropriately upon certain events, and you can then check if the achievement has been reached.
This link also has a relatively detailed explanation of the thinking behind a badge/achievement system: RoR Achievement System - Polymorphic Association & Design Issues
check out https://github.com/paulca/paths_of_glory
I think it's less a framework but a design question. If you know how to build it in an object-oriented way, you'll eventually know how to build it in Rails too.
If you're a Rails newbie, check out the Rails Guide on "Active Record Associations" and try to identify the models and the associations of your "badge/achievment system".
Besides that: No, I don't know of any turnkey-gem/plugin/tutorial that would help you build such a system.
There is also Gioco, which I haven't yet tried:
http://joaomdmoura.github.io/gioco/

Resources