Bad Controller/Model names in Rails? - ruby-on-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

Related

How to localize existing content in Rails 3?

I have set up a simple Rails app based on Post, Comment example from the default documentation. -> http://edgeguides.rubyonrails.org/getting_started.html
What would be the appropriate way of localizing existing content from these Posts ?
If I assume that Post has a :title and "content, and I would like this content to be entered not only in English, but also in German and Dutch, then how would I achieve this if I want the end result to look similar to this :
http://0.0.0.0:3000/en/posts.json to get all posts in English and respectively,
http://0.0.0.0:3000/de/posts.json for German?
I havent found a gem yet, that would support such multilangual content.
I have seen doe, many existing ways to do so, mostly in applications like Wordpress or Drupal, which are not rails based.
I guess the preferred way would be either storing EN and DE posts in seperate tables (but then how to add new languages?), or to place all translations within the same database table, but seperate translations in a different way, but how to get them easily out in json in the right language?
I am really confused and puzzled why such a "common" problem, is not solved in Rails by default, or maybe I am simply looking in the wrong place.
Globalize3 will do what you want to do. It stores all translations for a particular model in a separate table, with a locale column identifying what the language of a particular translation is. This makes it possible to add new languages without having to migrate the database, so the approach is very scalable.
There are other gems for translating content as well, e.g. Traco is one that I know of, but this one stores the translations in the same table as the model, so when you add new languages you have to migrate each model table. I personally prefer the Globalize3 approach, although it makes the gem itself more complicated internally.

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!
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.

Is model a reserved keyword in rails?

Does anyone know if defining a rails model called "model" can be an issue?
The link that was given in a previous question seems to be broken (http://wiki.rubyonrails.org/rails/pages/ReservedWords)
Thanks!
I found these links:
http://oldwiki.rubyonrails.org/rails/pages/ReservedWords
http://www.yup.com/articles/2007/01/31/no-reservations-about-keywords-in-ruby-on-rails
And some others that weren't as good, and none of them state Model is a keyword.
I also just tried it out. All I did was make a scaffold and then tried out actions on the website, and everything went fine.

Good approach to a tagging system in Rails 3?

I'm trying to add tagging to my rails app - I'd rather not use a plugin/gem for this though.
My needs are pretty simple:
Users can add multiple tags to a single item, they can search for items by those tags, and they can see a page which lists all the tags they've used before.
What I'm wondering is, whats the best way to go about this? An array/hash in the model being tagged? A join table?
Any suggestions would be highly appreciated.
I would use a join table between the tags and the taggableItems. Then with a before_create you could check if the tag's are already in the system and create them if they aren't in the system. Next you could use searchlogic (i think there is a rails 3 branch on github) for easy tag/taggableItems searching. And it would be nice to give the user some feedback of the available tags with some autocompletion or a short list of most used tags but that is up to you/the design.
Maybe this helps you: Best Rails Tagging Plugin/Gem

rails project help

i am trying to get my head around the best way to develop an app in ruby on rails
i have a ducument creation system that for each document has multiple associations ie text docs, images, optional accessories etc. I have created this system now to do CRUD.
The next thing i want to do is have each document able to be translated into multiple languages and each document need to be versionized and audited with whats changed when and who dun it etc. Also i need to be able to clone a document for another user so he can then edit it for himself and with all above version and audit features.
I have looked at Globalize2, acts_as_audited, acts_as_versioned, paper_trail and deep_clone and sort of need abit of each !
Please can anyone help me with how or what is the best method of develping this app ? Is there better more suited plugins to use ? can these be used with each other ? and what would be the best process to set this up ?
any help would be most appreciated.
thanks
Rick
thanks askegg for your reply
the thing is acts_as_version does not include your models relations and i need to version each document model along with the children asscociated models ?
So basically if i dont use Globalize my transplated documents will just be other versions but in a different language. Is that correct ? I thought that is what Globalize does ?
thanks alot
rick
It seems you have a grasp of the basic structure you want and have investigated some alternatives.
First pass of my reading: You have a Document model, probably with a polymorphic association to an Asset model. Come to think of it, a Document is just a type of Asset, so one could inherit from the other - perhaps Single Table Inheritance.
From here, add acts_as_versioned to deal with ....well, versioning. This should also be able to give you the differences between various versions, just ensure you record the user_id along with the change.
I am not sure Globalise or i18n will help you here, as they are more geared to translating the web site itself with reasonably static content, not highly dynamic documents such as you're dealing with. I would leave translations to the users and use i18n to present different translations of the web site itself.
Cloning a document should not be too difficult - just create a new Document and populate the information in it. You will probably need a cloned_from_id field and build a self referential has_many in the model.
Well, they are the thoughts off the top of my head.
I was thinking you might treat each image, document, pdf, whatever as separate assets each with their own versions - operating independently of any other. If you need to clone a document and it's children, then you would need to iterate over the children and clone them as well - and their children's children. etc.
Globalise (or any other translation platform) is only really good for set/simple statements. Due to syntax and grammatical issues I would not expect it to be able achieve good translations between languages. Humans are great at that.
It occurs to me that you might want to think about using Git as a document store. This gives you full document versioning with tracking changes. See this presentation for some ideas. Don't worry about all the tech details - just think what it can do for you. The good stuff start about 15 minutes in. The Grit Ruby library can be found here.

Resources