Home > Software engineering >  Resolving ruby params.permit syntax error
Resolving ruby params.permit syntax error

Time:08-09

params.permit(
            :type,
            payload: [
                user: [
                    :id, :email, :isAnonymous, :isAdmin, :firstName, :createdAt, :updatedAt, :phone, :inviteCode, :employeeId, :activated, :lastName, 
                    :locked, :authVersion, :isCompanyAdmin, :isEditor, :isAnalyst, :isManager, :seenWebOnboarding, :language, :hidden, :deleted, :isSuperAdmin, :hasSignedInAsLearner,
                    :hasCreatedCourse, :hasInvitedAdmins, :isCompanyOwner, :sendActivationReminders, :timezone, :isUnsubscribedFromEngagementEmails, :authType, :activatedDate, :shouldShowGamificationOnboarding, :depositedPoints, :name,
                    :identifiers
                ],
                course: [
                    :id, :customerId, :title, :courseKey, :public, :icon, :createdAt, :updatedAt, :courseImageUrl, :colour, :published, :type,
                    :requiresEnrolment, :isSample, :completionMessage, :completionButtonUrl, :completionButtonText, :isHidden, :learnersCount
                ],
                parentGroup: [
                    :id, :customerId, :name, :createdAt, :updatedAt, :parentGroupId, :welcomePageConfigId, :selfSignupConfigId, :language, :deleted,
                    :ssoConfigId, :cookieConsentDisabled, :usersCount, :activatedUsersCount
                ],
                completion: [
                    :overallAssessmentScore, :overallLessonScore, :scoresBreakdown,  :courseStartDate, :courseCompletionDate, 
                    :courseCompletionDurationInSeconds, :completionLanguage
                ]
            ],
            :timestamp).to_h
    end

This is the code that I'm having an issue with. When I try making a call to this endpoint, I run into this error.

syntax error, unexpected ')', expecting =>
             :timestamp).to_h
                       ^ excluded from capture: Not configured to send/capture in environment 'development'

I've noticed if I put the timestamp before the payload in the params.permit than this issue isn't there anymore. However, this is not how the request is going to formatted and I need to follow the structure of the request as it determines a hash later on in the code. Anyone know how to resolve this?

CodePudding user response:

Even though you can do this in a method call, you can't have hash-style elements in a Ruby array. You need to have a formal hash inside of the array itself.

You need to split it out explicitly:

params.permit(
  :type,
  {
    payload: [ ... ]
  }
)

CodePudding user response:

From further research, it seems that having a nested attributes must be permitted last. Makes sense as to why this only occurs when timestamp is behind payload.

I'm going to deal with this by just creating a new hash with the params later on with the correct structure.

source: https://smartlogic.io/blog/permitting-nested-arrays-using-strong-params-in-rails/

  • Related