在rails上使用rake路由的routes.rb信息

时间:2013-12-14 01:54:10

标签: ruby-on-rails ruby ruby-on-rails-3

嗨我正在学习Rails我需要了解routes.rb文件我已阅读http://guides.rubyonrails.org/routing.html,这非常有用,无论如何。

我正在尝试

<% @posts.each do |post| %>

<div id="entrada">  

<h2><%= link_to post.title, post %></h2>
<% if post.postimage.present? %>
       <%= link_to (image_tag post.postimage.url(:thumb)), (post_path(post))  %>
     <% else %>
     <%= link_to (image_tag ("espanol/playersample.png")), (post_path(post))  %>

     <% end %>
<p><%= simple_format h(post.text) %></p>
<table>
     <tr>
    <td><%= link_to t('generales.ver'), post %></td>
    <td><%= link_to t('generales.editar'), edit_post_path(post) %></td>
    <td><%= link_to t('generales.delete'), post_path(post),
                    method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
    </table>
</div>
<% end %>

这个问题。是<%= link_to post.title, post %>这会产生类似

的网址

domain.com/posts/?id

它还可以,但我是管理员,所以它应该是domain.com/admin/posts/?id

我几乎尝试了所有事情,比如做<%= link_to post.title, admin_post_path %>

但这有一个错误的语法错误,对你来说可能很明显,我需要理解为什么。

我的佣金路线抛出:

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

我的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

如何将link_to设置为正确的domain.com/admin/posts/?id

2 个答案:

答案 0 :(得分:1)

您只想使用post_path

背后的原因是来自rake routes

的这一行
post GET    /admin/posts/:id(.:format)       admin/posts#show

阅读方式如下。

post是路径的名称,IE,您可以使用post_path

GET是访问路线的方法。

/admin/posts/:id(.:format)是您将在浏览器中看到的网址。 :id将替换为整数,(.:format)是可选格式。 IE json,xml等

admin/posts#show分别是要使用的文件夹,控制器和方法。

答案 1 :(得分:1)

在路线文件上,我会替换以下内容:

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' 

由此:

namespace :admin do
  resources :posts
end

然后链接应该像这样工作:

<%= link_to post.title, admin_post(post) %>

如果您的Post模型已在app / models / admin / Post.rb等管理文件夹中创建,则使用&lt;%= link_to post.title,post%&gt;应该按你的意愿工作