How to host a website on Rails without following the MVC structure? - ruby-on-rails

This may be a dumb question, but since I don't know the answer I'm going to go ahead and ask anyway. I have an app (mobile) that is hosted on Heroku with a Rails server. I have all the basic API stuff there.
Now I have a landing page for my app that I would like to host on this server. It's a basic template, but the problem is that it has a non MVC structure, i.e it is just HTML, CSS and a few jQuery/JS plugins.
How do I host this on my existing server, which has an App --> Controllers, Models, Views, Assets... type of structure?

If you want to serve static pages in rails you can create a Static controller and Static views and route the actions in the Static controller to your Static views. Note there will be no associated static model. For example:
controllers/static_controller.rb
class StaticController < ApplicationController
def landing_page
end
def another_page
end
end
views/landing_page.html
<title> <h1>Landing Page stuff</h1> </title>
views/landing_page.html
<title> <h1>Another Static Page</h1> </title>
config/routes.rb
Project::Application.routes.draw do
root :to => "static#landing_page"
match '/another_page', to: 'static#another_page', via: 'get'
end

Firstly, if you want to do something out of scope of MVC, you may wish to check out other frameworks. IonicFramework recently just attracted funding & is an HTML5 mobile app framework
Static Pages
If your question is about loading static pages in Rails, you have to remember Rails is not tied into using a DB. The MVC structure is that - a structure
The Rails way of doing things is as such:
HTTP Request > ActionDispatch::Routing > Controller::Action
This means if you set up a route as follows:
#config/routes.rb
get "landing_page", to: "static#action"
get "static_page", to: "static#static_action"
You'll be able to see how to set up the controller from Steve's answer

Related

Rails path helpers without routes

We are in the process of taking a few pages out of our rails app to be served separately (they are a few static pages with some content being managed through a cms). The urls will stay the same. Our own routing system in front of the servers will decide which request should go to the rails app and which to the static part.
My question is about path helpers that we use quite a bit throughout the rails app, such as link_to about_path that generate mahwebsite.com/about. As I understand I can just leave them be, they will still generate correct urls. My only concern is that for them to work I'll have to keep the routings in the routes file, which will have to be connected to the dummy controller methods. Seems like a lot of redundant code just to fool rails into creating path helpers.
Alternatively, I can hard-code links to the static pages. But before I start replacing a whole lot of code, I'd like to know if there is a clean Railsy way to keep the path helpers without having to route to the redundant controllers.
Thanks.
Why not just create your own helper method? E.G.
# application_controller.rb
def about_path
"mahwebsite.com/about"
end
helper_method :about_path
alias_method :about_url, :about_path
This will overwrite any Rails helper method and do exactly what you're after :)
Hope this helps - give me a shout if you've any questions or comments.
How about
resources :custom_pages, only: [:your_options] do
get :view/:page_id_or_whatever_for_identify
end
and do the following content with the controller?

Really static pages with Rails

Some page in the application must be accessible even application is down. For example pages for 50x errors. The easiest way to do so - create static HTML pages, which will be served by web-server (like apache on Nginx). Most of this pages have a common layout with the application. So, if we change some part of layout in the application we must change all static pages by hand.
What is the best way to store rails pages as static files and recreate it (automatically or by rake task) on same changes in the project? Is any gem for rails or static-site generator that's able to reuse rails layout and resources (CSS, js, images).
Generally static content goes in your public folder which you can configure Nginx or equivalent to route to accordingly without even needing to hit Rails.
For static site generation in Ruby you might want to check out Jekyll https://jekyllrb.com/. You could manage your Jekyll site separately from your Rails site and generate the static HTML/CSS/JS on deployment. There's a jekyll watch command that will listen for file edits and compile your static content accordingly.
There simple rake task (via GIST) to load all files from VIEW_PATH, wrap it with application layout and store it under same path in public. Work with Rails 4.
https://gist.github.com/potapuff/090b2da4a4156c1272430241cb70edc0
namespace :static do
desc 'Render all resources'
task :publicate => :environment do
resources(VIEW_PATH).each do |src, dest|
html= controller.render_to_string(file:src, layout:'application')
dirname = File.dirname(dest)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
File.write(dest, html)
end
end
def resources search_path
...
end
def controller
ApplicationController.new.tap do |controller|
...
end
end
end
Other possibility is using gem render_anywhere .
In Rails 5 we have new ability to use render outside controllers
https://medium.com/evil-martians/the-rails-5-post-9c76dbac8fc#1b36

generate routes for one page landing pages?

I am attempting to build a simple webpage using Rails and this guide to great looking landing pages by William Ghelfi (thanks!) http://www.williamghelfi.com/blog/2013/08/04/bootstrap-in-practice-a-landing-page/
I have started a new project on rails using Rails 4.0.0
Server is running fine on localhost:3000 as evidenced by the RoR landing page.
My question is what's the best way to generate routes and controllers for a one page landing page using RoR?
Right now, when I attempt to point to localhost:3000/lpd.html I get the error No route matches [GET] "/lpd.html" No routes defined.
If you just want a static page, you can just move the lpd.html to the public folder and it will work. But many rails websites have semi static pages.
So you could create a LandingController or HomeController where you keep all your semi-static pages. E.g. home, about, contact us ...
So for instance, do the following
rails g controller Home index about
This will generate a HomeController with two actions index and about, with the corresponding views and default routing. So in app/views/home two views will be added,
which you can then edit as you wish.
Then you can edit the routing (in config/routes.rb) as follows:
get '/about' => 'home#about'
root_to 'home#index'
This will make sure that http://localhost:3000 will be your home/index view, and http://localhost:3000/about will be your about page.

How to change ActionCaching "views/" prefix per request

So we use the same controllers to serve both mobile and desktop views of our site. We also use action caching heavily to cache the html for a page in memcache. I've been trying to figure out a way to globally change the caching prefix for all mobile requests to "views-mobile/" instead of the standard "views/". That way the mobile and and desktop pages will be saved under a different namespace so there are no conflicts in memcache.
We could do this per caches_action method by creating a custom cache_path using the controller variable for is_mobile?, but we'd prefer to do it globally somehow. Any suggestions? I imagine this would require monkey-patching ActionController::Caching but I can't figure out where it generates the "views/" prefix.
I'm sorry, I was Rails nubie, so I don't really understand about your question, but if it right, is this what you mean?
This is on my routes.rb:
scope "/administrator" do
resources :users
end
I changed my users_path 'prefix' to administrator. Sorry if wrong :D
I actually ended up figuring this out myself. Basically ActionController::Base uses a function called fragment_cache_key to generate the cache key for a specific fragment (which is what ActionCaching uses deep down). So you basically override that method and include your own logic for how to generate the prefix. This is how my method override looks:
# Monkey patch fragment_cache_key
def fragment_cache_key(key)
ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, mobile_device? ? "views-mobile" : "views")
end
Where mobile_device? is my own function that figures out whether the user is requesting the mobile or desktop version of the site.

Rails: Resources_Controller/ Resource_Controller/ Make_Resourceful with Subdomain_Fu

I need to use one of the resourceful controllers plugins - resources_controller/ resource_controller/make_resourceful as I have several polymorphic models and the models have to be either initialized/build depending on the route.
For example:
www.example.com/groups/1/pages
www.example.com/projects/1/pages
where page acts as polymorphic object as both Group and Project have many pages. So I am thinking of using one of the aforementioned plugins to make pages_controller adapt to both routes. All three plugins works fine and differences are just their implementation of recognizing the routes and loading the models based on that.
Now I want to add sub-domain support using Subdomain_fu plugin so the above example would be:
Site1.example.com/groups/1/pages
Site1.example.com/projects/1/pages
Site2.example.com/groups/2/pages
Site2.example.com/projects/2/pages
On looking at all the three plugins, I don't see any way for them to start loading the resources from my subdomain object, as the subdomain is not part of the route. Any advise on what I am trying to accomplish in a dry/restful way?
I don't know how to do that with resources_controller but i was able to pull off the same thing with the inherited_resources plugin.
Here is how i accomplished it:
In my application controller I set up a before_filter to find the subdomain:
def set_subdomain
#subdomain = Subdomain.find_by_url( request.host )
end
Then in my controllers Using inherited resources I set the #subdomain association using the very cool method "begin_of_association_chain"
protected
def begin_of_association_chain
#subdomain
end
Agile web development has great documentation.

Resources