Rails Simple Form - Add an error not related to attribute - ruby-on-rails

I wanna do a validation that sums the values of nested fields so I make sure it's 100%. So, in my parent model, I would do the validation, and do a self.errors.add and add the error if the validation fails. The problem is, the errors.add as long as I know expects some attribute as argument, but it's not related to any attribute on my parent model, so I would like to display that message on the top of the form, for example. Any ideas of how I can do that? thank you!
UPDATE:
This is my parent model, where I wanna validate. The form has nested fields for :arrendamento_contrato_unidades.
class ArrendamentoContrato < ApplicationRecord
has_many :arrendamento_contrato_unidades, dependent: :destroy
validate :check_total_percentual_credito
def check_total_percentual_credito
if arrendamento_contrato_unidades.sum(&:percentual_credito).to_f != 100.0
self.errors.add :base, "Tem que ser 100%"
end
end
end
My create method, which it's the one I'm testing:
def create
#arrendamento_contrato = ArrendamentoContrato.new(arrendamento_contrato_params)
respond_to do |format|
if #arrendamento_contrato.save
format.html {
flash[:notice] = flash_notice
redirect_to action: "index"
}
format.json { render json: {status: 1, redirect: arrendamento_contratos_url} }
else
format.html { render :new }
format.json { render json: {status: 0, errors: #arrendamento_contrato.errors, status: :unprocessable_entity} }
end
end
end
--- Also, I debugged my object.errors.full_messages on the form, and the error is there. It's only not being displayed!
I guess that add errors to the base it's what I'm looking for. But now, it's not showing my message, but only that I have validation errors. My form code:
= simple_form_for(#arrendamento_contrato, validate: true, html: { id:"dropzoneForm", class: "dropzone dz-clickable"}) do |f|
= f.error_notification
.form-inputs
.row
.col-md-6
= f.input :numero
.col-md-6
= f.association :usina, input_html: {class: "chosen-select"}
.hr-line-dashed
.row
.col-md-6
= f.association :esco_contrato, input_html: {class: "chosen-select"}
.col-md-6
= f.association :om_contrato, input_html: {class: "chosen-select"}
.hr-line-dashed
.row
.col-md-4
= f.input :data_inicio, as: :string, input_html: {"data-mask" => "date"}
.col-md-4
= f.input :data_fim, as: :string, input_html: {"data-mask" => "date"}
.col-md-4
= f.input :valor_mensal, as: :string, input_html: {"data-mask" => "decimal"}
.hr-line-dashed
#arrendamento_contratos_unidades
- if !#arrendamento_contrato.arrendamento_contrato_unidades || #arrendamento_contrato.arrendamento_contrato_unidades.empty?
h3 = I18n.t('activerecord.models.unidade_consumidora.other')
i
'Aguardando ESCO...
- else
.row
.col-md-6
label class='control-label'
= I18n.t('activerecord.models.unidade_consumidora.other')
.col-md-6
label class='control-label'
= I18n.t('activerecord.attributes.arrendamento_contrato_unidade.percentual_credito')
.hr-line-dashed
.blockquote
= f.simple_fields_for :arrendamento_contrato_unidades do |f|
= render 'arrendamento_contrato_unidade_fields', f: f
.hr-line-dashed

i guess it should work for you
https://apidock.com/rails/ActiveRecord/Errors/add_to_base
just
errors.add_to_base("")

I think Jeff was on the right path, but I think the method you are supposed to use is model_instance.errors[:base].
I think you also might want to take into account the over all design of this feature (not that I have the full context of your app). if you have a validation of the parent model on its children model's it means that you will be saving erroneous children model to your data base only to then notify the user. Since it seems like this would be done with nested attribute you may want to consider doing this in the controller but there is an argument to be made about having too much logic in your controller.

Updating the answer for this common question and if anybody finds it useful. You can use Errors#add(:base, msg) instead
I.E.
def validate_method
errors.add(:base, 'Error message') if some_logic
...
end

Related

Rails best_in_place Unprocessable Entity

I'm trying to edit the name field of a model with the best_in_place gem to edit items directly in line. When trying to do so I'm getting a an error 422 Unprocessable Entity.
I've done some research and found out in the response the controller is expecting not only the name attribute but also the group and some other attributes.
["Group must exist","Lumo can't be blank","Lumo is not a number"]
I have setup my controller in the correct way (I think).
materials_controller.rb
def update
#material = Material.find(params[:id])
if params[:commit] == "Save" then
success = #material.update(material_params)
params[:create_pure_composition] = false
else
#material = Material.new(material_params)
#material.user_id = current_user.id
success = #material.save
end
respond_to do |format|
if success
plot1 = prepare_e_plot(#material)
plot2 = prepare_st_plot(#material)
format.html { redirect_to #material }
format.json { render json: { plot1: plot1, plot2: plot2, status: 200 } }
else
format.html { render 'edit' }
format.json { respond_with_bip(#material) }
end
end
end
Is there a way with best_in_place to send these value's when updating the name attribute? I've tried with the params attribute from best_in_place.
<%= best_in_place #material, :name, as: :input, params: { group_id: #material.group_id } %>
This wasn't sending any extra params with the update.
Here's is what the Material model looks at.
material.rb
class Material < ActiveRecord::Base
belongs_to :user
belongs_to :group
validates :name, presence: true, uniqueness: {scope: :user_id}
validates :lumo, presence: true, numericality: true
end
Does anybody know why it's asking for other attributes and why best_in_place is not sending those along?
I figured out what the problem was and how to fix it. We use an option to Save or Save As when editing materials. In the controller we therefore check for the params[:commit].
By editing the url I was able to send in the params[:commit] with the update with best_in_place. Here is how the best_in_place code ended up like:
<%= best_in_place #material, :name, as: :input, url: material_path(#material, commit: "Save") %>

Ruby on Rails error when validates_uniqueness_of using collection_select

First, sorry for my bad English. I'm still learning.
I have 3 tables in my DB:
Problem
has_many :registers
has_many :solutions, through : :registers
Solution
has_many :problems
has_many :problems, through : :registers
Register
belongs_to: problem
belongs_to :solution
The system is working well. I am able to insert new data in all of the 3 tables.
In the views for the table/model Register, to select problems and solutions, I make use of collection_select, like this:
= form_for #register do |f|
.field
= f.label :problem_id
= collection_select( :register, :problem_id, #problems, :id, :name, {}, { :multiple => false })
.field
= f.label :solution_id
= collection_select( :register, :solution_id, #courses, :id, :name, {}, { :multiple => false })
.field
= f.label :entry_at
= f.datetime_select :entry_at
.actions = f.submit
The problem only appears when I try to add this validation to Register:
validates_uniqueness_of :student_id , scope: :course_id
Then I get:
> undefined method `map' for nil:NilClass
> = collection_select( :register, :problem_id, #problems, :id, :name, {}, { :multiple => false })
And I dont know why.
So, I tried to do the validation by the controller:
def create
#register = Register.new(register_params)
problem_id = #register.problem_id
solution_id = #register.solution_id
if Register.exists?(['problem_id LIKE ? AND solution_id LIKE ?', problem_id, solution_id ])
render 'new'
else
#register.save
respond_with(#register)
end
end
But the error remains.
I believe that the cause is the collection_select, but I don't know how to solve it.
Saying one more time, I am able to persist date in all the 3 DB tables. But when I try to avoid duplication, the error appears.
This is how I solve this problem:
def create
#register = register.new(register_params)
#if #register.save
# respond_with(#register)
#else
# #register = register.all
# render :new
#end
problem_id = #register.problem_id
solution_id = #register.solution_id
if register.exists?(['problem_id LIKE ? AND solution_id LIKE ?', problem_id, solution_id ])
#register = register.new
#solutions = Solution.all
#problems = Problem.all
flash[:error] = "Problem alread in the register for this solution"
render 'new'
else
#register.save
respond_with(#register)
end
end

Rails using a layout for new and edit form

In my application I have a form for a new action that is the same, four or five lines apart, that my edit form.
The problem that i encounter is that my new.html.haml gives me an error explainning that it doesn't know what "f" is.
The f is the one in "form_for #object do |f|".
Obviously in my new the form_for's object is not the same that the edit one.
I think that it should be a common behaviour but i can't seem to find anything that suits my needs in the rails documentation.
Here a quick exemple of code
#new.html.haml
%div{ id: "StaticForms" }
= f.label :project_id
= f.collection_select :project_id, #projects, :id, :name,
{ selected: #edit.task.project_id },
{ class: "form-control", onchange: 'get_projects(this.options[this.selectedIndex].value);' }
- index = 1
- #project_tree.each do |task|
= render partial: "edit", locals: {task: task, index: index} // render here was okay before "layouting"
- index += 1
#form_layout.html.haml%html
%div{ :class => "col-md-12" }
%h2 Reporting
%div{ :id => "Form"}
= form_for #activity do |f|
= f.label :consultant_id
= f.collection_select :consultant_id, #consultants, :id, :name,
{ selected: current_user.consultant.id },
{ class: "form-control" }
= yield
The JS tag "onchange: ..." you can see on the new.html.haml file is supposed to be triggered by JS that i have in the layout file fyi.
The error i have is : "undefined local variable or method `f' for #<#:0x007feb84e6b848>"
Can someone help with a link or an explanation ? :)
Thank you.
Best regards.
Edit : Added some code, did not think it was relevent for that type of issue :>
#app/views/activities/_form.haml
= form_for activity do |f|
= f.label :consultant_id
= f.collection_select :consultant_id, #consultants, :id, :name,
{ selected: current_user.consultant.id },
{ class: "form-control" }
...
#app/views/activities/new.haml
= render "form", activity: #activity
#app/views/activities/edit.haml
= render "form", activity: #activity
So i finnaly get around my problem and i was able to fix it.
new and edit views :
= render layout: 'form_activity', locals: {activity: #object} do |f| # here #objet depends on the controller method i.e. #activity for new, #edit for edit
= f.label ...
...
#relativly the same code in edit and new
_from_activity.html.haml (in the same folder than the views above) :
= form_for activity do |f|
... # some form stuff
= yield f # here i get back to new or edit code
... # some stuff - rest of the form.
This works perfectly !
Layout stuff -- before the yield.
--
Views stuff - thanks to the line : 1) render layout: ... do | f | in the view and 2) yield f in the layout
--
Layout stuff - after the yield.
Thx all of you for your answers.
By the way, i'm aware that the first question wasn't asking the result i just post. If so my apologies :/
Best regards.

NoMethodError undefined method `id' for nil:NilClass:

I know this kind of question is already answered multiple times but i seriously unable to figure it out what is causing a problem here, I am having trouble solving this problem. I keep getting the same error when i'm trying to create new registration ( http://localhost:3000/registrations/new?course_id=1 ) :
NoMethodError at /registrations
undefined method `id' for nil:NilClass
Here is my RegistrationsController:
class RegistrationsController < ApplicationController
before_action :set_registration, only: [:show, :edit, :update, :destroy]
def index
#registrations = Registration.all
end
def show
end
def new
#registration = Registration.new
#course = Course.new
#course = Course.find_by id: params["course_id"]
end
def create
#registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"], card_token: stripe_params["stripeToken"])
raise "Please Check Registration Errors" unless #registration.valid?
#registration.process_payment
#registration.save
redirect_to #registration, notice: 'Registration was successfully created.'
rescue Exception => e
flash[:error] = e.message
render :new
end
protect_from_forgery except: :webhook
def webhook
event = Stripe::Event.retrieve(params["id"])
case event.type
when "invoice.payment_succeeded" #renew subscription
Registration.find_by_customer_id(event.data.object.customer).renew
end
render status: :ok, json: "success"
end
private
def stripe_params
params.permit :stripeEmail, :stripeToken
end
def set_registration
#registration = Registration.find(params[:id])
end
def registration_params
params.require(:registration).permit(:course_id, :full_name, :company, :telephone, :email, :card_token)
end
end
My Registration Model:
class Registration < ActiveRecord::Base
belongs_to :course
def process_payment
customer_data = {email: email, card: card_token}.merge((course.plan.blank?)? {}: {plan: course.plan})
customer = Stripe::Customer.create customer_data
Stripe::Charge.create customer: customer.id,
amount: course.price * 100,
description: course.name,
currency: 'usd'
#Annotate Customer Id when Registration is Created
cusotmer_id = customer.id
end
def renew
update_attibute :end_date, Date.today + 1.month
end
end
Registration New.html.haml File :
%section#course-content
%section#ruby
%section.detailed-syllabus
.wrapper-inside
= form_for #registration, html: { class: "basic-grey" } do |f|
- if #registration.errors.any?
#error_explanation
%h2
= pluralize(#registration.errors.count, "error")
prohibited this registration from being saved:
%ul
- #registration.errors.full_messages.each do |message|
%li= message
.field
= f.hidden_field :course_id, value: #course.id
.field
= f.label :full_name
= f.text_field :full_name
.field
= f.label :company
= f.text_field :company
.field
= f.label :email
= f.text_field :email
.field
= f.label :telephone
= f.text_field :telephone
//‘Stripe.js’ will recognize the card data because we have marked the inputs with ‘data-stripe’ attribute as: number, cvv, exp-month and exp-year.
= javascript_include_tag "https://js.stripe.com/v2/"
:javascript
Stripe.setPublishableKey('#{Rails.application.secrets.stripe_publishable_key}');
= label_tag "Card Number", nil, required: true
.control-group
.controls
= text_field_tag :card_number, nil, class: "input-block-level", "data-stripe" => "number"
= label_tag "Card Verification", nil, required: true
.control-group
.controls
= text_field_tag :card_verification, nil, class: "input-block-level", "data-stripe" => "cvv"
= label_tag "Card Expires", nil, required: true
= select_tag :exp_month, options_for_select(Date::MONTHNAMES.compact.each_with_index.map { |name,i| ["#{i+1} - #{name}", i+1] }), include_blank: false, "data-stripe" => "exp-month", class: "span2"
= select_tag :exp_year, options_for_select((Date.today.year..(Date.today.year+10)).to_a), include_blank: false, "data-stripe" => "exp-year", class: "span1"
.actions
= f.submit "Registration Payment", class: "btn", style: "color: white;background: rgb(242, 118, 73);"
Does anyone know how to assist me in this? Greatly appreciate all the help.
Additional Can anyone please guide me through how to pass id between 2 models like this guy did between 2 models as he's creating a scaffold for one model but passing ID lets him create values for another model too without creating actions for another controller https://github.com/gotealeaf/stripe-basics.git
Edited:
GitHub Repository For This Code
https://github.com/ChiragArya/Stripe_CheckOut_Demo
From your comments, it appears the error is caused by :
#course.id being nil
The way to fix this is to ensure #course is defined properly. You need to do the following:
def new
#registration = Registration.new
#course = Course.find_by id: params["course_id"]
end
The other issue you have here is that your routes should be able to handle courses without having to append them with ?course_id=1:
#config/routes.rb
resources :registrations do
get :course_id, to: "registrations#new" #-> yoururl.com/registrations/:course_id
end
This will still give you the course_id param in the new action; just makes it more Rails.
--
Controller
You also need some structure in your code (you're aiming for fat model, thin controller). It looks like you're coming to Rails as a Ruby dev; you need to appreciate that Rails handles most of the exceptions etc for you.
Specifically, you need to look at how to remove code out of your actions:
def create
#registration = Registration.new registration_params
#registration.process_payment
if #registration.save
redirect_to #registration, notice: 'Registration was successfully created.'
else
# handle error here
end
end
private
def registration_params
params.require(:registration).permit(:course_id, :full_name, :company, :telephone, :email, :card_token).merge(email: stripe_params["stripeEmail"], card_token: stripe_params["stripeToken"])
end
-
`id' for nil:NilClass
Finally, you have to remember this error basically means the variable you're trying to invoke an action for is nil.
Ruby populates nil variables with a NilClass object, thus it's difficult to determine what the error actually is. All it means is that the variable you're trying to call a method on doesn't have the aforementioned method, as Ruby has populated it with the NilClass object.
Try changing Registration#new action to
def new
#course = Course.find(params[:course_id])
#registration = #course.registrations.new
end
add this in your def create
def create
#course = Course.find_by id: params["registration"]["course_id"]
#registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"], card_token: stripe_params["stripeToken"])
raise "Please Check Registration Errors" unless #registration.valid?
#registration.process_payment
#registration.save
redirect_to #registration, notice: 'Registration was successfully created.'
rescue Exception => e
flash[:error] = e.message
#course = Course.find_by id: params["registration"]["course_id"]
render :new
end

Rails form params changing in controller

I have a form:
<%= form_for(:report_main, :url => {:action => 'exporttoxiccreate'}) do |f| %>
<%= collection_select(:waste, :code, Waste.find_all_by_istoxic(false), :id, :code, :include_blank => '') %>
<%= f.check_box(:q_pripadnost) %>
<%= f.text_field(:amount) %>
<% end %>
and this code in controller:
def exporttoxiccreate
#report = ReportMain.new
#reportexport = ReportExport.new
#reportparam = params[:report_main]
#report.waste_id = #reportparam.waste.code
#report.amount = #reportparam.amount
if #report.save
#reportexport.report_main_id = #report.id
else
redirect_to(:action => 'exporttoxicnew')
end
#reportexport.q_pripadnost = #reportparam.q_pripadnost
if #reportexport.save
redirect_to(:action => 'show', :id => #reportexport.id)
else
redirect_to(:action => 'exporttoxicnew')
end
end
I want to save in two tables, in two objects data from this form, and I need to separate params to manipulate with. I tried with this:
#reportexport.q_pripadnost = #reportparam.q_pripadnost
I want to set q_pripadnost field in #reportexport with some value from param.
Where I make mistake?
When you get params from a form in Rails, it comes in the form of a hash. For example:
params[:report_main][:waste]
params[:report_main][:amount]
So when you call #reportparam = params[:report_main], you are setting #reportparam to a hash, but then you are trying to use it later like an object. For example, instead of #reportparam.q_pripadnost, use #reportparam[:q_pripadnost].
You can take a closer look at your variable by temporarily changing your action to show a text version of the variable, for example:
def exporttoxiccreate
#reportparam = params[:report_main]
render :text => #reportparam.to_yaml
end

Resources