Adventures of a Rails Apprentice – Part 3
Adventures of a Rails Apprentice – Part 3
Join the DZone community and get the full member experience.Join For Free
In part 2 I got a very basic Rails app working.In this post, I’ll explain the steps I took next, to continue fleshing out this simple app.
There is no escaping from data. So now that I had a simple app working, the next step was going to be to create at least one model.
The reason for that, apart from the obvious, is that ActiveAdmin needs something to work with when it’s added to an existing app.
Creating a Model
In Terminal then, we can use a generator to create a model:
rails g model Article title:string blurb:string body:text
Rails will then create the required files. Next, you do:
And the articles table will be created. Now for ActiveAdmin.
Installing ActiveAdmin For Use With Rails
The first step is to add
gem 'activeadmin' and if you are using Rails version 3.1 or above, you will also need:
gem "meta_search", '>= 1.1.0.pre' added to your Gemfile.
Then, in Terminal (in your project folder obviously – don’t forget that part. I never have…) run
Now that’s done, you can jump back on Terminal and do:
rails generate active_admin:install
Through partially covered eyes I watched ActiveAdmin install itself. When it completes, you are advised to run:
Assuming everything has worked, you can go to
http://localhost:3000/admin and login in with: “email@example.com” and “password “. That appears to be all there is to getting ActiveAdmin set up. The next thing I did was to create my own admin account and delete the default one. It’s very easy to do, just click on the
Admin Users menu item.
Registering a Model
Now that we have ActiveAdmin set up and running, the next thing to do is register a model. I figured the that since I’ve already created an
Article model, I’d register that one. Back to the Terminal:
rails generate active_admin:resource Article
I was assuming that by registering the model, we’d get a few nice options for working with our data:
Rails with ActiveAdmin
And so we did. Now I have an interface for creating articles. It does open up a flurry of questions though. For example, I’ve realised that I need a
field in the database. How do I add that? And will ActiveAdmin pick up the change?
Changing a Model
It turns out that you have to run a new migration to add a new field. Unlike with a PHP app where you would just add the new field, and change your queries accordingly.
I think the Rails way is better because you are after all, affecting the design of the app.
Anyway, here is what I ran in Terminal:
rails g migration AddStatusColumnToArticles status:string
Now, here is the really cool thing. After I restarted the server, ActiveAdmin had automatically picked up the change to the model. All the forms were updated, and the filters. Brilliant.
Now I can add a test article…
Hold on though, there is a problem:
Can't mass-assign protected attributes: status
When I tried to save a new article I got the above message. The error is actually quite useful. It tells me that there is something preventing the
status field from being used.
It boils down to the
attr_accessible property in the model. I need to add the status field to it, to make the field accessible:
attr_accessible :blurb, :body, :title, :status
I refreshed my browser, and the article saved as expected. Now I have another issue though. How do I get say, a list of articles to display on the home page?
Displaying a List of Articles
index action of the drafts controller is where I needed to call my data from. I could use something as simple as:
@articles = Article.all
But I don’t want to list all the articles, just say the top 5. I also want to apply a sort. After a bit of digging around I came up with:
@articles = Article.limit(5).order("created_at DESC").all
So now, in the
index.html.erb view I can add:
<h2>Recent Drafts</h2> <table class="table table-bordered"> <% @articles.each do |article| %> <tr> <td><%= article.title %></td> </tr> <tr> <td><em>Posted: <%= article.created_at.strftime('%a %d %B %Y - %H:%M') %></em></td> </tr> <tr> <td><a href="drafts/read_article/<%= article.id%>">Read Article</a></td> </tr> <% end %> </table>
And Now For The Mistakes…
Yes, I’m aware that I’ve made a mistake in the implementation here. Not a major, unfixable one though. ActiveAdmin is still going to work just fine. Progress has been made, so in the next part I’ll get to fixing the problem.
Published at DZone with permission of Andy Hawthorne , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.