0

我认为这是:

<% @users.each do |user| %>
  <% if user.teacher == current_user.teacher || current_user.role == "admin" %>
  <tr>
    <td><%= user.username %></td>
    <td><%= user.email %></td>
    <td><%= user.teacher %></td>
    <td><%= user.role %></td>
    <td><%= user.admin %></td>
    <td><%= link_to 'Problems', student_problems_path(user) %></td>
    <td><%= link_to 'Edit', edit_user_path(user) %></td>
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
  <% end %>
<% end %>

所以,我user作为参数传递给student_problems_path. 在我的UsersController我有:

def student_problems
    @problems = Problem.all
    @user = user.email
end

但是,Rails 告诉我以下内容:

undefined local variable or method `user' for #<UsersController:0x3d79208

如何访问从视图user传递给的变量?UsersController谢谢!

编辑

这是我的路线:

                  root        /                              problems#new
        feedback_index POST   /feedback(.:format)            feedback#create
          new_feedback GET    /feedback/new(.:format)        feedback#new
      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
edit_user_registration GET    /users/edit(.:format)          devise/registrations#ed
     user_registration PUT    /users(.:format)               devise/registrations#up
              problems GET    /problems(.:format)            problems#index
                       POST   /problems(.:format)            problems#create
           new_problem GET    /problems/new(.:format)        problems#new
          edit_problem GET    /problems/:id/edit(.:format)   problems#edit
               problem GET    /problems/:id(.:format)        problems#show
                       PUT    /problems/:id(.:format)        problems#update
                       DELETE /problems/:id(.:format)        problems#destroy
                 users GET    /users(.:format)               users#index
                       POST   /users(.:format)               users#create
              new_user GET    /users/new(.:format)           users#new
             edit_user GET    /users/:id/edit(.:format)      users#edit
                  user GET    /users/:id(.:format)           users#show
                       PUT    /users/:id(.:format)           users#update
                       DELETE /users/:id(.:format)           users#destroy
            help_about GET    /help/about(.:format)          help#about
          problems_new GET    /problems/new(.:format)        problems#new
                  data        /data(.:format)                problems#data
      student_problems        /student_problems(.:format)    users#student_problems
4

2 回答 2

0

不知道您的问题的详细信息,但是如果您想在 student_problems 操作中获取用户的 ID,那么您可以使用以下代码:

@user_id = params[:id]

如果您想要更多解决方案,请使用代码指定问题详细信息。谢谢

于 2013-05-02T04:52:45.957 回答
0

请执行下列操作:

# pass the following to your helper:
student_problems_path(user_email: user.email)

# now the URL will be /student_problems?user_email=user@gmail.com
# and in the controller do:

def student_problems
  @user = params[:user_email]
  @problems = Problem.all
end
于 2013-05-02T04:42:11.183 回答