我创建了一个控制器并添加了默认的7个动作方法,我还创建了4个视图,其中包括index,new,show,edit all包含difeent数据
also added resources :posts controller configuration into my routing file as well
'
My problem is when i try to navigate to index or edit views using url .. it displays me only show views , however index method is working as a default
below are my urls `enter code here`
1。 http://localhost:3000/posts/index[ ^]
如果我试试这个,它会给我显示视图数据
这给了我正确的索引数据(默认情况下没有提到索引操作名称)
这个但总是向我显示在展示视图中显示的数据,而不是在编辑视图中显示的数据
请建议
以下是我路线档案中的几行。
Hide Copy Code
Rails.application.routes.draw do
get 'home/index'
resources :posts
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'home#index'
答案 0 :(得分:1)
请阅读Rails guide entry on default resource routing,此处没有任何异常。
/posts
是您访问posts#index
的方式,而不是/posts/index
。
对于显示和编辑,您需要在URL中使用您的帖子ID,以便Rails知道要检索的记录,即/posts/1/edit
答案 1 :(得分:0)
您可以从控制台运行rake routes
以查看所有路线。 resources :posts
的结果应该是以下七条路线:
Prefix Verb URI Pattern Controller#Action
POST /posts(.:format) posts#create
new_sheet GET /posts/new(.:format) posts#new
edit_sheet GET /posts/:id/edit(.:format) posts#edit
sheet GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
Rails会将URL与URI模式匹配,并采取匹配的控制器操作。顺便说一下,您可以使用rake routes CONTROLLER=posts
查看posts
的路线。