remove new.haml/ new.erb from ruby on rails? - ruby-on-rails-3

The default ruby on rails scaffolds generate a new.haml or new.erb these include only 1 line: = render 'form'
Thus, basically the file is obsolete, is there any preferred way to remove the new partial and make it use the _form partial instead straight away or would this go against rails conventions?

no, there is no preferred way. it goes against rails conventions.
TL;DR don't do it.
if you don't need any customizations on your templates and are just creating basic CRUD stuff, have a look at admin tools like RailsAdmin ActiveAdmin Typus etc.

Related

Rails 3: ActionMailer Default Layout with Inline Attachments

I have a number of mailers in my Rails 3.2 application, and I would like all of them to use the same email layout. That layout includes a header and footer, which includes images to be used inline. I'd like to follow the DRY principle of Rails, but I'm not entirely sure what the best practices are for this.
As this answer points out, I can use AbstractController::Callbacks to create a before_filter to add inline attachments. But how do I do this as well as set the layout in one module that I can then include in the mailers?
Ideas I have at the moment are to create a new mailer class called DefaultMailer, then have all mailers that I want to use the layout inherit from that class. Or to create a concern that would handle these tasks. Before attempting to hack something together using one of those techniques, I thought I would ask here to see if anyone had done this successfully before to help guide me.
AFAIK there are some issues when inheriting from other mailers (ie. not inheriting the settings).
Common solution is to create a mailer base module. This is a snippet from existing project:
module MailerBase
extend ActiveSupport::Concern
included do
helper :application
layout 'mailer'
default from: "#{AppConfig.application_name} <#{AppConfig.mailer_sender}>"
end
end
You can combine this with your callbacks, although including remote images is more common and arguably better solution.

Safe templates (Ruby on Rails)

Is there any plugin allowing user to create there own templates? So I use smth like
Templates.find(5).render(:val1 => val1, :val2 => val2)
There is a good plugin named liquid however it doesn't seem to be safe (user can drop database and so on).
Thank you.
Liquid is a very popular templeting system and is considered safe. In fact it was one of the design goals. From the documentation of liquid:
Liquid is a template engine which was written with very specific requirements:
It has to have beautiful and simple markup. Template engines which don't produce good looking markup are no fun to use.
It needs to be non evaling and secure. Liquid templates are made so that users can edit them. You don't want to run code on your server which your users wrote.
It has to be stateless. Compile and render steps have to be seperate so that the expensive parsing and compiling can be done once and later on you can just render it passing in a hash with local variables and objects.
The locomotive CMS use a gem called liquid that claims to do that. Check it here http://rubygems.org/gems/liquid.

How to name a coffeescript file for a Rails 3.1+ namespaced controller?

If I have a Rails 3.1+ controller called PeopleController and a second one for admin functions called Admin::PeopleController, how should I name and/or construct the associated coffeescript files to be picked up unambiguously when a given controller is hit?
It seems for the standard people controller, I should create people.js.coffee - that makes sense. How do I name the coffeescript file that will get loaded for the admin people controller? Is there a naming convention I can follow, or does the one coffeescript file need to be used for both?
Firstly, it doesn't really matter what you call them. If you're requiring the whole tree, or at least specifying the files you want to include, in your application.js file then they'll all get picked up anyways.
If you're including the files individually then you'd still need to specify, presumably in your layout file, which one(s) you're including with the usual stylesheet tag.
I guess the convention would be to name them people.js.coffee and admin_people.js.coffee.

Rails 3 autoload_paths versus Rails 2 load_paths

So, I figured out that I needed to change my config.load_paths to config.autoload_paths but when I did that, I start seeing this exception:
Expected /path/to/myapp/app/helpers/controllers/my_helper.rb to define Controllers::MyHelper
In Rails 2, it never complained about the helpers/controllers folder, but now it seems that it expects a namespaced module. Do I have rename all of my helpers, or is there some other way this can work? I have so many helpers that I use helpers/models helpers/views and helpers/controllers to keep them all organized.
Thanks in advance!
Rails 3 does expect your classes to be namespaced according to file name and directory hierarchy, so you will have to rename your helper to Controllers::MyHelper.
File naming conventions in Rails 3 are a lot more strict than they were in Rails 2 for performance reasons. Rails 3 expects the file structure to directly match the class hierarchy.
If you would like the run the application in Rails 3 I would suggest removing the helpers directory or adding Controllers:: to the front of each class declaration.

how to override rails 3 engine models and controllers in the main application?

I want to be able to override models and controllers of my rails 3 engine in the base app.
Inspecting $LOAD_PATH, I found engine's 'app/{models,controllers}' there, but I can't explicitly require engine's model or controller file: require 'engine_name/model_name' fails with "no such file" (tried with both namespaced(app/controllers/enginename/*) and plain engine).
So, what's the best way to extend engine's models/controllers in rails 3 without copying them to base app?
Basically, it's a load order problem. So, if I explicitly require model from engine, everything's ok, but I hope there is a better way.
So I actually went back and wrote the documentation. The answer is to Open Class the Controller and Model classes using either,
Class#eval_class
ActiveSupport::Concern
More details here,
http://guides.rubyonrails.org/engines.html#overriding-models-and-controllers
(edited. Changed from "edgeguides" subdomain to "guides" subdomain)