Posts Tagged resource_controller

“resource libraries” polemic!

I’ve been off for a while but I’m planning to come back now that I resolved most of my personal and professional life issues that were not letting any time for me to write here.

I’ve been fond of Resource Libraries since I discovered resource_controller devolved by James Golick.

I’ve read some good stuff about ResourceLogic by Ben Johnson (the creator of Authlogic).

More recently emerged Inherited Resources by José Valim that claims to resolve many issues that resource_controller and ResourceLogic had.

I did not have time to study Inherited Resources or even to use it yet, but I suggest that you read the reasons why Ben Johnson decided to discontinue ResourceLogic development.

My personal opinion is: I’ll still use Resource Libraries when it is appropriate. Like everything, you must always ask yourself if it is pertinent to use it for this particular task, piece of code, controller, application, plugin, gem…

, , ,

No Comments

TDD on Rails #4: RSpec, Authlogic, Factory_girl and resource_controller

So well, I decided to use RSpec, Authlogic, Factory_Girl and resource_controller on my new application! But I’ve encountered some problems making all these working together. I mean I did not find any good tutorial showing how to do it step by step, so there it is!

Read the rest of this entry »

, , , , ,

23 Comments

Namespaced resource generation with resource_controller!

Most of the time, in my projects, I need a separated administration area: http://teste.local/admin/*

For those who doesn’t know resource_controller (r_c) yet, please visit: Restful Controllers with resource_controller. I’ll never have a Rails app not using it, that’s for sure!

The goal here is to create a resource called Post and access it using http://teste.local/admin/posts .
My first thought was to try:

1
script/generate scaffold_resource admin/Posts title:string content:text

But it created things in a way I did not like:

  • app/models/admin/posts.rb ( I really do not want my model to be in the admin folder)
  • db/migrate/20090119032314_create_admin_posts.rb / create_table :admin_posts (In fact it created a admin_posts table … :-( )

I then resigned myself to the fact that I’ll have to generate in the “normal” way and then hack the generated files to adapt to what I wanted to do. Here we go:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
script/generate scaffold_resource Posts title:string content:text
exists  app/models/
exists  app/controllers/
exists  app/helpers/
create  app/views/posts
exists  test/functional/
exists  test/unit/
create  app/views/posts/index.html.erb
create  app/views/posts/show.html.erb
create  app/views/posts/new.html.erb
create  app/views/posts/edit.html.erb
create  app/views/posts/_form.html.erb
create  app/models/posts.rb
create  app/controllers/posts_controller.rb
create  app/helpers/posts_helper.rb
create  test/functional/posts_controller_test.rb
create  test/unit/posts_test.rb
create  test/fixtures/posts.yml
create  db/migrate
create  db/migrate/20090119032942_create_posts.rb
route  map.resources :posts

I then changed the following:

1
2
3
4
app/views/posts                            to    app/views/admin/posts
app/controllers/posts_controller.rb        to    app/controllers/admin/posts_controller.rb
app/helpers/posts_helper.rb                to    app/helpers/admin/posts_helper.rb
test/functional/posts_controller_test.rb   to    test/functional/admin/posts_controller_test.rb

Don’t forget to change add Admin:: in all the .rb files that you moved.

Last thing to change is the route to the Post resource:

1
2
3
4
5
map.resources :posts
#Has to be replaced by:
map.namespace :admin do |admin|
admin.resources :posts      #Directs /admin/posts/* to Admin::PostsController (app/controllers/admin/posts_controller.rb)
end

Namespaces configuration is pretty straight forward and there is loads of articles and websites explaining it. So if you’re confused about this, google it! ;-)

That should do it. Now you can access http://teste.local/admin/posts

,

2 Comments

Reserved Words!

First thought:

Have a look at the Rails reserved words: Rails Reserved Words

On the page there is reserved words and some that can cause problems. I had a nasty waste of time trying to use resource_controller to create a Template resource. Template is not a reserved word, but you can’t have a Create action for a template controller…

, , ,

No Comments