mystic cola
mystic cola

Reputation: 1643

Ruby on Rails - Route to home#index

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?

Upvotes: 0

Views: 7478

Answers (1)

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

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 %>

Upvotes: 3

Related Questions