Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

Can't use "acts_as_url" in ruby on rails 3

I'm newb and I'm sorry because of my dumb question! Please, help me!! I'm working with Rails 3, and this my problem:

I have a model name: Photo using gem 'mongoid'.

I want to make a permalinks which are readable url instead of unreadable '_id' generated from mongoid!

After searching in google, I found a gem called 'stringex'! I decided to use this gem, and put this line in my Gemfile:

gem 'stringex'

then ran "bundle install' to use it.

I just do everything following the guide in github Readme_rsl/stringex but the trouble occurs:

undefined method `acts_as_url' for Photo:Class

Is it because ROR 3 doesn't support this gem? Or I missed something? Please, I need help!

Upvotes: 0

Views: 1224

Answers (4)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

This isn't difficult to write yourself. And it will help you to understand a few parts of rails and routes.

  1. Add a 'permalink' column to the db.
  2. Do a find_by_permalink(params[:id]) in the controller
  3. Add a def to_param method to the model and return the permalink column
  4. Add a create_permalink before_validations method. Generate the permalink however you like and store it in the db.

The only trick is ensuring uniqueness and handling name (and url) changes. stringex looks like it helps with uniqueness, not with name changes.

There are other gems to help with name changes (aliasing and redirecting if there's a new name) if you care about that. You can handle that in different ways.

Upvotes: 0

juanpaco
juanpaco

Reputation: 6433

This is probably not the answer you're looking for, but I think the answer is "you can't do that." At least not currently.

The gem you mention is intimately tied to ActiveRecord (see this issue ticket). If you look in the stringex source code in lib/stringex.rb, you can see that acts_as_url is only included on ActiveRecord and not on Mongoid.

What's happening is that you're using Mongoid on your model, and the nice acts_as_url methods are not attached to the Mongoid::Document. It MAY be as simple as just modifying lib/stringex.rb to also include acts_as_url on Mongoid, though I assume that if it were that simple it would already have been done.

So where does that leave you? There are other ways to generate slugs. I haven't used any of them, so I can't speak to which ones are good or not. Googling "mongoid slug generation" can hopefully point you in the right direction.

Upvotes: 2

szeryf
szeryf

Reputation: 3387

In my case restarting the rails server command helped.

Upvotes: 0

jplaza
jplaza

Reputation: 276

Your model needs to have a url attribute to store the nice url in the database for later use.

I don't know why the don't mention that on the README. I found that out after reading a couple of articles.

Upvotes: 0

Related Questions