How to prepend rails view paths in rails 3.2 (ActionView::PathSet) - ruby-on-rails

I am trying to prepend views to the rails view array e.g.
prepend_view_path("#{Rails.root}/app/views/custom/blah")
This works fine, however in my test suite I keep seeing
DEPRECATION WARNING: process_view_paths is deprecated and will be removed from Rails 3.2.
After a bit of research I see mention of ActionView::PathSet, but cannot find any help searching google or in the Rails API documentation. I need to know how to use this new way of prepending paths in rails 3.2
I would really like to get rid of this warning. Any thoughts?

If it is dynamic (set on a per-request basis):
class ApplicationController < ActionController::Base
before_filter :set_view_path
def set_view_path
prepend_view_path "#{Rails.root}/app/views/custom/blah"
end
end
I think it went to AbstractController::ViewPaths, but still available from controller - should be without deprecation.
If you prepend static fixed path:
# config/application.rb
config.paths.app.views.unshift("#{Rails.root}/app/views/custom/blah")

Related

working with views using rails new backend Rails 5

I started with rails 5, I am noob in rails. I want to create a simple API, but I want to have views (such as active admin). I found the autogenerated API using the code 'rails new backend' command.
Is there a way to autogenerate views and not only Json response using this command?
I may not have the answer, but I found this which may be a better answer to the question.
Approach:
Subclass APIController from ActionController::API, rather than ApplicationController, make ApplicationController inherit from ActionController::Base.
You may include the Rack::MethodOverride middleware.
I'd try to build something similar to this in the coming week, largely to learn about ActiveAdmin and some newer Rails methodologies.
Using the standard ActiveAdmin interface won't work with an API app because those (by definition) cut away the presentation layer, i. e. all the gems for views/js/etc.
But it will work the other way around: --api is almost a subset of a full rails application and comes with, for example, json rendering by default. I say 'almost' because a few details need to be adjusted, such as (probably) cors and csrf settings.
You can get get to a functioning rails 5 app serving .json in about a minute:
rails new foo
rails db:setup
rails generate scaffold Post title:string body:text
rails db:migrate
rails server
-> http://localhost:3000/posts/index.json
Apart from reading the docs and a few tutorials, you should generate two applications, one with --api and one without, add model/views/controller scaffold and just walk through the diff. Then you can mix&match the customizations for api-mode into your app.
Enabling Active Admin for Rails 5 API application
1. Separate view-rendering controllers from API controllers
Active Admin requires ApplicationController to inherit from ActionController::Base.
API controllers should still inherit from ActionController::API
Use the following
class ApplicationController < ActionController::Base
class APIController < ActionController::API
where any API-specific controllers inherit from APIController.
2. Enable flash middleware
Inside config/application.rb
module MyApp
class Application < Rails::Application
# ...
# add this =>
config.middleware.use ActionDispatch::Flash
end
end
3. (For Devise authentication) Enable session management
Inside config/application.rb
module MyApp
class Application < Rails::Application
# ...
# add these =>
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
end
end
Here is the full guide

Refactoring a large routes.rb file in rails 4

I'm in the process of upgrading a rails 3 app to rails 4.0.1.
In my rails 3 app I have the following code in the my application.rb to use multiple route files.
config.paths["config/routes"] += Dir[Rails.root.join('config',
'routes', '*.rb').to_s]
but that throws an exception when I try to use the same thing in rails 4.
Any tips?
In one of my larger applications I use the following segment of code inside of my config/routes.rb file.
class ActionDispatch::Routing::Mapper
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
end
end
YourApplication::Application.routes.draw do
# Loads config/routes/common.rb
draw :common
# Loads config/routes/another.rb
draw :another
end
Rails 4 initially had support for draw :routeName but it was removed as it did not show any improvements. (I dont know ^.^) You can check out the git commit doing so here: https://github.com/rails/rails/commit/5e7d6bba79393de0279917f93b82f3b7b176f4b5
Check out this SO answer: rails 4: split routes.rb into multiple smaller files
Looks like this ability was deprecated in Rails 4.
I don't know how big your application is. But you should look into routing concern in rails 4, if you need some proper refactoring with Rails route.
Mo' files, mo' problems.

Rails layout inheritance documentation wrong?

On the rails 3.2 release notes page (http://guides.rubyonrails.org/3_2_release_notes.html), it says:
Deprecated implied layout lookup in controllers whose parent had a explicit layout set
But I tried the following in my rails 3.2.6 app:
class ApplicationController < ActionController::Base
protect_from_forgery
layout "application_main"
end
class HomeController < ApplicationController
def index
#slideshow_pics = Event.get_intro_slide_photos
end
end
with layouts application_main.html.haml and home.html.haml defined and when i go to the home#index page, I get the home.html.haml layout rendered instead of the other.
This seems to go against the deprecation so I was wondering, did one of the releases since 3.2.6 regress the deprecation?
To be even more clear than Dave above, "Deprecated" means: Watch out, we're going to remove this functionality in the future! We're throwing this warning so you pay attention and you better change this soon as we are change how things work!
It doesn't mean that the functionality has already been changed.

'RuntimeError: route set not finalized' when calling Rails.application.routes.generate() or Rails.application.routes.recognize_path()

I use the Rails.application.routes.generate() and Rails.application.routes.recognize_path() in my application to disassemble and generate URLs using the Rails routing table. I never had a problem with this using the Rails 2.3.x series but since Rails 3 (specifically 3.1.3) I've been getting the following error when invoking these commands
RuntimeError: route set not finalized
Two questions :
Why? Am I getting this - my application is up and running and handling other requests. Why are the routes not finalized by now?
To solve this error I call Rails.application.routes.finalize! before invoking either of these methods as it seems to work. Are there any implications to doing this?
In Rails 3, you don't use this technic. You need include Rails.application.routes.url_helpers after you can use your named_route
class User < ActiveRecord::Base
include Rails.application.routes.url_helpers
def my_own_url
user_url(self)
end
end

Why can't I locate this method in the rails api docs?

I'm studying some Rails 3 code from Spree:
module Spree
module Generators
class SiteGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates", __FILE__)
desc "Configures an existing Rails application to use Spree."
def create_lib_files
template 'spree_site.rb', "lib/spree_site.rb"
end
def additional_tweaks
remove_file "public/index.html"
append_file "public/robots.txt", <<-ROBOTS
.... continues ....
This works with Rails 3, but I've looked up Rails::Generators::Base, following the inherited modules to Rails::Generators::Actions and the Thor classes, but still can't seem to find api documentation on the #template method. I can figure out what it does, but I'm troubled that I can't find the docs on it. It's got me feeling like a real newbie (though, since I haven't worked with Rails in quite awhile, I guess in some ways I am).
Any help would be appreciated. Please tell me why I'm not able to find this (and other) methods in the Rails api docs. What am I missing???
The template method is an instance method included with Thor::Actions and can be found at http://rubydoc.info/gems/thor/0.14.6/Thor/Actions:template

Resources