Question

I feel like I'm really close so I'll get to the point. I've created a new view file (app/views/subjects/screening.html.erb). That would place the new file next to the edit.html.erb, which I used as my template. My edit.html.erb file works as intended, which is really weird because I've copied the logic/routes/etc for my new file, and I'm not getting the same results.

This is frustrating. The MVC is being mean.

So here is the link to the page:

subjects/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 %>

Where the link to "edit" works, producing this URL 'localhost:3000/subjects/{:id}/edit'. And the link to "scr" does not, producing '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'

Do I have to declare something extra because I added it manually? Any help would be greatly appreciated. Thanks.

Was it helpful?

Solution

I would suggest a nested route like so:

resources :subjects do 
  match '/screening', to: 'subjects#screening', as: :screening
end

Which should allow:

link_to "scr", subject_screening_path(sub)
#> /subjects/1/screening

I could be a little off with the naming. Be sure to check your routes (rake routes in shell). Also, remove:

  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'

These routes are automatically generated with resources :subjects, though by default their paths differ.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top