Rails nested routes for simple blog - ruby-on-rails

I need to make page for my blog and also for each post under blog. It should look like this example.com/blog/my_first_post. Posts are static HTML files and I don't use any database.
Here is my routes:
get 'blog' => 'static_pages#blog' do
get '/my_first_post' => 'blog#my_first_post'
end
Here is my StaticPages controller:
...
def blog
def my_first_post
end
end
...
Blog page is working fine, but post is not working.

To get this example.com/blog/my_first_post, your routes should look like the followings -
get 'blog' => 'static_pages#blog'
get 'blog/my_first_post' => 'static_pages#my_first_post'
Your controller should be looking like this-
class StaticPagesController < ApplicationController
def my_first_post
end
def blog
end
end

Check this one: but one thing
If its just static page then better use as like in below.
get 'blog/my_first_post' => 'static_pages#my_first_post'
you cannot call that method like view if its in it. make it like this.
def blog
end
def my_first_post
end

Use below, above resources: blog if present
get 'blog/my_first_post'

Have a try:
get 'blog/my_first_post', to: Proc.new { |env|
[
200,
{"Content-Type" => "text/html"},
[File.read("public/my_first_post.html")] // where you static files are
]
}
In your case, a controller is redundant

Related

Rails Take all actions in the controllers in area

In my rails application I add an "api" area with controllers
In the route.rb file
I add the area
namespace :api do
#get "dashboard/sales_rate"
end
The controllers Class:
class Api::DashboardController < Api::ApplicationController
before_filter :authenticate_user!
def user_service
render :json => {"user_id" => current_user.id}
end
The Path is:
app/controllers/api/dashboard_controller
My question is if I can that the route take all action
for example /api/dashboard/user_service
and I will not add for each route row on the route.rb page
/api/{controller_under_api_namespace}/{action}
You can do with some meta programming sprinkle!
Api.constants.each |c|
c.action_methods.each do |action|
get [c.controller_name, action].join('/')
end
end
This method limits only to GET request. You can overcome it with RESTful routing.
Api.constants.each |c|
resources c.controller_name.to_sym
end
Hope, that helps. :)
I try add the code on the route.rb file
and I got this error
This is my file
But before trying to fix this part of code, I want to know if it's can change the performance or the calls to those pages?
If it not good for the performance I leave this option.

Rails 4 subdomains, tied to controller

grateful to have you guys as a resource! I think this should be a simple question but haven't been able to find a simple answer through searching yet, any help/guidance would be appreciated!!
I have a "subdomain" controller which is setup in the following way:
get 'subdomain/:store' => 'subdomain#index'
get 'subdomain/:store/products' => 'subdomain#product_index'
get 'subdomain/:store/products/:id' => 'subdomain#products_show'
As you can see, the subdomain controller matches the request with a Store ID and can also get an index of all the associated products with the Store ID. I'd like to somehow convert each of these requests into a subdomain rather than a path. Each Store has a "subdomain" attribute (in the example below, one of the Store records has a subdomain value of "nike").
For example
host.com/subdomain/nike => nike.host.com
host.com/subdomain/nike/products => nike.host.com/products
host.com/subdomain/nike/products/5 => nike.host.com/products/5
Notice the controller "subdomain" was removed from the path. Any help? I looked into gems such as apartment but they look like they are way too complex for this. Also subdomain-fu but it looks like it's outdated for Rails 4. Thoughts? THANKS!
For this, you can add routing Constraint.
Add the file to lib/subdomain_required.rb
class SubdomainRequired
def self.matches?(request)
request.subdomain.present? && request.subdomain != 'www'
end
end
Then, in your routes.rb, you can enclose your routes into a contraint block, somewhat like this:
constraints(SubdomainRequired) do
get '/' => 'subdomain#index'
get '/products' => 'subdomain#product_index'
get '/products/:id' => 'subdomain#products_show'
end
Now the last step is to load the store based on subdomain which can be done using a before_action like this
class SubdomainController < ActionController::Base
before_action :ensure_store!
def index
#products = current_store.products.all
end
def ensure_store!
#store ||= Store.find_by subdomain: request.subdomain
head(:not_found) if #store.nil?
#store
end
def current_store
#store
end
end
now anywhere you want to get the store, you can use current_store helper method.
Hope it helps

Rails dynamic error pages (404, 422, 500) showing as blank

I'm implementing dynamic error pages into an app, and have a feeling it's looking in the public folder for (now non-existent) templates, rather than following the routes I've set up.
In config/application.rb I've added the line config.exceptions_app = self.routes to account for this.
I've then added the following to my routes:
get "/not-found", :to => "errors#not_found"
get "/unacceptable", :to => "errors#unacceptable"
get "/internal-error", :to => "errors#internal_error"
And the errors controller looks like so:
class ErrorsController < ApplicationController
layout 'errors'
def not_found
render :status => 404
end
def unacceptable
render :status => 422
end
def internal_error
render :status => 500
end
end
Going to /not-found shows the template as it should be, though visiting any non-existing URL (i.e. /i-dont-exist) renders an empty page.
The only reason I could see for this would be that the exception handling needs the routes to be, for example, get "/404", :to => "errors#not_found", though, ironically, it's not finding the route for /404 (and no, that's not just it working :) ).
Any advice, greatly appreciated. Thanks, Steve.
It seems some setting is wrong.
Try this in your routes:
match '/404', to: 'errors#not_found', via: :all (match instead of get)
You mention that in application.rb you have config.exceptions_app = self.routes, that is good. But make sure you are restarting the server before testing your changes.
And make sure your error views files have the same name than the actions in your ErrorsController.
If you are getting any kind of (haha) error in the console, could you post it?
Do this instead:
routes.rb
%w(404 422 500).each do |code|
get code, :to => "errors#show", :code => code
end
errors_controller.rb
class ErrorsController < ApplicationController
def show
render status_code.to_s, :status => status_code
end
protected
def status_code
params[:code] || 500
end
end
inside your config/application.rb ensure you have:
module YourWebsite
class Application < Rails::Application
config.exceptions_app = self.routes
# .. more code ..
end
end
Then you will need the views, obviously ;) don't forget to remove the error pages in your public directory as well.

Rails: How do I create links like "mysite.com/fFD2Zad" instead of "mysite.com/?var=fFD2Zad"

I want users to have codes to invite other users to the website. I know how I could generate random strings but how can I make it so that each user has a link such as "mysite.com/fFD2Zad" that uses the code instead of having a bulky link like "mysite.com/?var=fFD2Zad"?
Rails.application.routes.draw do
get '/:invitation_code', to: 'users#welcome'
end
class UsersController < ApplicationController
def welcome
p params
end
end
Check yourserver.com/fFD2Zad
#=> {"controller"=>"users", "action"=>"welcome", "invitation_code"=>"fFD2Zad"}
You can add the lowest priority route in the end of routes.rb:
get '/:user_code', to: 'users#profile', user_code: /[a-zA-Z0-9]{7}/
And process it in your controller:
def profile
# => params[:user_code]
...
end

Advanced Routing with Rails3

I want to use regular expressions inside my routes. I have an Products controller, but I want a different URL structure to access the products
http://host/this/
http://host/that/
http://host/andthat/
These URLs should call a action in my controller (Products:show_category(:category))
Is something like this possible?
match "(this|that|andthat)" => "products#show_category", :category => $1
the action should look like this
def show_category
puts params[:category] # <-- "this" if http://host/this/ is called
# ...
end
I haven't actually tested it, but try out:
match ':category' => 'products#show_category', :constraints => { :category => /this|that|andthat/ }
I'm not too sure if this answers your question, but you could add a collection to routes.rb:
resources :products do
collection do
get :category1
get :category2
get :category3
end
end
If you then run rake routes, you'll see that you have urls like /products/category1 and products/category2. Category1, 2 and 3 can be defined in your controller as usual:
def category1
#custom code here
end
def category2
#custom code here
end
def category3
#custom code here
end
As I said, I'm not too sure if that's what you're looking to do, but hope that helps a bit!

Resources