Rails 4 Polymorphic Associations and Problems

I am trying to add an Evaluation model to my Rails 4 application.

I created a model called evaluation.rb . He has:

 class Evaluation < ActiveRecord::Base belongs_to :evaluator, :polymorphic => true belongs_to :evaluatable, :polymorphic => true 

I was also worried about evaluator and evaluatable like:

 module Evaluator extend ActiveSupport::Concern included do has_many :given_evaluations, as: :evaluator, dependent: :destroy, class_name: 'Evaluation' end end module Evaluatable extend ActiveSupport::Concern included do has_many :received_evaluations, as: :evaluatable, dependent: :destroy, class_name: 'Evaluation' end end 

I have included every problem in my user model:

 class User < ActiveRecord::Base include Evaluator include Evaluatable 

On my show page, I want to show specific user ratings (received from other users who are evaluators).

In my show, I have:

 <% Evaluation.find(params[:id]).evaluations.order('created_at DESC').each do |eval| %> <div id="portfolioFiltering" class="masonry-wrapper row"> <%= eval.remark %> <%= eval.personal_score %> <small><%= eval.created_at %></small> 

In my assessment form, I’m not sure how to assign a rating recipient. I made the main form, but I don’t know how to associate it with the user who needs to get the grade.

 <%= simple_form_for(@evaluation) do |f| %> <%= f.error_notification %> <div class="form-inputs"> <%= f.input :score, collection: 1..10, autofocus: true, :label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %> <%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10} %> 

My grades table has:

  t.integer "user_id" t.integer "evaluatable_id" t.string "evaluatable_type" t.integer "overall_score" t.integer "project_score" t.integer "personal_score" t.text "remark" t.boolean "work_again?" t.boolean "continue_project?" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "evaluations", ["evaluatable_type", "evaluatable_id"], name: "index_evaluations_on_evaluatable_type_and_evaluatable_id", unique: true, using: :btree 

QUESTIONS

How to customize the display page to display the received user ratings?

How do I configure the form to indicate the user ID as the person who should receive the rating?

+8
polymorphism ruby-on-rails polymorphic-associations
source share
1 answer

How to customize the display page to display the received user ratings?

Your problems with the model should help you with this. In your UsersController#show action, just adding the following should do the trick:

 @received_evaluations = @user.received_evaluations 

Then you can use it in your template:

 <% @received_evaluations.each do |evaluation| %> // render some view stuff <% end %> 

Or use collection rendering .

Note: Evaluation.find(...) , which currently needs to be put into the action of the controller in your view, it is not recommended to leave this in the view.

How do I configure the form to indicate the user ID as the person who should receive the rating?

If you have identified a user to be evaluatable , you can set it in the action of your controller or in the view form if you have a list of users to evaluate on your page.

In the controller:

 @evaluation.evaluatable_id = user_to_evaluate.id @evaluation.evaluatable_type = user_to_evaluate.class.to_s 

Or this simple statement should do the same:

 @evaluation.evaluatable = user_to_evaluate 

Likewise, you should be able to set an evaluator in the same way:

 @evaluation.evaluator = user_that_evaluates 

In view:

 <% @users_to_evaluate.each do |user| %> <%= simple_form_for(Evaluation.new) do |f| %> <%= f.error_notification %> <div class="form-inputs"> <%= f.input :score, collection: 1..10, autofocus: true, :label => "How do you rate this experience (1 being did not meet expectations - 10 being met all expectations) ?" %> <%= f.input :remark, as: :text, :label => "Evaluate your project experience", :input_html => {:rows => 10} %> <%= f.hidden_field :evaluator_id, :value => current_user.id %> <%= f.hidden_field :evaluator_type, :value => current_user.class.to_s %> <%= f.hidden_field :evaluatable_id, :value => user.id %> <%= f.hidden_field :evaluatable_type, :value => user.class.to_s %> </div> <% end %> <% end %> 
+2
source share

All Articles