how router works in ROR 3.0? - ruby-on-rails

HI i am new to rails i started working on one existing project. i want to integrate new page to website. I created new page under folder Sites(page name xxxx.html.erb) which have controller named sites.and created method within controller
def xxxx
end
when i tried access that page following error is showing
The action 'show' could not be found for SitesController
i think it is problem with routes.rb file please help me
i tried to add this
match "xxxx" => "Sites#xxxx" but not working .......

You need to create a controller named it as SitesController, in this controller you can add the xxxx action.
2 . Then you need add routes in config/routes.rb file. For your reference http://guides.rubyonrails.org/routing.html

Sorry all i got answer problem with i used capital "S" in Sites routes file and used capital "s" in link also <%= link_to "Profile", :controller => "**Sites**", :action => "xxxxx"%>

Related

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

What action after "submit"?

In rails after pressing submit button in "new" I automatically come to "create" (similar after "edit" to "update"). All my params getting in "new" available in "create". This is by default.
If I have "export" instead of "new" and "process_export" instead of "create" - how reach similar effect?
If I get you right than you don't want to send the form to the default create action but rather to another, in your example to a process_export action.
All you have to do is to create a route in your routes.rb and set a custom form action url.
Example:
routes.rb
get '/process_export' => 'your_controller#export' # replace `your_controller` with your controller name
post '/process_export' => 'your_controller#process_export', as: :process_export
In your export view:
<%= form_for :resource, url: process_export_path do |f| %> <!-- Replace resource with your proper resource -->
<!-- Your form here -->
<% end %>
But I highly recommend you to observe the conventions of REST. It makes your live a lot easier.
Here are two resources which explain it in more detail:
Rails Routing: http://guides.rubyonrails.org/routing.html
Form Helpers: http://guides.rubyonrails.org/form_helpers.html
Rails follow the Convention over Configuration concept . The 7 methods of REST are automatically called from the controller when a similar request comes in the Rails 4 . For ex : the GET request will call new method , POST will call create method , DELETE will call destroy method etc .
Now , if you are creating a custom method in your controller like export and if you want to call it after the clicking on the submit button , you have to set routes accordingly in the routes.rb file . Which can be done as :
post "/chats" => "chats#export"
Here , chats is the controller and export is the method in that controller which you want to call on the submit action.
You can do :
$ > rails g scaffold Controller_name
and this will generate all the 7 REST methods in your controller automatically and similar routes are generated which you can check by doing :
$ > rake routes
I hope this helps.

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.

simple link_to in rails

I have a page app/views/new/news.html.erb and I simply want to link to this page from within my layouts/application.html.erb. Can someone please help! I have been struggling with this all morning.
I have tried things like <%= link_to "News", ... But I'm not really sure where to go from there.
You don't "link" to a view, you link to a controller which renders that view. Then, you'll need an entry in your routes.rb to wire up the url routing for that controller. If you have a controller named NewsController with a method called index and an entry in your routes.rb that looks like resources :news the following link_to should work: link_to "News", news_path.
In case it's not clear, the index method in your NewsController needs to have render :news in it.
Sounds like you may want to check out the guide on this topic: http://guides.rubyonrails.org/routing.html
If you have it setup correctly you should have a plural for your controller (i.e. news instead of new) and the following should work:
<%= link_to 'News', :controller => "news", :action => :news %>
This is assuming you are using scaffold.
If are adding this folder and page manually: for dynamic page, you have to create an action in your controller. For static page, you have to put it in your public folder.
You can always run
rake routes > routes.txt
in your application directory, which will dump a list of all routes into a txt file. Choose path that leads to action and controller you want, and then supply it as a param for link_to method :)

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