Lenocam
Lenocam

Reputation: 341

IRB Documentation, Rails 4, Syntax Error, Params, Console

I'm going to slip in a specific code question to illustrate my point, but my real concern is, where can find some translation or deeper explanation of how to use IRB and a good explanation of what the errors mean?

The error I'm getting in console:

SyntaxError: (irb):10: syntax error, unexpected '\n', expecting =>

I'm inexperienced in rails, but I think I understand what this says. Let me try:

"Syntax error, unexpected end of the line, expecting hash rocket" I assume this means I didn't correctly right out my params when I was creating a new Account and User/Owner.

I'm trying to create a new account and add the owner attribute at the same time, as it should work in my app. Below is the AccountController code related to my issue:

def create
    @account = Account.new(account_params)
        if @account.save
            sign_in(@account.owner)
            flash[:notice] = "Your account has been successfully created."
            redirect_to root_url(subdomain: @account.subdomain)
        else
            flash.now[:alert] = "Sorry, your account could not be created."
            render :new
        end
end

And the params also in AccountController:

private
    def account_params
        params.require(:account).permit(:name, :subdomain,
            { owner_attributes: [ :email, :password, :password_confirmation
                ]}
        )
    end

This is how I entered it into console:

Account.create! name: "Cheese", subdomain: "cheesy", { owner_attributes: [ email: "[email protected]", password: "foobar", password_confirmation: "foobar" ]}

User is made by Devise in case that matters in this circumstance.

Frankly, even if I've translated this correctly, which I probably didn't, did I make the error in my original code or in the console? But back to my original question where is a good resource to figure out what the IRB error is telling me? If I Google this error as is the results only remotely relate to my concern. More specifically, Google seems to treat \n as "n" and => as nothing.

Here is the complete console transcript:

2.1.5 :002 > Account.create! name: "Cheese", subdomain: "cheesy", { owner_attributes: [ email: "[email protected]", password: "foobar", password_confirmation: "foobar" ]} SyntaxError: (irb):2: syntax error, unexpected '\n', expecting => from /home/reed/.gem/ruby/2.1.5/gems/railties-4.2.2/lib/rails/commands/console.rb:110:in start' from /home/reed/.gem/ruby/2.1.5/gems/railties-4.2.2/lib/rails/commands/console.rb:9:instart' from /home/reed/.gem/ruby/2.1.5/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:68:in console' from /home/reed/.gem/ruby/2.1.5/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:inrun_command!' from /home/reed/.gem/ruby/2.1.5/gems/railties-4.2.2/lib/rails/commands.rb:17:in <top (required)>' from bin/rails:9:inrequire' from bin/rails:9:in `'

Upvotes: 0

Views: 136

Answers (2)

Lenocam
Lenocam

Reputation: 341

The response from fylooi was the correct one:

Ah, I missed the earlier hashes. Try create! name: "Cheese", subdomain: "cheesy", owner_attributes: { email: "[email protected]", password: "foobar", password_confirmation: "foobar" } – fylooi

Thanks fylooi

Upvotes: 0

fylooi
fylooi

Reputation: 3870

[ email: "[email protected]", password: "foobar", password_confirmation: "foobar" ] is an array containing hashes.

The inner hashes need curly braces.

Upvotes: 0

Related Questions