Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate params #16

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: duplicate params
  • Loading branch information
karinevieira committed Dec 30, 2023
commit f2808f51af92bd8dbbba0720371ba7ded948642c
17 changes: 10 additions & 7 deletions app/controllers/api/v1/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def show
end

def update
if profile.update(user_params[:profile_attributes])
if profile.update(profile_params)
render json: UserSerializer.new(current_user)
else
render json: profile.errors, status: :unprocessable_entity
Expand All @@ -17,12 +17,15 @@ def update

private

def user_params
params.require(:user).permit(
profile_attributes: %i[
weight height_in_cm workout_in_min workout_days_frequency
active_lifestyle gender physical_activities
]
def profile_params
params.require(:profile).permit(
:weight,
:height_in_cm,
:workout_in_min,
:workout_days_frequency,
:active_lifestyle,
:gender,
:physical_activities
)
end

Expand Down
2 changes: 1 addition & 1 deletion docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ It returns an authentication JWT token in the response header, which will need t
-H 'Accept: application/json'
-H 'Content-Type: application/json'
-H 'Authorization: user-token'
-d '{"user": {"profile_attributes": {"weight": 45, "height_in_cm": 153, "workout_in_min": 60, "workout_days_frequency": 3}}}'
-d '{"profile": {"weight": 45, "height_in_cm": 153, "workout_in_min": 60, "workout_days_frequency": 3}}'
```

| Parameter | Type | Description |
Expand Down
16 changes: 6 additions & 10 deletions spec/requests/api/v1/profiles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
it "renders a successful response" do
jwt_token = sign_in

profile = {
profile_attributes: {
weight: 50, height_in_cm: 150, workout_in_min: 60, workout_days_frequency: 3, active_lifestyle: true
}
attributes = {
weight: 50, height_in_cm: 150, workout_in_min: 60, workout_days_frequency: 3, active_lifestyle: true
}

put api_v1_profile_path, headers: { Authorization: jwt_token }, params: { user: profile }, as: :json
put api_v1_profile_path, headers: { Authorization: jwt_token }, params: { profile: attributes }, as: :json

expect(response).to be_successful
end
Expand All @@ -43,13 +41,11 @@
user = create(:user)
jwt_token = sign_in(user)

profile = {
profile_attributes: {
weight: 50, height_in_cm: 150, workout_in_min: 60, workout_days_frequency: 3, active_lifestyle: true
}
attributes = {
weight: 50, height_in_cm: 150, workout_in_min: 60, workout_days_frequency: 3, active_lifestyle: true
}

put api_v1_profile_path, headers: { Authorization: jwt_token }, params: { user: profile }, as: :json
put api_v1_profile_path, headers: { Authorization: jwt_token }, params: { profile: attributes }, as: :json

expect(user.reload.profile.attributes).to include(
"weight" => 50,
Expand Down