Home>

rails 6.0.0
ruby '2.6.5'

I've nested the routing, but I can't get the routing id well.
Currently I want to set it to http: // localhost: 3000/users/id /.
Is it an error at http: // localhost: 3000/users?

NameError in UsersController # index
undefined local variable or method `id'for #

I think that the description of the controller is wrong because the id of @user cannot be retrieved. I tried trial and error, but it didn't work, so could anyone give me some advice? Is there a way to describe the view?

Rails.application.routes.draw do
  devise_for: doctors, controllers: {
    sessions:'doctors/sessions',
    passwords:'doctors/passwords',
    registrations:'doctors/registrations'
  }
  devise_for: users, controllers: {
    sessions:'users/sessions',
    passwords:'users/passwords',
    registrations:'users/registrations'
  }
  resources: doctors, only: [: index,: show]
  root to:'healths # index'
  resources: users do
    resources: medicines, only: [: create,: new,: index]
  end
end
class UsersController</pre>
<pre><code>
  
 <% = image_tag "remote.jpg", class: "main-pict"%>
  <% if user_signed_in?&&current_user.id%>
    
    <% = link_to destroy_user_session_path, method:: delete do%>
      
      <p>Log out</p>
      
    <% end%>
    <% = link_to new_user_medicine_path (@ user.id), class: "post" do%>
       
      <p>Post</p>
      
    <% end%>
     
  <% else%>
     
    <% = link_to new_user_session_path do%>
      
      <p>Login</p>
      
    <% end%>
    <% = link_to new_user_registration_path do%>
      
      <p>New member registration</p>
      
    <% end%>
     
  <% end%>
  
class User

class Medicine

`` ```

  • Answer # 1

    What is @user = User.find (params [id]) in the def index? ?? However, the error in this part is.@user = User.find (params [: id])It will heal with.
    But maybe it will be find (nil) so you will get another error in the same place.

    What do you want to do with index? The view on it is index ?.
    It looks like a new medicine.

    Normally, the user index lists users, so in that case @ users = User.all written by no1knows.

  • Answer # 2

    In general, this is fine for the controller, but if that doesn't work, add index.html.erb to the question as well.

    def index
        @users = User.all
      end