静态页面控制器的路由问题

时间:2015-03-03 06:44:18

标签: ruby-on-rails ruby routes

我有两个用户类型的主页:home.html.erb(对于用户)和shop_home.html.erb(对于商店)。当我点击网站标题中的主页链接时,"用户"登录后,它通过静态页面控制器中的home方法将用户路由到home.html.erb。我虽然无法路由到shop_user.html.erb。 shop_home路线不起作用。

shop_header.erb

<% if shop_logged_in? %>
  <li id="blacklink"><%= link_to "Home", shop_home %></li>
  <li id="blacklink"><%= link_to "Drivers", users_path %></li>
  <li id="blacklink"><%= link_to "Log out", shop_logout_path, method: "delete" %></li>  
<% end %>

点击&#34; Home&#34;链接给了我undefined local variable or method 'shop_home' 我需要做些什么才能将主页链接路由到shop_home.html.erb

的routes.rb

Rails.application.routes.draw do

  get 'shop_sessions/new'

  get 'shops/new'

  get 'cars/new'

  get 'sessions/new'

  get 'users/new'

  root             'static_pages#home'
  get 'shophome' => 'static_pages#shop_home', as:"shop_home"
  get 'shop', to: 'static_pages#shop_home', as: 'shop_root'
  get 'shops', to: 'shops#index', as: 'shop_index'
  get 'help'    => 'static_pages#help'
  get 'about'   => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup'  => 'users#new'
  get 'shopsignup' => 'shops#new'
  get    'login'   => 'sessions#new'
  post   'login'   => 'sessions#create'
  get    'shoplogin'   => 'shop_sessions#new', as: 'shop_login'
  post   'shoplogin'   => 'shop_sessions#create'
  delete 'logout'  => 'sessions#destroy'
  delete 'shoplogout'  => 'shop_sessions#destroy', as: 'shop_logout'

  resources :users
  resources :shops
  resources :cars,  only: [:create, :destroy, :edit]
end

staticpagescontroller.rb

class StaticPagesController < ApplicationController

  def home
    if logged_in?
      @car = current_user.cars.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end

  def shop_home
    @shop = current_shop
  end
end

1 个答案:

答案 0 :(得分:0)

您需要将_path添加到有缺陷的link_to的末尾。

所以改变这一行:

  <li id="blacklink"><%= link_to "Home", shop_home %></li>

对此:

  <li id="blacklink"><%= link_to "Home", shop_home_path %></li>