Ruby on rails 对教师的评分不';在用户登录并进行分级后不会显示

Ruby on rails 对教师的评分不';在用户登录并进行分级后不会显示,ruby-on-rails,Ruby On Rails,在我的rails项目中,用户只能在其老师已经注册并登录的情况下对其进行评分。然而,在用户登录并给老师评分后,我看不到他的评分。我不知道是什么引起了这个问题。如有任何建议,将不胜感激 这是我的评级\u控制器。rb: class RatingsController < ApplicationController before_action :authenticate_user! before_action :get_teacher def new get_teacher

在我的rails项目中,用户只能在其老师已经注册并登录的情况下对其进行评分。然而,在用户登录并给老师评分后,我看不到他的评分。我不知道是什么引起了这个问题。如有任何建议,将不胜感激

这是我的评级\u控制器。rb

class RatingsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_teacher

  def new
    get_teacher
    @rating = current_user.ratings.build
  end

  def create
    get_teacher
    @rating = current_user.ratings.create(rating_params)
    if @rating.save
      redirect_to school_teacher_path(@teacher.school, @teacher)
    else
      render 'new'
    end
  end

  def destroy
    get_teacher
    @rating = @teacher.ratings.find(params[:id])
    @rating.destroy

    redirect_to school_teacher_path(@teacher.school, @teacher)
  end

  def get_teacher
    @teacher = Teacher.find(params[:teacher_id])
  end

  private

    def rating_params
      params.require(:rating).permit(:easiness, :helpfulness, :clarity, :comment,
      :teacher_id, :school_id)
    end
end
<!-- Caculate the average rating of the teacher -->
<h1>Average ratings:</h1>
<p>Clarity:
  <%= @teacher.ratings.average(:clarity) %>
</p>
<p>Easiness:
  <%= @teacher.ratings.average(:easiness) %>
</p>
<p>Helpfulness:
  <%= @teacher.ratings.average(:helpfulness) %>
</p>
<hr>

<!-- Show all the ratings -->
<h2>All the ratings:</h2>
<div>
  <%= @teacher.ratings.each do |rating| %>

    <p>Clarity:
      <%= rating.clarity %>
    </p>

    <p>Helpfulness:
      <%= rating.helpfulness %>
    </p>

    <p>Easiness:
      <%= rating.easiness %>
    </p>

    <p>Comment:
      <%= rating.comment %>
    </p>

    <%= link_to "Delete rating", [rating.teacher, rating], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-warning" %>

    <hr>
  <% end %>
</div>

<p>
  <%= link_to "Rate teacher", new_teacher_rating_path(@teacher), class: "btn btn-primary" %>
</p>

<p>
  <%= link_to "Back to school", school_path(@school), class: "btn btn-primary" %>
</p>
<h1>Teacher Rating</h1>
<%= form_for([@teacher, @rating]) do |f| %>
  <p>
    <%= f.label :clarity %>
    <%= f.text_field :clarity %>
  </p>

  <p>
    <%= f.label :easiness %>
    <%= f.text_field :easiness %>
  </p>

  <p>
    <%= f.label :helpfulness %>
    <%= f.text_field :helpfulness %>
  </p>

  <p>
    <%= f.label :comment %>
    <br>
    <%= f.text_area :comment %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
class Rating < ActiveRecord::Base
  belongs_to :teacher
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :ratings
  has_many :teachers
end
class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings, dependent: :destroy
  has_many :users

  def name
    "#{firstName} #{middleName} #{lastName}"
  end

  def to_s
    name
  end
end
分级控制器
教师/show.html.erb

class RatingsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_teacher

  def new
    get_teacher
    @rating = current_user.ratings.build
  end

  def create
    get_teacher
    @rating = current_user.ratings.create(rating_params)
    if @rating.save
      redirect_to school_teacher_path(@teacher.school, @teacher)
    else
      render 'new'
    end
  end

  def destroy
    get_teacher
    @rating = @teacher.ratings.find(params[:id])
    @rating.destroy

    redirect_to school_teacher_path(@teacher.school, @teacher)
  end

  def get_teacher
    @teacher = Teacher.find(params[:teacher_id])
  end

  private

    def rating_params
      params.require(:rating).permit(:easiness, :helpfulness, :clarity, :comment,
      :teacher_id, :school_id)
    end
end
<!-- Caculate the average rating of the teacher -->
<h1>Average ratings:</h1>
<p>Clarity:
  <%= @teacher.ratings.average(:clarity) %>
</p>
<p>Easiness:
  <%= @teacher.ratings.average(:easiness) %>
</p>
<p>Helpfulness:
  <%= @teacher.ratings.average(:helpfulness) %>
</p>
<hr>

<!-- Show all the ratings -->
<h2>All the ratings:</h2>
<div>
  <%= @teacher.ratings.each do |rating| %>

    <p>Clarity:
      <%= rating.clarity %>
    </p>

    <p>Helpfulness:
      <%= rating.helpfulness %>
    </p>

    <p>Easiness:
      <%= rating.easiness %>
    </p>

    <p>Comment:
      <%= rating.comment %>
    </p>

    <%= link_to "Delete rating", [rating.teacher, rating], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-warning" %>

    <hr>
  <% end %>
</div>

<p>
  <%= link_to "Rate teacher", new_teacher_rating_path(@teacher), class: "btn btn-primary" %>
</p>

<p>
  <%= link_to "Back to school", school_path(@school), class: "btn btn-primary" %>
</p>
<h1>Teacher Rating</h1>
<%= form_for([@teacher, @rating]) do |f| %>
  <p>
    <%= f.label :clarity %>
    <%= f.text_field :clarity %>
  </p>

  <p>
    <%= f.label :easiness %>
    <%= f.text_field :easiness %>
  </p>

  <p>
    <%= f.label :helpfulness %>
    <%= f.text_field :helpfulness %>
  </p>

  <p>
    <%= f.label :comment %>
    <br>
    <%= f.text_area :comment %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
class Rating < ActiveRecord::Base
  belongs_to :teacher
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :ratings
  has_many :teachers
end
class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings, dependent: :destroy
  has_many :users

  def name
    "#{firstName} #{middleName} #{lastName}"
  end

  def to_s
    name
  end
end

平均评级:
清晰度:

轻松:

帮助:


所有评级: 清晰度:

帮助:

轻松:

评论:


评级/new.html.erb

class RatingsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_teacher

  def new
    get_teacher
    @rating = current_user.ratings.build
  end

  def create
    get_teacher
    @rating = current_user.ratings.create(rating_params)
    if @rating.save
      redirect_to school_teacher_path(@teacher.school, @teacher)
    else
      render 'new'
    end
  end

  def destroy
    get_teacher
    @rating = @teacher.ratings.find(params[:id])
    @rating.destroy

    redirect_to school_teacher_path(@teacher.school, @teacher)
  end

  def get_teacher
    @teacher = Teacher.find(params[:teacher_id])
  end

  private

    def rating_params
      params.require(:rating).permit(:easiness, :helpfulness, :clarity, :comment,
      :teacher_id, :school_id)
    end
end
<!-- Caculate the average rating of the teacher -->
<h1>Average ratings:</h1>
<p>Clarity:
  <%= @teacher.ratings.average(:clarity) %>
</p>
<p>Easiness:
  <%= @teacher.ratings.average(:easiness) %>
</p>
<p>Helpfulness:
  <%= @teacher.ratings.average(:helpfulness) %>
</p>
<hr>

<!-- Show all the ratings -->
<h2>All the ratings:</h2>
<div>
  <%= @teacher.ratings.each do |rating| %>

    <p>Clarity:
      <%= rating.clarity %>
    </p>

    <p>Helpfulness:
      <%= rating.helpfulness %>
    </p>

    <p>Easiness:
      <%= rating.easiness %>
    </p>

    <p>Comment:
      <%= rating.comment %>
    </p>

    <%= link_to "Delete rating", [rating.teacher, rating], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-warning" %>

    <hr>
  <% end %>
</div>

<p>
  <%= link_to "Rate teacher", new_teacher_rating_path(@teacher), class: "btn btn-primary" %>
</p>

<p>
  <%= link_to "Back to school", school_path(@school), class: "btn btn-primary" %>
</p>
<h1>Teacher Rating</h1>
<%= form_for([@teacher, @rating]) do |f| %>
  <p>
    <%= f.label :clarity %>
    <%= f.text_field :clarity %>
  </p>

  <p>
    <%= f.label :easiness %>
    <%= f.text_field :easiness %>
  </p>

  <p>
    <%= f.label :helpfulness %>
    <%= f.text_field :helpfulness %>
  </p>

  <p>
    <%= f.label :comment %>
    <br>
    <%= f.text_area :comment %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
class Rating < ActiveRecord::Base
  belongs_to :teacher
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :ratings
  has_many :teachers
end
class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings, dependent: :destroy
  has_many :users

  def name
    "#{firstName} #{middleName} #{lastName}"
  end

  def to_s
    name
  end
end
教师评级


评级.rb

class RatingsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_teacher

  def new
    get_teacher
    @rating = current_user.ratings.build
  end

  def create
    get_teacher
    @rating = current_user.ratings.create(rating_params)
    if @rating.save
      redirect_to school_teacher_path(@teacher.school, @teacher)
    else
      render 'new'
    end
  end

  def destroy
    get_teacher
    @rating = @teacher.ratings.find(params[:id])
    @rating.destroy

    redirect_to school_teacher_path(@teacher.school, @teacher)
  end

  def get_teacher
    @teacher = Teacher.find(params[:teacher_id])
  end

  private

    def rating_params
      params.require(:rating).permit(:easiness, :helpfulness, :clarity, :comment,
      :teacher_id, :school_id)
    end
end
<!-- Caculate the average rating of the teacher -->
<h1>Average ratings:</h1>
<p>Clarity:
  <%= @teacher.ratings.average(:clarity) %>
</p>
<p>Easiness:
  <%= @teacher.ratings.average(:easiness) %>
</p>
<p>Helpfulness:
  <%= @teacher.ratings.average(:helpfulness) %>
</p>
<hr>

<!-- Show all the ratings -->
<h2>All the ratings:</h2>
<div>
  <%= @teacher.ratings.each do |rating| %>

    <p>Clarity:
      <%= rating.clarity %>
    </p>

    <p>Helpfulness:
      <%= rating.helpfulness %>
    </p>

    <p>Easiness:
      <%= rating.easiness %>
    </p>

    <p>Comment:
      <%= rating.comment %>
    </p>

    <%= link_to "Delete rating", [rating.teacher, rating], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-warning" %>

    <hr>
  <% end %>
</div>

<p>
  <%= link_to "Rate teacher", new_teacher_rating_path(@teacher), class: "btn btn-primary" %>
</p>

<p>
  <%= link_to "Back to school", school_path(@school), class: "btn btn-primary" %>
</p>
<h1>Teacher Rating</h1>
<%= form_for([@teacher, @rating]) do |f| %>
  <p>
    <%= f.label :clarity %>
    <%= f.text_field :clarity %>
  </p>

  <p>
    <%= f.label :easiness %>
    <%= f.text_field :easiness %>
  </p>

  <p>
    <%= f.label :helpfulness %>
    <%= f.text_field :helpfulness %>
  </p>

  <p>
    <%= f.label :comment %>
    <br>
    <%= f.text_area :comment %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
class Rating < ActiveRecord::Base
  belongs_to :teacher
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :ratings
  has_many :teachers
end
class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings, dependent: :destroy
  has_many :users

  def name
    "#{firstName} #{middleName} #{lastName}"
  end

  def to_s
    name
  end
end
等级
user.rb

class RatingsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_teacher

  def new
    get_teacher
    @rating = current_user.ratings.build
  end

  def create
    get_teacher
    @rating = current_user.ratings.create(rating_params)
    if @rating.save
      redirect_to school_teacher_path(@teacher.school, @teacher)
    else
      render 'new'
    end
  end

  def destroy
    get_teacher
    @rating = @teacher.ratings.find(params[:id])
    @rating.destroy

    redirect_to school_teacher_path(@teacher.school, @teacher)
  end

  def get_teacher
    @teacher = Teacher.find(params[:teacher_id])
  end

  private

    def rating_params
      params.require(:rating).permit(:easiness, :helpfulness, :clarity, :comment,
      :teacher_id, :school_id)
    end
end
<!-- Caculate the average rating of the teacher -->
<h1>Average ratings:</h1>
<p>Clarity:
  <%= @teacher.ratings.average(:clarity) %>
</p>
<p>Easiness:
  <%= @teacher.ratings.average(:easiness) %>
</p>
<p>Helpfulness:
  <%= @teacher.ratings.average(:helpfulness) %>
</p>
<hr>

<!-- Show all the ratings -->
<h2>All the ratings:</h2>
<div>
  <%= @teacher.ratings.each do |rating| %>

    <p>Clarity:
      <%= rating.clarity %>
    </p>

    <p>Helpfulness:
      <%= rating.helpfulness %>
    </p>

    <p>Easiness:
      <%= rating.easiness %>
    </p>

    <p>Comment:
      <%= rating.comment %>
    </p>

    <%= link_to "Delete rating", [rating.teacher, rating], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-warning" %>

    <hr>
  <% end %>
</div>

<p>
  <%= link_to "Rate teacher", new_teacher_rating_path(@teacher), class: "btn btn-primary" %>
</p>

<p>
  <%= link_to "Back to school", school_path(@school), class: "btn btn-primary" %>
</p>
<h1>Teacher Rating</h1>
<%= form_for([@teacher, @rating]) do |f| %>
  <p>
    <%= f.label :clarity %>
    <%= f.text_field :clarity %>
  </p>

  <p>
    <%= f.label :easiness %>
    <%= f.text_field :easiness %>
  </p>

  <p>
    <%= f.label :helpfulness %>
    <%= f.text_field :helpfulness %>
  </p>

  <p>
    <%= f.label :comment %>
    <br>
    <%= f.text_area :comment %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
class Rating < ActiveRecord::Base
  belongs_to :teacher
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :ratings
  has_many :teachers
end
class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings, dependent: :destroy
  has_many :users

  def name
    "#{firstName} #{middleName} #{lastName}"
  end

  def to_s
    name
  end
end
class用户
教师.rb

class RatingsController < ApplicationController
  before_action :authenticate_user!
  before_action :get_teacher

  def new
    get_teacher
    @rating = current_user.ratings.build
  end

  def create
    get_teacher
    @rating = current_user.ratings.create(rating_params)
    if @rating.save
      redirect_to school_teacher_path(@teacher.school, @teacher)
    else
      render 'new'
    end
  end

  def destroy
    get_teacher
    @rating = @teacher.ratings.find(params[:id])
    @rating.destroy

    redirect_to school_teacher_path(@teacher.school, @teacher)
  end

  def get_teacher
    @teacher = Teacher.find(params[:teacher_id])
  end

  private

    def rating_params
      params.require(:rating).permit(:easiness, :helpfulness, :clarity, :comment,
      :teacher_id, :school_id)
    end
end
<!-- Caculate the average rating of the teacher -->
<h1>Average ratings:</h1>
<p>Clarity:
  <%= @teacher.ratings.average(:clarity) %>
</p>
<p>Easiness:
  <%= @teacher.ratings.average(:easiness) %>
</p>
<p>Helpfulness:
  <%= @teacher.ratings.average(:helpfulness) %>
</p>
<hr>

<!-- Show all the ratings -->
<h2>All the ratings:</h2>
<div>
  <%= @teacher.ratings.each do |rating| %>

    <p>Clarity:
      <%= rating.clarity %>
    </p>

    <p>Helpfulness:
      <%= rating.helpfulness %>
    </p>

    <p>Easiness:
      <%= rating.easiness %>
    </p>

    <p>Comment:
      <%= rating.comment %>
    </p>

    <%= link_to "Delete rating", [rating.teacher, rating], method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-warning" %>

    <hr>
  <% end %>
</div>

<p>
  <%= link_to "Rate teacher", new_teacher_rating_path(@teacher), class: "btn btn-primary" %>
</p>

<p>
  <%= link_to "Back to school", school_path(@school), class: "btn btn-primary" %>
</p>
<h1>Teacher Rating</h1>
<%= form_for([@teacher, @rating]) do |f| %>
  <p>
    <%= f.label :clarity %>
    <%= f.text_field :clarity %>
  </p>

  <p>
    <%= f.label :easiness %>
    <%= f.text_field :easiness %>
  </p>

  <p>
    <%= f.label :helpfulness %>
    <%= f.text_field :helpfulness %>
  </p>

  <p>
    <%= f.label :comment %>
    <br>
    <%= f.text_area :comment %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
class Rating < ActiveRecord::Base
  belongs_to :teacher
  belongs_to :user
end
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :ratings
  has_many :teachers
end
class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings, dependent: :destroy
  has_many :users

  def name
    "#{firstName} #{middleName} #{lastName}"
  end

  def to_s
    name
  end
end
班主任
我认为您的问题在于评级,而不是用户。您可以使用build方法,它会自动将用户id添加到评级中

@rating = current_user.ratings.build(rating_params) 
而你没有关联教师id

@rating.teacher_id = @teacher.id
而你的用户和老师之间的关系,两者都有很多它的错误语法,你应该使用has和would to。我不确定你的数据库模式,如果你在用户和教师之间的关系中有评级,你可以使用“has_many through:”,如下所示

user.rb:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :ratings
  has_many :teachers, through: :ratings
end
class用户
teacher.rb:

class Teacher < ActiveRecord::Base
  belongs_to :school
  has_many :ratings, dependent: :destroy
  has_many :users, through: :ratings

  def name
    "#{firstName} #{middleName} #{lastName}"
  end

  def to_s
    name
  end
end
班主任
不清楚你在问什么对不起,我的问题模棱两可。基本上,现在用户可以登录并给老师评分。然而,问题是评级没有显示出来。希望这能消除我问题的困惑。你有任何例外吗?@tokhi,没有,我没有任何例外。我刚刚发布了我的模型文件。你能看一下吗?很抱歉回答晚了,我正忙于我的主要工作。谢谢你的建议。不过,我相信我的ratings_controller中已经有了这一行代码。嗨,不,你已经使用了创建方法而不是内置评级。我试图编辑它来生成,但仍然不起作用。您还需要查看我的其他文件吗?我发现您没有将教师与评分关联,您应该在保存之前添加它。如果这不起作用,请发布您的模型类。我刚刚按照您的要求更新了文件。你能看一下吗?我正忙着我的主要工作,所以很抱歉回答得太晚。