Search code examples
ruby-on-railsrubyruby-on-rails-5

How can create records using input date and save the param using ROR?


I'm trying to generate records according when somebody choose a date it will save more records according an input value.

Controller

##./app/controller/user_job_controller.rb
def new
  @user_job = UserJob.new
end

def user_job_params
  params.require(:user_job).permit!
end

Model

#user_job.rb
after_save :save_default_values, :if => :new_record?

private
  def save_default_values

    if params[:dateadd].to_i > 0
      params[:dateadd].to_i.times do |i|
         self.name = params[:name]      
         self.dateuser = params[:dateuser] + i+ 1.day
      end
    else
      self.name = params[:name]
      self.dateuser = params[:dateuser]          
    end
    
  end    

View

<%= form_with(model: user_job) do |form| %>
   Name:<%= form.text_field :name %>
   Date: <%= text_field_tag "dateuser", params[:dateuser].to_i %>
   Date added: <%= text_field_tag "dateadd", params[:dateadd].to_i %>
   <%= form.submit %>
<% end %>

Example with data:

 #form values
 Name: test
 Date: 23-09-2021
 days added: 2

Result Wanted:

 #it will do on console or ruby on rails logs
  insert into user_jobs (id:1, name:test, dateuser: 23-09-2021),
  insert into user_jobs (id:2, name:test, dateuser: 24-09-2021),
  insert into user_jobs (id:3, name:test, dateuser: 25-09-2021),

The log is showing that is not

Processing by UserJobsController#create as HTML
 Parameters: {"authenticity_token"=>"[FILTERED]", "user_job"=>{"name"=>"test"}, "dateuser"=>"2021-09-24", "dateadd"=>"2", "commit"=>"Create User job"}
 Unpermitted parameter: :name

Result log:

  insert into user_jobs(created_at: 23-09-2021)

Can you please help me to save the data correctly?


Solution

  • There are some problems in the codes.

    1. params are only accessible in controller, you wanted to read it in your model

    There are many ways to pass params value to model, for example, you can add attr_accessor :dateadd to the model. But I think a clearer way is to extract the behavior in :save_default_values into a new public method and call it in the controller.

    2. params[:name] is an attribute in the model, you should use strong parameters to assign the attributes

    3. it's apparently that dateuser is a column of user_jobs, isn't it?

    We can use date_field to pick the date and also add this field into strong parameter.

    4. After a record saved, that record is not new record any more since it has id value.

    Try to use after_create or after_save on: :create. However, I don't use this callback in the code below, but it's just personal choice :)

    # user_job.rb
    class UserJob < ApplicationModel
      # add new public method
      def add_additional_jobs!(days)
        (1..days).each do |n|
          UserJob.create!(name: name, dateuser: dateuser + n.days)
        end
      end
    end
    
    # user_jobs_controller.rb
    class UserJobsController < ApplicationController
      def new
        @user_job = UserJob.new
      end
    
      def create
        UserJob.transaction do
          @user_job = UserJob.create!(user_job_params)
          if params[:dateadd].to_i > 0
            @user_job.add_additional_jobs!(params[:dateadd].to_i)
          end
        end
      end
      
      private
    
      def user_job_params
        params.require(:user_job).permit(:name, :dateuser)
      end
    end
    

    Then in the view

    <%= form_with(model: user_job) do |form| %>
      Name:<%= form.text_field :name %>
      Date: <%= form.date_field :dateuser %>
      Date added: <%= text_field_tag "dateadd", params[:dateadd].to_i %>
      <%= form.submit %>
    <% end %>