我觉得我真的很亲密,所以我会明白这一点。我创建了一个新的视图文件(app / views / subjects / screening.html.erb)。这会将新文件放在edit.html.erb旁边,我将其用作模板。我的edit.html.erb文件按预期工作,这非常奇怪,因为我已经为我的新文件复制了逻辑/路由/等等,而且我没有得到相同的结果。
这令人沮丧。 MVC是卑鄙的。
所以这是指向页面的链接:
受试者/ index.html.erb
<% @subjects.each do |sub| %>
<tr>
<td><%= link_to "edit", edit_subject_path(sub) %></td>
<td><%= link_to "scr", screening_path(sub) %></td>
<td><%= sub.subject_id %></td>
<td><%= sub.study_site == 1? "Seattle" : sub.study_site == 2? "Portland" : "nil"%></td>
<td><%= sub.treatment_group %></td>
</tr>
<% end %>
如果“编辑”链接有效,则生成此网址“localhost:3000 / subjects / {:id} / edit”。并且“scr”的链接没有,生成'localhost:3000 / screening。{:id}'。
的routes.rb
resources :users
resources :sessions, only: [:new, :create, :destroy]
# resources :subjects, only: [:new, :create, :destroy]
resources :subjects
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/edit', to: 'users#edit'
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/subjects', to: 'subjects#show'
match '/newsubject', to: 'subjects#new'
match '/subjects', to: 'subjects#index'
match '/showsubject', to: 'subjects#show'
match '/editsubject', to: 'subjects#edit'
match '/screening', to: 'subjects#screening'
我是否必须申报额外的东西,因为我手动添加了它?任何帮助将不胜感激。感谢。
答案 0 :(得分:1)
我会建议像这样的嵌套路线:
resources :subjects do
match '/screening', to: 'subjects#screening', as: :screening
end
应该允许:
link_to "scr", subject_screening_path(sub)
#> /subjects/1/screening
我的命名可能有点偏差。请务必检查您的路线(shell中的rake routes
)。另外,删除:
match '/subjects', to: 'subjects#show'
match '/newsubject', to: 'subjects#new'
match '/subjects', to: 'subjects#index'
match '/showsubject', to: 'subjects#show'
match '/editsubject', to: 'subjects#edit'
match '/screening', to: 'subjects#screening'
这些路线是使用resources :subjects
自动生成的,但默认情况下它们的路径不同。