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

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!

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.

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.

In my case restarting the rails server command helped.

This isn't difficult to write yourself. And it will help you to understand a few parts of rails and routes.
Add a 'permalink' column to the db.
Do a find_by_permalink(params[:id]) in the controller
Add a def to_param method to the model and return the permalink
column
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.

Related

How to use rails generators without active record

I am building a rails app without a database.
In Disable ActiveRecord for Rails 4 I learned how to configure the app so that the absence of the database related gems does not interfere with running it. The problem is that I still want to create models using the commend rails generate mode MyModel.
Under this configuration, the above command does nothing at all.
I am assuming here I would need to require some modules (for example, activemodel, which seems to provide ActiveRecord-like capabilities without necessarily having a DB) in application.rb, but I can seem to find which.
Could someone help?
Thanks in advance
You can take a look at How to create custom generators for my rails app.
Basically you will have to change the behavior of your model generators. This way you can tell which file will be created, with which code template and etc.

Bad Controller/Model names in Rails?

On my current RoR project I have a table that stores pdf's (using paperclip gem). I've named the model 'Document' and a controller called 'documents'. This gave me a lot of weird errors, and I think it's because Rails uses those names already somewhere. From there I wondered if there's a list somewhere with names you should avoid in Rails? Does anybody know if there's something like this on the web? My searches didn't pan out... Also, let me know if you think the name of the model/controller is not the problem at all, so I can do some other checks. Thanx in advance!
The document word is probably a reserved one.
I found a long list of other reserved words here,
There's an attempt at reviving the old Rails wiki reserved words page here:
https://github.com/walterdavis/railsready/wiki

Versioned associations using vestal_versions?

I'd like to be sure if vestal_versions does support versioned associations (it seems like it doesn't) before switching out to another versioning gem that can support versioned associations e.g => has_versioning. I haven't looked at the code yet but I couldn't find anything related with versioned associations from the readme file or the issue section on github. Help would be appreciated!
(At the moment of writing this) There is an associations branch in the official vestal_versions repository, It is still a basic idea and isn't merged yet in the master branch. So I decided to go with another versioning gem, specifically acts_as_revisable following the instructions in this blog post.
I'm looking for something that appears to be very close to your needs. But I don't need to revert the associated objects, just to record them. I was thinking of handle it in a nosql way. So I can save the model version and the associations would be embedded documents.
So I can compare versions in a more comprehensive way. Right now I use paper_trail, but as it can't handle associations, it's not possible to store the tags associated to a model and see how it changes through time.

AJAX Rails Validation

I have my form and validation working great with regular http requests. I would like it to use AJAX. I know i can validate on the client side but that seems redundant given that I have defined the validations in my model.
As the user is filling out the form, I'd like to give feedback to them on their entries. What is the best way to use the rails defined validations in an AJAX form and give live feedback?
Check out the live-validations plugin. There's also an introductory screencast.
For Rails 3 check out Client Side Validations: https://github.com/bcardarella/client_side_validations
Here's the railscast: http://railscasts.com/episodes/263-client-side-validations
Live-validations was kinda messy to get working for me, so I started with my own solution from scratch backed by Validatious. It's actually really DRY because of the Rails conventions in the back that made it possible to do a lot of smart assumptions. In most cases, all you need is to include a few javascript dependencies and declare your validations in your models as always - and it all just works! =) Peep the README for details.
Here it is:
http://github.com/grimen/validatious-on-rails
If you're looking for a solution to this that does not introduce any plugin dependencies check out my screencast on the issue:
AJAX Validations on Rails 2.3.8
https://github.com/augustl/live-validations/wiki has installation instructions.
When you add LiveValidations.use :jquery_validations to the bottom of your environment.rb, make sure it is outside of the Rails::Initializer block.

Acts_as_paranoid, is_paranoid... Alternatives?

I'm looking for a rails plugin/gem which brings the functionality of marking an ActiveRecord-Model deleted, instead of deleteing it.
Does anybody know, what gems or plugins are up to date? (AAP is out-dated and is_paranoid doesn't appear to be used by the community).
Do you know alternatives?
It seems even the authors of both acts_as_paranoid and is_paranoid aren't using their respective plugins/gems any more. Both are using named scopes.
Yeah, it's not automagic or anything, but sometimes being explicit about your intentions is a good thing.
For completeness, here is a more recent gem for this purpose:
Paranoia - acts_as_paranoid for Rails 3
https://github.com/radar/paranoia
And another:
https://github.com/JackDanger/permanent_records
is_paranoid doesn't appear to be used by the community..
http://chadfowler.com/blog/2009/07/08/how-ruby-mixins-work-with-inheritance/ - Just a blog post the other day talking about it. Seems like it solved Chad's problem just fine (as well as lead him to write a post about inheritance and mixins).
How about you just have a valid:boolean column/attribute and set it to false when you wish to soft-delete the model? Or am I missing something?

Resources