How to save data from another model. To archive information

0

I have two models: employees and reports. Each month some information about employees changes while others remain the same. I want to create reports with month-to-month information. So if you want to know how much an employee received in a given month, you only have to access that employee's report that month. I'm trying to create this functionality using accept next attributes. And the correct one? I'm having trouble creating the report form on the Employee show page. You look like it, huh? Thank you.

    
asked by anonymous 19.05.2017 / 20:50

1 answer

0

First: "create the form" on the "employee show" page I do not know if it is necessary, by convention forms are in the "new.html.erb" view, but if that is what you want, a form on the same page in which displays the user information, the form is created in the same way.

In addition, you need a has_many relationship between the employee and the report (an employee has multiple reports).

#app/controllers/offices_controller.rb
class OfficesController < ApplicationController  
  def show
    @employee = Employee.find(params[:id]) # Aqui você está pegando o funcionário
    @report = Report.new # Aqui você cria uma nova instância de um objeto Report
  end
end

# app/views/offices/show.html.erb
<%= @employee.name %>
<%= @employee.age %>
<%= @employee.salary %>

<%= form_for(@report) do |f| %>
  <%= f.hidden_field :employee_id, value: @employee.id %>
  <%= f.text_field :salary, value: @employee.salary %>
  <%= f.submit %>
<% end %>

#app/controllers/reports_controller.rb
class ReportsController < ApplicationController
  def create
    Report.create(report_params)
  end

  private

  # Aqui é a validação do Strong Parameters, para uma validação que se encaixe
  # com suas necessidades consulte a documentação:
  # https://github.com/rails/strong_parameters
  def report_params
    params.fetch(:report).permit!
  end
end
    
20.05.2017 / 16:07