at first I'm newbie in rails , I have rails application witch contains profile for each user , in profile based on each user gender front send a params with . for example "Mr . " but in my controller i could not accecpt so it return error
"#<ArgumentError: 'mr.' is not a valid title>",
i want to edit my controller to get params and change it to without dot ,I think maybe it could possible to use gsub but when i try to edit it returns error
"exception": "#<NoMethodError: undefined method `gsub' for #<Enumerator: "mr.":gsub(" ")>>",
,is it possible to refactor in params permit. it's my controller
def update
if (profile_update_params.present?)
profile_update_result = ::Api::V1::Profile::Update.call(profile_params: profile_update_params, profile: #current_user.profile, request: request)
return render json: Helpers::ErrorsHandler.view_parse(profile_update_result), status: :bad_request if profile_update_result.errors
end
if (user_update_params.present?)
user_update_result = ::Api::V1::User::Update.call(user_params: user_update_params, user: #current_user)
return render json: Helpers::ErrorsHandler.view_parse(user_update_result), status: :bad_request if user_update_result.errors
end
result = sheriff
render result.view
end
and this is my params permit
def profile_update_params
params.permit(:phone, :title, :email, :gender, :first_name, :last_name, :description, :job_title, :time_zone)
end
I found using gusb is currect but i'm using in wrong place ,I do not need to change hole params to string so I took my field out of params and change my params permit as bellow and every things is going fine
def profile_update_params
params.permit(:phone, :email, :gender, :first_name, :last_name, :description, :job_title, :time_zone).merge({title: params[:title].to_s.gsub(".", "")})
end
Related
Im trying to understand how parameters require work with rails controllers.
I have a user controller to create a user. My json looks like so:
{
"name":"user onex",
"username":"useronea",
"email":"userone#gmail.comb",
"password":"123456",
"password_confirmation":"123456"
}
And my controller code looks like this
# POST /users
def create
#user = User.new(user_params)
if #user.save
render json: #user, status: :created
else
render json: { errors: #user.errors.full_messages },
status: :unprocessable_entity
end
end
def user_params
params.permit(
:name, :username, :email, :password, :password_confirmation
)
end
rails lets me create this user but I do see an error in red stating
Unpermitted parameter: user
If I modify the user_params as follows (Like I've seen on other examples)
params.require(:user),permit(
:name, :username, :email, :password, :password_confirmation
)
The rails application fails to see my email and password. I get a 403 error
I don't understand why I'm getting the unpermitted parameter 'user' when Im not sending that parameter in the first case unless its just implied. If its implied then why doesn't the second case work.
Yes, there is.
Also, if you've turned on config.wrap_parameters in your initializer
or called wrap_parameters in your controller, you can safely omit the
root element in the JSON parameter. In this case, the parameters will
be cloned and wrapped with a key chosen based on your controller's
name.
https://guides.rubyonrails.org/action_controller_overview.html#json-parameters
ActionController::ParamsWrapper will turn:
{ "name": "acme", "address": "123 Carrot Street" }
Into:
{ name: "acme", address: "123 Carrot Street", company: { name: "acme", address: "123 Carrot Street" } }
The idea is that you can use the same params whitelisting method for both flat JSON parameters and nested FormData parameters when you have controllers that serve both JSON and HTML. This is one of those ideas that hasn't aged well.
You can disable it with for your entire application with config.wrap_parameters = false or per controller with the wrap_parameters false class method.
I am trying to use multiple permits in a single method similar to the following (psuedocode)
def index
model.create(
params.permit(:b, :c)
)
params.permit(:a)
end
This is my actual code
def create
params.permit(:create_special_categories)
balance_sheet = ::BalanceSheet.create!(
balance_sheet_params.merge(date: Time.zone.now.to_date, entity: #entity)
)
balance_sheet.create_special_categories if params[:create_special_categories]
render json: balance_sheet, serializer: ::Api::V3::BalanceSheetSerializer
end
def balance_sheet_params
params.permit(
:id,
:entity,
:entity_id,
:date,
:name
)
end
However, I get the following error...
ActionController::UnpermittedParameters:
found unpermitted parameter: :create_special_categories
UPDATE
my solution was to avoid strong parameters all together.
def create
balance_sheet = ::BalanceSheet.new(
date: Time.zone.now.to_date, entity: #entity
)
balance_sheet.name = params[:name]
balance_sheet.save!
balance_sheet.create_special_categories if params[:create_special_categories]
render json: balance_sheet, serializer: ::Api::V3::BalanceSheetSerializer
end
This line doesn't have any effect, params.permit are not chained or added to a previous permit, you must use the result, that is why it's almost always used in a separate method.
params.permit(:create_special_categories)
What you must do is use what that returns for your following statements
permitted_params = params.permit(:create_special_categories)
Model.create(permitted_params)
...however you really should outsource this to a special method like you already have. You will have to tweak this to your use-case obviously.
def balance_sheet_params
if params[:create_special_categories]
params.permit(:id,
:entity,
:entity_id,
:date,
:name,
:create_special_categories)
else
params.permit(
:id,
:entity,
:entity_id,
:date,
:name)
end
end
I want to let the admin user see a particular message after or before or during he creats a new record.
I need either an alert box after he created the new record, or to change the current confirmation message just for the user model, or to add a small text in the form specifying this.
I can't seem to find any of the ways.
Thank you
You need to use "notice:". In my case, after saving new "admin_user", I am checking for "resource". If it is "valid", then "redirect_to" with a "message". ... This always works for me.
ActiveAdmin.register AdminUser do
....
....
permit_params :first_name, :last_name, :email, :password
def create
#admin_user = AdminUser.new( admin_user_params )
#admin_user.save
if resource.valid?
redirect_to collection_url, notice: 'Creation Success'
else
flash[:alert] = 'Creation Failed'
render :new
end
end
private
def admin_user_params
params.require(:admin_user).permit(:id, :first_name, :last_name, :email, :password)
end
end
you can modify the flash message with an after_create callback for that case, something like this
ActiveAdmin.register User do
permit_params :name, :email, :password
after_create do |user|
flash[:notice] = "User has been created with the default password" if user.valid?
end
end
I have a modified copy of https://github.com/talho/openphin/blob/master/app/controllers/admin/invitations_controller.rb
The main code is primarily the same, however our system was upgraded a few months back to Rails 4.x and the invitation system no longer works.
The method with the issue is create. I have tried swapping out:
#invitation = Invitation.new(params[:invitation])
with
#invitation = Invitation.create(invitation_params)
And creating
def invitation_params
params.require(:invitation).permit!
end
However, here is what I have:
invitation_params = {"name":"Test","subject":"test","body":"test","organization_id":"","author_id":24448}
#invitation = {"id":null,"name":null,"body":null,"organization_id":null,"author_id":null,"subject":null,"created_at":null,"updated_at":null,"lock_version":0}
Also, if I use create!, then my output error is:
E, [2015-12-14T13:03:38.664099 #24385] ERROR -- : Validation failed: Author can't be blank (ActiveRecord::RecordInvalid)
I could use any guidance/help on why everything ends up as null.
You call return what leaved the method, before you call save! in the record. Furthermore you might want to read about Strong Parameters. You might want to change your code to:
#invitation = Invitation.new(
params.require(:invitation).permit(
:name, :subject, :body, :organization_id, :author_id
)
)
#invitation.save!
render :json => { :invitation => #invitation }.as_json
return
Please note that you usually do not need to call return in controller method. And when you call save! immediately after new then create! might be an better option:
def create
invitation = Invitation.create!(invitation_params)
render json: { invitation: invitation }.as_json
end
private
def invitation_params
params.require(:invitation).permit(
:name, :subject, :body, :organization_id, :author_id
)
end
I'm using strong parameters in my User controller in my Rails 4 application.
def new
#user = User.new
end
def create
#user = User.new(user_params)
if !#user.save
render :new
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :address_1, :address_2, :city, :state, :zip, :phone, :email, :password, :ssn)
end
I also have validation in the User model.
validates_presence_of :first_name, :last_name
If the user fills out my form to create their account and don't pass the validation, when the controller renders the new action, none of the previously fields the user filled out are populated, the entire form is blank. Am I missing something?
It seems you made new instance variable when validation fails.
You have to use failed instance variable in new form.
if you can't understand my answer please add your code of create action and form view.
I was using Single Table Inheritance and was pre-designating the type of user it would be. Once I removed STI, my fields were appearing in the form no problem.