Include FormTagHelper in model? - ruby-on-rails

I am trying to take an html file created by MSWord (don't ask) clean it up and create a form out of it on the fly. I am doing this work in the model (clean it up, create the form, stuff it in a DB). I would like to use the form_tag helpers, but can't quite get the hang of including them for use in the model. When I do an "include ActionView::Helpers::FormTagHelper" the helpers aren't available.
So, I figure either
I should be doing the clean up & create in a helper
I fundamentally don't grasp including in a model
All of the above.
Suggestions?

Form tags should not be included in the model. They are for views only.
I've got to ask. Why? It sounds like you're in for a wold of hurt.

Related

Ruby on Rails generating controller with existing folder

I'm really new at this and I don't want to mess up my current application. I'm currently working on HTML/CSS stuff but I have a little understanding as to how to get things working.
I created a new controller by just doing
rails generate controller sign_up
I've created a index file (index.html.erb) inside sign_up folder from rails that automatically generated. Now I wanted to add more files, can I just add more files by typing in
rails generate controller sign_up send_page more_page other_pages
Will send_page, more_page, other_pages be automatically combined into the existing sign_up folder? Is this the correct way of adding pages if I want rails to add other files automatically? I just want it to ruin what I have so far so I don't want to try it for myself because I'm still really new to this,
Thanks!
To answer your question :
New files will be added to the existing folder but existing files will not be merged, you'll have to resolve the conflict by choosing between old and new files.
It seems to me you're trying to use generators for every action you need in your app :
Scaffolding is great to have a sample structure, but as soon as you start building your controller for real you need to move away from it : extend the controllers, models and views by hand. It's the only way you'll start to really understand how things are working.
If you run the generator again, it's going to complain about conflicts with existing files. Then you'll have to choose to overwrite (losing your existing changes to e.g. the controller), or not (in which case you won't get the new auto-generated methods, etc.).
You can just create a new controller action and a new view for each additional page you want to add (and a unit test, of course!). If you're using non-standard/non-restful action names, you'll also have to edit config/routes.rb to route them.
Looking at the names in your example, and your reference to "pages", I suspect what you really may want to do is create separate controllers for send_page and more_page. If that's the case, you'd run the generator separately for each new controller.

Rails Generator vs Create Files by Hand

I have notice that there are many files created by generators (not only scaffold, but rails generate in general) that I don't use.
So I came up with the following questions (about best practices):
Do you use generators or it is preferable to create by hand only files you need?
If you use generators, do you keep all generated files?
You can use generators specifically to help you build individual files. For instance, you could use
rails g model Video title:string link:string
to create a model file with those as attributes, but you don't need to use scaffolding to generate everything (if that's what you're referring to)
Personally I like to use a combination -- it really depends on the project. But I'd tell you not to have extra files that you aren't using, so definitely delete the ones you aren't using (or don't plan to use).
Hope this helps.
i do use the generators and just delete the files. It is probably because I am lazy but I find this easier.
Especially with models, because it includes the migration.
Honestly it all comes down to just preference. Just don't scaffold...

Does every rails app have a default application.html.erb created in their layouts?

I just had a general doubt which I wanted to clear wrt Rails. I am currently working on Rails 2.0.2 for project specific purposes and I had a doubt especially related to this version of Rails.
I did a basic scaffold in my rails app on "posts".. something like ruby script/generate scaffold posts . This created a posts.html.erb file for me in my app/views/layouts .. I have seen in many blogs/screen casts they say when we add Javascript(JS) files like for e.g. those implementing jquery etc. we need to make include the necessary files in our "application.html.erb"files.. Now since I don't have anything exactly coined as such in my app.. does Rails by default take my posts.html.erb in my layouts as the equivalent application.html.erb..?
Or is that I need to explicitly create application.html.erb in my rails app?
My main concern behind this question is that would JS files be included in case if I have something like posts.html.erb or is that.. it should be done only in the application.html.erb..
Thank you..
For a PostsController, Rails will first look for an app/views/layouts/posts.html.erb file. Only if it doesn't find this controller-specific layout will it then fall back to app/views/layouts/application.html.erb.
does Rails by default take my posts.html.erb in my layouts as the equivalent application.html.erb..?
Yes. For the PostsController it will (by convention) take layouts/posts.html.erb as the overall layout template.
If you remove this file it will fall back to the layouts/application.html.erb layout.
My main concern behind this question is that would JS files be included in case if I have something like posts.html.erb or is that.. it should be done only in the application.html.erb..
You would have to include all necessary JS/CSS in each layout, as there is no layout inheritance.
If you want multiple layouts you can refactor out sections into partials (eg: for the <head> bit).
an app/views/layouts/application.html.erb file should have been generated for you when you did "rails new ProjectName"
make sure you have one... it controls the general layout of your web-site.

Where should Rails 3 custom validators be stored?

I've seen docs/websites show that custom validators should go in a /lib or /lib/validators directory of a project. I've found (by reading an answer to another post) that they only seem to work in config/initializers. Does anyone know, or have a pointer to official documentation that shows where custom validators should live?
If you place your custom validators in app/validators they will be automatically loaded without needing to alter your config/application.rb file.
If you add this to your /config/application.rb file:
config.autoload_paths += %W["#{config.root}/lib/validators/"]
Then Rails will automatically load your validators on start up (just like /config/initializers/), but you keep the clean structure of having your validators in one nice, well named spot.
lib/validators seems by far the cleanest. However you may need to load them in before your models, so probably from an initializer.
Here's the official docs about custom validations. AFAIK its a good practice to keep them in the relevant models.

Change Rails Scaffold Naming Scheme

I'm a happy user of RoR but have one complaint. When I do script/generate scaffold it automatically generates all my files and places them in their proper folders. However, all the different scaffolds I've created name their view files the same.
I have a bunch of index.html.erb view files and when I have them open in my text editor, it's almost impossible to tell what controller they're related to.
I'd like to change the default naming scheme of the scaffold command to name the individual files to contain their view folder name. So, instead of index.html.erb, use index.home.html.
Is there a way to do this, or am I stuck? What solutions to the multiple files with the same name problem have you Rails developers discovered?
Thanks!
You're going to be fighting the Rails' conventions by going down that path and Rails works best when you work with it rather than against it. A core part of the philosophy of Rails is that there are a set of conventions that once learned make it easy to find your way around any Rails application.
Instead of trying to redefine how Rails works, I would recommend taking advantage of the features offered by your text editor or IDE for quickly navigating to the correct view template. For example, the Rails bundle within TextMate on the Mac lets you quickly open the view file associated with a particular model and there's a plugin for Vim that provides an equivalent feature.

Resources