How to save data from another model. To archive information

Asked

Viewed 230 times

0

I have two models: employees and reports. Every month some information about employees changes while others remain the same. I want to create reports with the information month by month. So if you want to know how much an employee received in a given month just access the report of that employee that month. I am trying to create this functionality using Accept next Attributes. Is it correct? I’m having trouble creating the report form on the employee show page. Is there any way right? Thanks.

1 answer

0

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

In addition, you need a has_many relationship between employee and 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
  • Cool guy, actually I was complicating needlessly. Every month only data pertaining to holerite change. So I created a new model holerite associate with employees. So in place of reports I did @funcionario.relatorios.each do |h| h.data enes and so I have the information relating to when each received in a given month. Now I’m tangled up in some bullshit here. How do I link to each holerite listed in each above? link_to h.data, function_holerite_path does not work.

  • <= link_to "report", report_path(report) %>. Just a detail, when someone helps you in a response, of a upvote. So we’ll follow the Stackoverflow conventions.

  • Almost that, but on the routes report is within employee . Awe does not work. Any idea?

  • Sorry, I am new here but I will do what you asked. I appreciate the help. The code is not working yet. I will try to explain it better. I have two models Employees and Holerites. So I did has_many and belogns_to and this part is working. In the show page of Funcionario I made a loop with each: <% @funcionario.holerites.each do |holerite| %> <p><%= link_to holerite.data, holerite_path(holerite) %></p> <% end %>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.