Rails 4 Polymorphic associations and concerns

前端 未结 1 1193
离开以前
离开以前 2020-12-20 17:00

I\'m trying to add an Evaluation model to my Rails 4 app.

I have made a model called evaluation.rb. It has:

cl         


        
1条回答
  •  天涯浪人
    2020-12-20 17:43

    How do I setup the show page to show a user's evaluations received?

    Your model concerns should help you with that. In your UsersController#show action, simply adding the following should do the trick:

    @received_evaluations = @user.received_evaluations
    

    Then you can use it in your show template:

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

    Or use collection rendering.

    note: the Evaluation.find(...) that's currently in your view should be put in the controller action, it's not good practice to leave that in the view.

    How do I adapt the form so that it specifies a user id as the person who should receive the evaluation?

    If you have identified the user that will serve as evaluatable you could set it in your controller action or in your view form in case 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 simpler statement should do the same:

    @evaluation.evaluatable = user_to_evaluate
    

    Similarly, you should be able to set the evaluator the same way:

    @evaluation.evaluator = user_that_evaluates
    

    In the view:

    <% @users_to_evaluate.each do |user| %>
      <%= simple_form_for(Evaluation.new) do |f| %>
        <%= f.error_notification %>
    
        
    <%= 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 %>
    <% end %> <% end %>

    0 讨论(0)
提交回复
热议问题