如何使用link.to在Rails中创建链接

时间:2013-12-14 03:18:18

标签: ruby-on-rails ruby devise

我正在做一个博客来学习Rails,使用像这样的入门教程

http://guides.rubyonrails.org/getting_started.html http://guides.rubyonrails.org/routing.html

我已经设法做了帖子部分,我也做了一个管理员/帖子部分,这是现在的问题..

系统“冲突”,管理员访问domain.com/posts而不是admin / posts。

我认为问题在于我构建链接的方式..

在链接项目的教程中你做了

<h2><%= link_to post.title, post  %></h2>

我试过了

<h2><%= link_to post.title, admin_post_path  %></h2>

同样的,但我得到了

undefined local variable or method `admin_post_path' for #<#<Class:0x007fe3e990ef28>:0x007fe3e6e3b508>

这是如何工作的我的意思是我已经完成了rake路线,我看到路线,但我不能使用它们

new_user_session GET    /users/sign_in(.:format)         devise/sessions#new
            user_session POST   /users/sign_in(.:format)         devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)        devise/sessions#destroy
           user_password POST   /users/password(.:format)        devise/passwords#create
       new_user_password GET    /users/password/new(.:format)    devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)   devise/passwords#edit
                         PUT    /users/password(.:format)        devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)          registrations#cancel
       user_registration POST   /users(.:format)                 registrations#create
   new_user_registration GET    /users/sign_up(.:format)         registrations#new
  edit_user_registration GET    /users/edit(.:format)            registrations#edit
                         PUT    /users(.:format)                 registrations#update
                         DELETE /users(.:format)                 registrations#destroy
                   users GET    /admin/users(.:format)           admin/users#index
                         POST   /admin/users(.:format)           admin/users#create
                new_user GET    /admin/users/new(.:format)       admin/users#new
               edit_user GET    /admin/users/:id/edit(.:format)  admin/users#edit
                    user GET    /admin/users/:id(.:format)       admin/users#show
                         PUT    /admin/users/:id(.:format)       admin/users#update
                         DELETE /admin/users/:id(.:format)       admin/users#destroy
                   posts GET    /admin/posts(.:format)           admin/posts#index
                         POST   /admin/posts(.:format)           admin/posts#create
                new_post GET    /admin/posts/new(.:format)       admin/posts#new
               edit_post GET    /admin/posts/:id/edit(.:format)  admin/posts#edit
                    post GET    /admin/posts/:id(.:format)       admin/posts#show
                         PUT    /admin/posts/:id(.:format)       admin/posts#update
                         DELETE /admin/posts/:id(.:format)       admin/posts#destroy
                 players GET    /players(.:format)               players#index
                         POST   /players(.:format)               players#create
              new_player GET    /players/new(.:format)           players#new
             edit_player GET    /players/:id/edit(.:format)      players#edit
                  player GET    /players/:id(.:format)           players#show
                         PUT    /players/:id(.:format)           players#update
                         DELETE /players/:id(.:format)           players#destroy
            player_steps GET    /player_steps(.:format)          player_steps#index
                         POST   /player_steps(.:format)          player_steps#create
         new_player_step GET    /player_steps/new(.:format)      player_steps#new
        edit_player_step GET    /player_steps/:id/edit(.:format) player_steps#edit
             player_step GET    /player_steps/:id(.:format)      player_steps#show
                         PUT    /player_steps/:id(.:format)      player_steps#update
                         DELETE /player_steps/:id(.:format)      player_steps#destroy
             coach_steps GET    /coach_steps(.:format)           coach_steps#index
                         POST   /coach_steps(.:format)           coach_steps#create
          new_coach_step GET    /coach_steps/new(.:format)       coach_steps#new
         edit_coach_step GET    /coach_steps/:id/edit(.:format)  coach_steps#edit
              coach_step GET    /coach_steps/:id(.:format)       coach_steps#show
                         PUT    /coach_steps/:id(.:format)       coach_steps#update
                         DELETE /coach_steps/:id(.:format)       coach_steps#destroy
              candidates GET    /candidates(.:format)            candidates#index
                         POST   /candidates(.:format)            candidates#create
           new_candidate GET    /candidates/new(.:format)        candidates#new
          edit_candidate GET    /candidates/:id/edit(.:format)   candidates#edit
               candidate GET    /candidates/:id(.:format)        candidates#show
                         PUT    /candidates/:id(.:format)        candidates#update
                         DELETE /candidates/:id(.:format)        candidates#destroy
   payment_notifications GET    /payment_notifications(.:format) payment_notifications#show
                    post GET    /posts/:id(.:format)             posts#show
                   posts GET    /posts(.:format)                 posts#index
        admin_posts_path GET    /admin/posts(.:format)           admin/posts#index
        admin_posts_path POST   /admin/posts(.:format)           admin/posts#index
         admin_post_path GET    /admin/posts/:id(.:format)       admin/posts#show
     new_admin_post_path GET    /admin/posts/new(.:format)       admin/posts#new
                                /*a(.:format)                    errors#routing
                  choose GET    /user_type(.:format)             home#user_type
                    root        /                                devise/sessions#new

也尝试了这个

<h2><%= link_to post.title, url_for([@post]) %></h2>

this throw ::::提供Nil位置。无法构建URI。

=(关于这样做的任何文件,??你知道我在哪里找到它

的routes.rb

Consult::Application.routes.draw do

  devise_for :users, :controllers => { :registrations => "registrations" }

  scope "/admin" do
    resources :users, :controller => 'admin/users'
    resources :posts, :controller => 'admin/posts'
  end

  resources :players
  resources :player_steps
  resources :coach_steps
  resources :candidates
  resource  :payment_notifications, :only => :show
  #match 'candidates' => 'candidates#index'
  #resources :posts 
  get '/posts/:id', to: 'posts#show', as: 'post'

  get '/posts/', to: 'posts#index', as: 'posts'  

   get '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path' 
   post '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path' 
   get '/admin/posts/:id', to: 'admin/posts#show', as: 'admin_post_path' 
   get '/admin/posts/new', to: 'admin/posts#new', as: 'new_admin_post_path' 

  match '*a', :to => 'errors#routing'

  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

  # Sample resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  #root :to => "devise/sessions#new"

  get 'user_type', to: 'home#user_type', as: :choose

  devise_scope :user do
    root :to => "devise/sessions#new"
  end

  # See how all your routes lay out with "rake routes"



  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

1 个答案:

答案 0 :(得分:1)

尝试使用命名空间而不是范围,因为命名空间更适合嵌套。你的路线文件应该是这样的。

namespace :admin do
  resources :users
  resources :posts
end

resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resource  :payment_notifications, :only => :show
  #match 'candidates' => 'candidates#index'
resources :posts 

你可以删除

get '/posts/:id', to: 'posts#show', as: 'post'
get '/posts/', to: 'posts#index', as: 'posts'  

get '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path' 
post '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path' 
get '/admin/posts/:id', to: 'admin/posts#show', as: 'admin_post_path' 
get '/admin/posts/new', to: 'admin/posts#new', as: 'new_admin_post_path' 

作为命名空间和资源处理它们的生成

完整路线文件应

Consult::Application.routes.draw do

  devise_for :users, :controllers => { :registrations => "registrations" }

  namespace :admin do
      resources :users
      resources :posts
   end

  resources :players
  resources :player_steps
  resources :coach_steps
  resources :candidates
  resources :posts 

  resource  :payment_notifications, :only => :show
  get 'user_type', to: 'home#user_type', as: :choose

  devise_scope :user do
    root :to => "devise/sessions#new"
  end
end