This one is a two-parter. Initially I had just one controller:
$ rails generate controller home index
...and I made that my root.
Recently, though, I wanted to get the database involved. So I made a scaffold called Daily and switched the root to dailies#index instead of home#index.
That worked just fine. Everything seems to be working fine.
...only problem is that I want to use home#index for something else... but now I have no way to link to it.
This is my routes.rb file:
LiquidAdmin::Application.routes.draw do
resources :dailies
devise_for :users
get '/auth/:provider/callback', to: 'sessions#create'
get "home/bandwall"
get "home/index" :to => "home#index"
# get "dailies/index"
root :to => "dailies#index"
and these are my routes:
home_index GET /home/index(.:format) home#index
I've tried.
<% link_to "Home", home_path %>
<% link_to "Home", home_index %>
<% link_to "Home", home/index %>
Nothing will get me back to that file... even though it was working just fine when it was the root.
Now all of this could've been avoided if "Plan A" had worked...
Plan A was just to make this Daily scaffold and do something like:
<% #dailies.each do |daily| %>
This worked as a link from the Daily index... but not as a link from home#index. Which is why I switched the root. I was getting annoyed and it was easier.
So my questions: 1. How do I link to the old home#index? 2. How should I have queried the dailies tables from home#index?
Assuming you have a Daily model.
routes.rb
resources :dailies
get "home/index"
Running rake routes:
dailies GET /dailies(.:format) dailies#index
POST /dailies(.:format) dailies#create
new_daily GET /dailies/new(.:format) dailies#new
edit_daily GET /dailies/:id/edit(.:format) dailies#edit
daily GET /dailies/:id(.:format) dailies#show
PUT /dailies/:id(.:format) dailies#update
DELETE /dailies/:id(.:format) dailies#destroy
home_index GET /home/index(.:format) home#index
app/controllers/dailies_controller.rb
class DailiesController
def index
#dailies = Daily.all
end
end
app/views/dailies/index.html.erb
<%= link_to "Home", home_index_path %>
<% #dailies.each do |daily| %>
<%= link_to 'A daily', daily_path(daily) %>
<% end %>
Related
I am beginner to ror, I am trying dynamic page in my application, while I click the about link in my application I got a routing error(as said in title). Here is my routes.rb file
Rails.application.routes.draw do
devise_for :users
resources :userdetails do
collection {post :import}
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "userdetails#index"
get 'about', to:'userdetails#about'
devise_scope :users do
get 'sign_in', to: 'devise/sessions#new'
get 'sign_out', to: 'devise/sessions#destroy'
end
end
here is index.html.erb file:
<div class="topnav">
Home
News
Contact
<a href="about_path" data-method = "get" >About Us</a>
...
</div>
Please help me identify my mistake.....
your routes says: -
get '/about', to: 'userdetails#about', as: :user_about
so according to this
About Us
Instead of this you can use rails view helper which fill convert this to html anchor tag: -
<%= link_to 'About Us', user_about_path%>
As you are beginner so you should go though this rails Routing for better clarification.
Note: - by default link is GET type http verb so you don't need to mention data-method = "get"
and you can see all your routes at console by running command rake routes
When you run rails routes or rake routes it will generate like this
about GET /about(.:format) userdetails#about
That means your path is about_path then you can call out like this
About Us
or like this HTML
About Us
or like this .erb
<%= link_to 'About Us', about_path %>
While your routes like exist
get 'about', to:'userdetails#about'
Here you have defined method get that's why this is unnecessary to HTML call out data-method = "get".
Hope it will work
I'm trying to add routes but the link in my view does not work, I have the message
undefined local variable or method `styles_path' for #<#<Class:0x007fea4e48a3d8>:0x007fea56135c98>
my routes :
Rails.application.routes.draw do
get '/styles/:id' => 'spree/spreepages#show_taxonomy', as: 'show_taxonomy'
get '/styles' => 'spree/spreepages#choose_style', as: 'styles'
mount Spree::Core::Engine, at: '/'
root to: 'pages#home'
end
and my view
<div class="col-sm-2 choose-style-steps">
<%= link_to "", styles_path do %><div class="steps step-one">1</div><% end %>
<%= link_to('#') do %><div class="steps step-two">2</div><% end %>
<%= link_to('#') do %><div class="steps step-three">3</div><% end %>
</div>
</div>
I try with spree.styles_path but it does not work
Output of rails routes
Prefix Verb URI Pattern Controller#Action
spree / Spree::Core::Engine
root GET / pages#home
show_taxonomy GET /styles/:id(.:format) spreepages#show_taxonomy
styles GET /styles(.:format) spreepages#choose_style
Routes for Spree::Core::Engine:
locales GET /locales(.:format) spree/locale#index
set_locale POST /locale/set(.:format) spree/locale#set {:format=>:json}
skrill_cancel_order_checkout GET /orders/:order_id/checkout/skrill_cancel(.:format) spree/checkout#skrill_cancel
skrill_return_order_checkout GET /orders/:order_id/checkout/skrill_return(.:format) spree/checkout#skrill_return
new_order_checkout GET /orders/:order_id/checkout/new(.:format) spree/checkout#new
And some other Spree Route
Thank's for your help
I know it is too late but for future use.
In main_app you will get all the details about your project. So you can use it as per below,
link_to main_app.styles_url
or
link_to main_app.styles_path
I found it, just put the url in place of path like
link_to styles_url
I have an app with subdomains for various people that all share the same domain (i.e. www.liz.domain.com, www.anthony.domain.com, etc.). I need to create links from one subdomain to the main domain and back, but can't figure out how to make it work. My routes stand like this:
Rails.application.routes.draw do
constraints subdomain: 'liz' do
scope module: 'liz', as: 'liz' do
get 'home/index'
root 'home#index'
resources :inquiries
get 'services/hire'
get 'services/dev'
get 'services/design'
get 'services/branding'
get 'services/portfolio'
end
end
constraints subdomain: 'anthony' do
scope module: 'anthony', as: 'anthony' do
get 'home/index'
root 'home#index'
end
end
get 'home/index'
root 'home#index'
end
And when I rake routes it turns out like this:
Prefix Verb URI Pattern Controller#Action
liz_home_index GET /home/index(.:format) liz/home#index {:subdomain=>"liz"}
liz_root GET / liz/home#index {:subdomain=>"liz"}
liz_inquiries GET /inquiries(.:format) liz/inquiries#index {:subdomain=>"liz"}
POST /inquiries(.:format) liz/inquiries#create {:subdomain=>"liz"}
new_liz_inquiry GET /inquiries/new(.:format) liz/inquiries#new {:subdomain=>"liz"}
edit_liz_inquiry GET /inquiries/:id/edit(.:format) liz/inquiries#edit {:subdomain=>"liz"}
liz_inquiry GET /inquiries/:id(.:format) liz/inquiries#show {:subdomain=>"liz"}
PATCH /inquiries/:id(.:format) liz/inquiries#update {:subdomain=>"liz"}
PUT /inquiries/:id(.:format) liz/inquiries#update {:subdomain=>"liz"}
DELETE /inquiries/:id(.:format) liz/inquiries#destroy {:subdomain=>"liz"}
liz_services_hire GET /services/hire(.:format) liz/services#hire {:subdomain=>"liz"}
liz_services_dev GET /services/dev(.:format) liz/services#dev {:subdomain=>"liz"}
liz_services_design GET /services/design(.:format) liz/services#design {:subdomain=>"liz"}
liz_services_branding GET /services/branding(.:format) liz/services#branding {:subdomain=>"liz"}
liz_services_portfolio GET /services/portfolio(.:format) liz/services#portfolio {:subdomain=>"liz"}
anthony_home_index GET /home/index(.:format) anthony/home#index {:subdomain=>"anthony"}
anthony_root GET / anthony/home#index {:subdomain=>"anthony"}
home_index GET /home/index(.:format) home#index
root GET / home#index
I've tried using a <%= link_to "Liz's Page", liz_root_path(subdomain: 'liz') %> or <%= link_to "Liz's Page", liz_root_path %> from the main domain (without a subdomain), but neither links to the subdomain.
Similarly, I've tried <%= link_to "Main Page", root_path %> from a page inside a subdomain and it just links to the subdomain's home, not the site root.
Can anyone straighten me out on how to link back and forth between subdomains/main domains?
I eventually changed these from path to url to solve the problem.
This way <%= link_to "Liz's Page", liz_root_path(subdomain: 'liz') %> became <%= link_to "Liz's Page", liz_root_url(subdomain: 'liz') %> and worked fine.
I have Exam controller.
In routes.rb there is "resources :exams"
In controller there are REST-like generated methods.
I want to add my own method there:
def search
#exams = Exam.where("name like ?", params[:q])
end
In view file:
<%= form_tag(search_path, :method => "post") do %>
<%= label_tag(:q, "Szukaj: ") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Szukaj") %>
<% end %>
I know, there is no results presentation yet, it doesn't work at all at this moment (:
When i go to http://localhost:3000/exams/search it's mapping it to show controller and search is a :id paramether then...
How to get http://localhost:3000/exams/search to run the seach controller?
You forgot to add route. Put this in routes.rb, before resources :exams
map.search '/exams/search', :controller => :exams, :action => :search
Note, that resources :exams doesn't generate routes for all public methods of the controller, it generates very specific set of routes. You can find more information in the rails routing guide. (see section 3.2 in particular)
You'll need to add additional parameters to your mapping. You can add "collection" methods like so:
map.resources :exams, :collection => {:search => :get}
When you rake routes, you'll see that it generates something like so:
search_exams GET /exams/search(.:format) {:controller=>"exams", :action=>"search"}
exams GET /exams(.:format) {:controller=>"exams", :action=>"index"}
POST /exams(.:format) {:controller=>"exams", :action=>"create"}
new_exam GET /exams/new(.:format) {:controller=>"exams", :action=>"new"}
edit_exam GET /exams/:id/edit(.:format) {:controller=>"exams", :action=>"edit"}
exam GET /exams/:id(.:format) {:controller=>"exams", :action=>"show"}
PUT /exams/:id(.:format) {:controller=>"exams", :action=>"update"}
DELETE /exams/:id(.:format) {:controller=>"exams", :action=>"destroy"}
i am working in Ruby on rails.. i am new to this..
i have used a line
<%= link_to "about",about_path %>
which throws me a error as,
undefined local variable or method `about_path' for #<ActionView::Base:0xb5f5baa8>
i am having the page about.html under app/views/pages/
please give some suggestions of why i am getting like this .
I just had the same problem using the Hartl's tutorial. Here is what I did.
When asking rake routes, I have :
tomsihap-MBP:sample_app tomsihap$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_contact GET /static_pages/contact(.:format) static_pages#contact
Then the correct path is :
<%= link_to "About", static_pages_about_path %>
And not <%= link_to "About", about_path %> as suggested by Hartl's guide.
EDIT :
Ok now I understand. That is because routes were defined like that :
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
Instead of, later explained into the tutorial :
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
Using this way, the correct path now is :
<%= link_to "About", about_path %>
Your code is looking for what is called a named route. You need to define these in config/routes.rb. In addition you will need some controller and action to handle them. See this post describing a very simple way to handle static pages by way of illustration.
To get the about_path named route, then you would add this to routes.rb
map.about "/pages/about", :controller => "pages", :action => "show", :id => "about"
Then add your about page contents to a file called app/views/pages/about.html.erb
Finally:
$ rake routes
tells you all of the named routes defined for your application and what they do
I guess your about page is "static". Check this..
routes.rb
# rails 2.3.x
map.about "/pages", :controller => 'pages', :action => 'about'
Controllers/pages_controller.rb
class PagesController < ApplicationController
def about # not needed, only for "tidiness"
end
end
... and your erb file have to be here: Views\pages\about.html.erb
Is in your routes.rb something like map.resources :about ?
If you don't know why it should be there or what is that, read about RESTful Routing on guides.