configure route to page that is in subfolder rails - ruby-on-rails

I have a design.html.erb file in folder views/services/
How do I configure route to this page in routes.rb?
(I have a controller for static pages called static_pages_controller but I don't need any backend actions)
I need something like this:
get '/services/design' => 'static_pages#what_to_write_here'

get '/services/design' => 'static_pages#design'
This would render the design.html.erb inside views/static_pages/ directory.
In order to specify another view in another sub-folder you have to specify in your action:
def design
render 'services/design'
end
This would render the design.html.erb inside views/services/ directory.

get '/services/design', to: 'static_pages#design'
of course if you have method design in your static_pages_controller.rb

I would suggest to use high_voltage gem.

Related

Why am I getting this Ruby on Rails Error?: #index is missing a template for request formats: text/html

I am a beginner to Ruby on Rails and just web app development in general. I am having a lot of trouble trying to simply display another page. Here are my codes.
Routes file:
Rails.application.routes.draw do
root 'pages#home'
get '/recent', controller: 'recent', action: 'index'
end
Controller File:
class RecentController < ApplicationController
def index
end
end
and the "recent" html.erb file (Its directory is app>views>recent>recent.html.erb):
<h2>Recent Recipes!</h2>
Any help would be appreciated. I really tried to understand what is wrong, but I can't seem to figure it out. Thanks!
Usually when you create an action, you must have a view (erb) with the same name, so, in this case you can modify the name of recent.html.erb to index.html.erb
Also you can modify the action in your routes from index to recent, and do the same change in the RecentController.
By default Rails expects a view template in the folder with same name as the controller named after the controller's action. If you rename recent.html.erb to index.html.erb it should be ok

Setting default page for Controller in Rails

In rails how to set the default page for controller. In my application I have a controller named "greet" which have two actions "welcome"
and "wishes". So while calling the welcome page like "localhost:3000/greet/welcome" is properly worked.
But My requirement is if I didn't
give the action name for that controller like "localhost:3000/greet", then it takes the default page associated for that controller only. How to do this
in rails 4.2. I tried to make an index action within greet controller. But it didn't work. Can anyone help me to solve this problem ?
in your routes.rb add line:
get '/greet' => 'greet#welcome'
you must also in folder view create folder greet and in this folder you have to create file welcome.html.erb
Rails work with REST concept. So, according to this when you just call localhost:3000/greet it will search greet#index method. Well, If you want to see any custom method while usinglocalhost:3000/greet, you will need to write in file config/routes.rb like:
Rails.application.routes.draw do
get 'greet', :to => 'greet#welcome', :as => :greet
end
Hope this will help.
Try this.
get '/greet', to: 'greet#welcome'
Rails.application.routes.draw do
resource :greet, controller: 'greet' do
get 'welcome'
get 'wishes'
#Default resource routing
get '/', to: 'greet#welcome'
end
end

Understanding routing with rails

I am trying to make a stupid simple CMS for one of my clients using rails. I generated scaffolding for Page and have been successful at creating and rendering those pages, however, the routing is ugly.
Right now, if I want to view the home page, I get a url like this: example.com/pages/1
I'd like to accomplish 2 things:
How do I set the routing so that example.com automagically grabs the page named "home"
and
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
Q1:
How do I set the routing so that example.com automagically grabs the page named "home"
In `routes.rb:
root :to => '[controller]#[action]'
#'pages#home' for example, if your home page is in `pages_controller`
# and uses the `home` action
Q2:
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
match '/page/:name', :to => 'pages#some_name', :as => :some_name
this would generate the following in $rake routes:
some_name /page/:name(.:format) pages#some_name
when you link to (or redirect, or otherwise access) this page, you'd write
link_to "click this link!", some_name_path(#SomeModel.some_name)
To accomplish the first thing you need to open the file routes.rb which is in the config folder and add the following:
root to: "home#index"
I'm assuming you have a controller named home which contains a method called index and this is the one that displays the home page.
If you want to make it the same as example.com/pages then you would have to use
root to: "pages#index"
to make a general rule you need to use
root to: "controller#method"
Don't forget to remove the index.html from the public folder too.
I recommend you make the blog application presented here:
http://guides.rubyonrails.org/getting_started.html
It can help you understand more.
Here's a solution that assumes your controller is named pages_controller instead of page_controller.
Add these routes in config/routes.rb:
root to: 'pages#show', page_name: 'home'
get 'pages/:page_name', to: 'pages#show'
For the controller app/controllers/pages_controller.rb:
class PagesController < ApplicationController
def show
#page = Page.find_by(name: params[:page_name])
end
end
Note: In rails4 the find_by dynamic finders have been deprecated... so if you're app is on 4 you should look into updating them. These docs have further details.
Also, if you're just trying to get static looking urls then I would definitely go with Marian Theisen's suggestion and use friendly_id.

Identical Files behave differently due to link with controller

I am building my first app with ROR and stumbled upon a couple of problems due to my understanding of the MVC
I have a page to add a new item, and this works fine, rails magically hooks it up to the items controller and somehow by magic it knows to look in the method 'new' as the page is called new.
But this layer is confusing me, as i need to now create a different version of new, same functionality but with a different look so to use a different layout to application.html.erb
So i attempt to create a copy of new.html.erb and create bookmarklet.html.erb - both contain exactly the same code: a link to a form. but of course bookmarklet will error on me because it does not have that link in the controller - how do i 'wire' up bookmarklet so that i can call the new method and so that it can behave in a similar way to the identical new.html.erb
Also, how can i tell the new bookmarklet.html.erb to ignore the application.html.erb and get its layout from another file?
thanks in advance
The magic happens in the routes. Rails uses something called RESTful routes, which is taking HTTP verbs and assigning standard actions to it. the new action is a GET request in HTTP speak, and if you are using scaffolding or following REST, will have the ruby call to build a new object in the controller, as an instance variable so you can use it in your view.
You have to tell rails routes that you want to BREAK this arrangement and to let /items/bookmarklet be used in the controller.
In your routes.rb file replace resources :items with
resources items do
member do
get 'bookmarklet'
end
end
In your controller put:
def bookmarklet
#item = Item.new
render :template => "bookmarklet", :layout => "different_layout" # or you can put this on the top of the controller, which is my style, but whatevs.
end
You should look into partials, if you are doing this as they clean up your code immensely.
A better way to think of things is to start with the controller instead of the view html.erb files. So each public method in your controller is effectively a page or action in the site. When you need a new action or page, add the method to the controller first. Then create the relevant view files.
So in your controller you'll need something like:
def bookmarklet
#item = Item.new(params[:item])
#item.save
render :template => "items/bookmarklet.html.erb", :layout => "different_layout.html.erb"
end
Normally you don't need to call render manually in the controller, but since you want a different layout than the default you need to specify it using render.

rails view without a controller

Can rails handle creating a view without a controller? For example, let say I have a page that just links to other pages, would I need to create a dummy controller for that, or could I just do something in my routes file?
I like August's answer but I have a slightly different method.
Let's say you want to add
/any/path/somefile.html.erb
but not add a controller...
You can just add folder to views called "application", create your file in that directory..
Then in your routes file just add
match '/any/path/somefile' => 'application#somefile'
Your erb still evaluates, you get your layout, and you can create any path you want...
(all this does is remove the need for the pages controller)
Hope it helps...
No. All requests has to go through a controller.
I like to have a PagesController, with map.page ":action", :controller => "pages". That way, I can create app/views/pages/foo.erb and have it available on /foo without any extra code.
Another option would be adding a static html file in your /public directory if you truly don't need it as part of your application.
If you are a brave soul. You can try edge rails 3. Katz demonstrated this possibility on his blog. Here is the link:
http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/
No. All requests have to go through a controller.
If you have a page like index.html.erb and contact.html.erb in your view folder. You need to create a dummy controller called contact. Then you can link to the contact.html.erb from the index.html.erb. And give the link as <%= link_to 'contact', :controller => "ads", :action => "contact" %> here "ads"->controller name.

Resources