action mailer and url generation in Rails 3.0.9 - ruby-on-rails-3

So I realize that Rails mailers need help in generating urls. I want HTML email with images served externally (no attachments) so I've set both:
config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.action_mailer.asset_host = "http://localhost:3000
and two things happen in my mailer template:
It hates the use of image_tag. Just barfs with an action view template error
url_for does nothing for unnamed routes. shouldn't it prepend the asset_host? for assets in public?

Related

Middleman’s link_to helper for localized templates

For the site I'm building with Middleman, I am localizing entire templates as described in the docs on the bottom of the "Localization" section. So the relevant part of the file tree looks like this:
/localizable
|
|- index.en.html.haml
|- index.ru.html.haml
|- about.en.html.haml
|- about.ru.html.haml
I can link from the index.en page to the about.en page using the path helper like so:
= link_to 'about me', '/about.html'
But when I try to do a similar trick to create a link from the index.ru page to the about.ru page:
= link_to 'some russian text', '/russian/about.html'
the helper doesn't work. It ignores the /russian folder and creates a link to /about.html in root.
Am I missing something or is the path helper unusable for localized templates? Is the only option to use the <a> tag directly?
============
Update1: relevant parts of my config.ru file:
set :css_dir, 'stylesheets'
set :js_dir, 'javascripts'
set :images_dir, 'images'
activate :relative_assets
set :relative_links, true
activate :i18n, :langs => [:en, :ru], :lang_map => { :en => :english, :ru => :russian }
activate :blog do |blog|
blog.prefix = "blog"
blog.paginate = true
end
# Build-specific configuration
configure :build do
# For example, change the Compass output style for deployment
# activate :minify_css
# Minify Javascript on build
# activate :minify_javascript
# Enable cache buster
# activate :asset_hash
# Use relative URLs
# activate :relative_assets
# Or use a different image path
# set :http_prefix, "/Content/images/"
end
Cant really say what is the problem without seeing your config.rb file and the structure of your file system.
My guess on your problem would be like this here
or it could be real simple that you have to change 'russian' to 'ru', since that is in file name.
Here is a good example
EDIT:
Now with your config.rb , I can see you are using :en as default and :ru as russian
Since you are using :en as default(non prefixed), you dont have to map that. or if you want that to be mapped and not be default you might have to use ':mount_at_root => false' with activate 'activate :i18n,'
Try the following solution
activate :i18n, :langs => [:en,:ru], :lang_map => {:ru => :russian}
Like I said, I simply just removed :en mapping and it worked on my test. Since you make :en as default you dont have to map it. If you want both languages to be mapped correctly then use the following
activate :i18n, :mount_at_root => false, :langs => [:en,:ru], :lang_map => {:en => :english ,:ru => :russian}

Rails 3 Remote Form Ajax Success

I'm having some issue figuring out how to handle success on my entries form using rails 3 ujs ajax.
If there are errors, I have a create.js.erb that will alert(j(#entry.errors.full_messages), and this works. But if there are no errors, the form doesn't redirect (because I'm rendering in a dialog) and I'd like the js to alert("success") and close the dialog. (using fancybox 2).
Can you give me some pointers working with rails 3 ujs and ajax?
An approach is to identify and handle the error at controller level instead of view.
def create
#entry = something
if #entry.save
#notice = "Success message!"
respond_to :js # render default create.js.erb
else
respond_to :js { render 'create_error.js.erb' }
end
end
// create.js.erb
$("#dialog").close();
alert("<%= #notice %>">;
// create_error.js.erb
alert(j(#entry.errors.full_messages);

I want to use `layout false` but I still want to use the CSS that would typically load when I'm rendering the layout - how do I do this?

I have a controller where I set layout to false:
class SplashController < ApplicationController
layout false
def index
end
end
But when I load this page there is no css whatsoever - I assume this has to do with how rails handles layout false - but my current knowledge of rails leaves me lost.
How do I not render a layout, but still load all the other assets (css, js, etc. . .) that would typically load if I were to load a layout? (*Note that the layout file has no specific reference to any of these assets)
By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the :layout => true option.
As you need only the information to be displayed, I suggest to use :text to render.
You can send plain text – with no markup at all – back to the browser by using the :text option to render:
render :text => "OK"
NOTE: Rendering pure text is most useful when you’re responding to AJAX or web service requests that are expecting something other than proper HTML.
UPDATE:
Also if you want that assets should be shown but still layout should be false then you have to render layout to false after making the assets available. This means you make some view, then define your required css and js files there and then call that view from controller and then set layout to false.
Setting the layout to false after view will show the css and js stuff but still keep the layout to false.
But setting the layout to false before showing the view that contains css and js will not include assets at all.
The other alternative of the above will work also:
css : <%= eval("render :partial => 'myurl/blah', :formats=> [:css], :layout => false").dump.html_safe %>
You see that how partial view that contains your assets like :css is getting called while layout is false.

Rails 3 how to render .json.erb file

I'm working on migrating a rather big project to Rails 3.
Here's my controller action:
def recent
#account = Account.find(session[:account_id])
render :layout => false
end
Here's my recent.json.erb file
formatted_account =
{
:code = 1,
:id = #account.id,
:prefix = 2
}
formatted_account.to_json()
I'm using jQuery.getJSON to get this data, when I get the response, this is what I get:
[{"code":1,"id":"1 "prefix":2}]
Instead of
[{code:1, id:1, prefix:2}]
I had to use safe_html in some other pieces of code to solve escaping issues like this but in this case I can't figure out how to solve without getting rid of the .json.rb file and rendering a json object in a proper way.
html_safe did the trick:
formatted_account =
{
:code = 1,
:id = #account.id,
:prefix = 2
}
formatted_account.to_json.html_safe
If you want to build up json from a template you'll need to use some kind of builder as ERB won't really cut it.
JBuilder comes commented out in a fresh Rails 3.2 Gemfile. Haven't used it myself but it seems well thought out with a clean DSL. There's also a list of links at the bottom of the README on the JBuilder github page.
RABL is another tool for building JSON, as well as supporting multiple other formats.
JBuilder
RABL

Override html in active_admin gem

I wanna override html code when working with active_admin gem in Rails; because the nav-bar and many elements in these gem's views are different with my views (other pages). I hope that has a way to change html code without changing css manually! Thanks
It is not very easy , activeadmin use DSL for building html (called "Arbre")
You have to monkey patch every page class, also , it may prevent customizing of css too.
For example to move sidebar to left, create initializer with next patch.
class ActiveAdmin::Views::Pages::Base < Arbre::HTML::Document
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_sidebar unless skip_sidebar?
build_main_content_wrapper
end
end
end
default method was
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
The full list of classes used for rendering can be found here , so some of them you need to patch.
https://github.com/gregbell/active_admin/tree/master/lib/active_admin/views
Be ready for a big piece of work.
UPD. Gem for changing activeadmin sidebar position
https://github.com/Fivell/active_admin_sidebar