How do you change the default error messages given by Grape on Validation Errors?
For Example -
params do
requires :email, allow_blank: false
end
If I don't pass the :email in the API call, grape will give error message as ['email is missing', 'email is empty'] but I want to override the message as ['Oops! Email is required.']
So, how can I override the default error messages for Grape default Validation Rules.
format :json
subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
error!({ messages: e.full_messages.map { |msg| "Oops!" + msg }}, 400)
end
Update:
If you want to customize the complete message you can manually edit the grape locale file and override it in your application.
Grape locale en.yml
It seems like the original answer is plain wrong:
https://github.com/ruby-grape/grape#custom-validation-messages
Which should work something like this for OP's example:
params do
requires :email, allow_blank: { false, message: '' }, message 'Oops! Email is required.'
end
The tricky part is that OP is violating 2 validations but wants to have one message. Maybe the workaround above will work, though.
Related
I am using Twilio SMS service for one of my projects it is in Ruby. In this, I want to send SMS with dynamic values like for example Hi, {{name}} in this name attributes will be dynamic. I have gone through the documentation but I am not able to figure it out how can I achieve this. I am using the ruby Twilio client library. However, I can send normal plan text SMS with it but not SMS having dynamic contents.
My current code is
#connection = Twilio::REST::Client.new(#account_sid, #auth_token)
#connection.messages.create({
:from => options.fetch(:from),
:to => options.fetch(:to),
:body => options.fetch(:body)
})
def welcome_message(name)
"Hi, #{name}! welcome to our website."
end
And when you're passing the options:
options = {
from: someone,
to: someone,
body: welcome_message(some_name),
}
Then the same snippet you shared:
#connection = Twilio::REST::Client.new(#account_sid, #auth_token)
#connection.messages.create({
:from => options.fetch(:from),
:to => options.fetch(:to),
:body => options.fetch(:body)
})
I'm in trouble with RSpec and fixture_file_upload on post request.
Here's the problem: I'm trying to send an image by upload to create a category with image, but when the image arrives, it changes it's class type.
I'm waiting to get in params[:category][:image] an ActionDispatch::Http::UploadedFile but what I receive is ActionController::Parameters.
My request example:
context 'when creates a new main category with valid params' do
let(:category) do
{ category: { name: 'Bolos E bolos',
description: 'São bolos sim',
locale: 'pt-BR',
image: fixture_file_upload('images/test-image.png', 'image/png') } }
end
post '/v4/categories' do
it 'upload image' do
expect { do_request(category) }.to change { ActiveStorage::Blob.count }.from(0).to(1)
end
end
end
what I got:
Failure/Error: expect { do_request(category) }.to change { ActiveStorage::Blob.count }.by(1)
expected `ActiveStorage::Blob.count` to have changed by 1, but was changed by 0
How do I send the image as upload and get it on the controller as ActionDispatch::Http::UploadedFile instead ActionController::Parameters
I could not get your controller spec working, but I managed to get an equivalent request spec working. Having spent 45+ minutes getting no where, I think this is the best I can do. This seems to work. (Just make sure you have an avatar.jpg in the public folder of your rails app.)
## spec/requests/user_spec.rb
require 'rails_helper'
RSpec.describe "Users", type: :request do
describe "it attaches uploaded files" do
it 'attaches the uploaded file' do
file = fixture_file_upload(Rails.root.join('public', 'avatar.jpg'), 'image/jpg')
expect {
post api_users_path, params: { user: {username: "Ben", avatar: file } }
}.to change(ActiveStorage::Attachment, :count).by(1)
end
end
end
Here I use the attribute :new_record to set the condition for validation.
attr_accessor :new_record
validates :email, presence: true, email: true, unless: :new_record?
def new_record?
#new_record || true
end
if the new_record? == true, the email validation will be skipped and new user is valid.
I wrote this test:
it { expect(subject).to validate_presence_of(:email) }
and it returns error:
Failure/Error: it { expect(subject).to validate_presence_of(:email) }
User did not properly validate that :email cannot be empty/falsy.
After setting :email to ‹nil›, the matcher expected the User to be
invalid, but it was valid instead.
Note that this test run perfectly if there is no condition added to the validation. Also, the code work well in production.
I also have tried
before(:each) do
subject.new_record = true
end
or
`before { allow(subject).to receive(:new_record?).and_return(true) }`
Can any one help?
I was trying to pass the exercise in listing 10.56 and test if the admin attribute is forbidden.
I added admin parameter in
app/controllers/users_controller.rb
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation,
:admin)
end
also, filled necessary parts in
test/controllers/users_controller_test.rb
test "should not allow the admin attribute to be edited via the web" do
log_in_as(#other_user)
assert_not #other_user.admin?
patch :update, id: #other_user, user: { password: "",
password_confirmation: "",
admin: true }
assert_not #other_user.reload.admin?
end
Still, I am getting unknown error after test:
ERROR["test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web", UsersControllerTest, 3.2600422599352896]
test_should_not_allow_the_admin_attribute_to_be_edited_via_the_web#UsersControllerTest (3.26s)
URI::InvalidURIError: URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80update
test/controllers/users_controller_test.rb:37:in `block in <class:UsersControllerTest>'
Anyone here was dealing with the same problem?
This error is intended. As the exercise says you added :admin to permitted params before.
Your test now will send the patch request and set admin: true for #other_user (which is possible here, since you added :admin to permitted params). After that you use "assert_not" which will raise the error, since the test expects that
#other_user.reload.admin?
will be false but in your case its true. Deleting :admin from permitted params in app/controllers/users_controller.rb will change your test to green.
The following should give you a non-erroring test that will fail when you allow admin to be altered as you have.
test "should not allow the admin attribute to be edited via the web" do
log_in_as(#other_user)
assert_not #other_user.admin?
patch user(#other_user), params: {
user: { password: "",
password_confirmation: "",
admin: true }
assert_not #other_user.reload.admin?
end
I want to display a name in 'es', 'en', 'de' and many others, hence, I have used the string :name and it is stored in en.yml, es.yml and others files. In order to manipulate them I have done it like this (see below):
<%= form.label :name %>
On en.yml
#contact form
helpers:
label:
message:
name: Name123
email: Email
subject: Subject
body: Body
This is not working. so please help.! As I am not aware of how to actually implement it.
Thanks
bugs idetified: Indentation issues in .yml files.
descriptive solutions: use correct indentation.
corrected source code: see below
#contact form
helpers:
label:
message: #name of the variable
name: Name123
email: Email
subject: Subject
body: Body