以用户身份(管理员或学生)登录时,以角色(讲师)预订用户ID

时间:2018-04-20 15:42:44

标签: ruby-on-rails

我正在尝试以学生(用户)身份登录时预约教师。 登录MODEL是具有[讲师] [学生] [管理员]的可选角色的用户。

我一直收到此错误

#的未定义方法`id' error message 按钮给我错误..

如何以学生等用户身份登录时,如何声明教师user_id的预订

instructors_controller.rb

# GET /instructors
# GET /instructors.json
def index
  #@instructors = Instructor.all
   @instructor_users = User.where(role: 'instructor')
end

讲师/索引

<h1>List Instructors</h1>

<table>
<tr>
  <th>ID</th>
  <th>Name</th>
  <th>Role</th>
</tr>

<% @instructor_users.each do |user| %>

  <tr>
  <td><%= user.id %></td>
  <td><%= user.name %></td>
  <td><%= user.role %></td>
  <td>
  <%= button_to 'Book a Lesson', {controller: 'lessons', action: 'new', id: @user.id } , {class: "button-to"} %>
    </td>
  </tr>
<% end %>
</table>


<br>

lessons_controller.rb

def new
    if logged_in?
    #@lesson = Lesson.new
    @lesson = Lesson.new(user_id: params[:id], name: User.find(session[:user_id]).name  

    session[:return_to] = nil       
else            
    session[:return_to] = request.url           
    redirect_to login_path, alert: " 'You need to login to book a lesson' " 
end
end



# POST /lessons
# POST /lessons.json
def create
@lesson = Lesson.new(lesson_params)
  if @lesson.save
      instructor = User.find(@instructor.user.id)
      redirect_to lesson, notice: 'Lesson was successfully created.'
  else
    render :new
  end
 end

1 个答案:

答案 0 :(得分:0)

更改

  

@ instructor_user.id

  

user.id

因为@instructor_users.each do |user| to user