Strong parameters and Nested Routes - Rails 4.0

931 views Asked by At

I have no idea how this works in rails but I set up routes like this:

  resources :users do
    resources :api_keys
  end

(User has_many: api_keys, api_key belongs_to: user)

So I then (since I only care about API Keys), created the following controller:

class ApiKeysController < ApplicationController
    before_action :authenticate_user!

    def index
        @user = User.find(params[:user_id])
        @api_key = User.apikeys
    end

    def create
        @user = User.find(params[:user_id])
        @api_key = ApiKey.new(create_new_api_key)
        create_api_key(@api_key, @user)
    end

    def destroy
        destroy_api_key
    end

    private

    def create_new_api_key
        params.require(:api_key).permit(user_attributes: [:id],  :api_key)
    end

end

Which states, authenticate user before every action, index fetches all api keys based on a user id. create is suppose to create an api key based on a user id, (note: create_api_key(@api_key, @user) just an abstracted method that states - if we saved, redirect to user_path with a message, if we failed, back to user path with a error message)

And destroy, well that just finds an api key, destroys it and redirects (again with the abstraction).

Whats the issue?

the create_new_api_key method. Its freaking out and saying:

syntax error, unexpected ')', expecting => (SyntaxError)

I thought this is how I pass in the user id ??

1

There are 1 answers

0
fivedigit On BEST ANSWER

You need to change the order of the arguments passed in to permit to fix the syntax error:

def create_new_api_key
    params.require(:api_key).permit(:api_key, user_attributes: [:id])
end