繁体   English   中英

IRB文档,Rails 4,语法错误,参数,控制台

[英]IRB Documentation, Rails 4, Syntax Error, Params, Console

我将通过一个特定的代码问题来说明我的观点,但我真正关心的是,在哪里可以找到有关如何使用IRB的一些翻译或更深入的说明,以及对错误含义的良好解释?

我在控制台中遇到的错误:

语法错误:(irb):10:语法错误,意外的'\\ n',期望=>

我没有经验,但是我想我明白这是什么意思。 让我尝试:

“语法错误,行的意外末尾,期待哈希火箭”,我认为这意味着在创建新的帐户和用户/所有者时,我没有正确排除参数。

我正在尝试创建一个新帐户并同时添加owner属性,因为它应该可以在我的应用中正常工作。 以下是与我的问题相关的AccountController代码:

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

而且参数也位于AccountController中:

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

这是我将其输入控制台的方式:

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

在这种情况下,由Devise创建用户。

坦白说,即使我翻译正确(可能没有翻译),我还是在原始代码或控制台中出错了? 但是回到我最初的问题,哪里有很好的资源可以弄清IRB错误告诉我什么? 如果我用Google搜索这个错误,那么结果只是与我的关注点无关。 更具体地说,Google似乎将\\ n视为“ n”,而=>则视为“无”。

这是完整的控制台记录:

2.1.5:002> Account.create! 名称:“ Cheese”,子域:“ cheesy”,{owner_attributes:[电子邮件:“ email@real.com”,密码:“ foobar”,password_confirmation:“ foobar”]} SyntaxError:(irb):2:语法错误,意外的'\\ n',期望从/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:in start'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:in run_command中! 来自/home/reed/.gem/ruby/2.1.5/gems/railties-4.2.2/lib/rails/commands.rb:17:in <top (required)>' from bin/rails:9:in require '来自bin / rails:9:in'

[ email: "email@real.com", password: "foobar", password_confirmation: "foobar" ]是一个包含哈希的数组。

内部哈希需要大括号。

fylooi的回答是正确的:

啊,我错过了以前的哈希值。 尝试创建! 名称:“ Cheese”,子域:“ cheesy”,owner_attributes:{电子邮件:“ email@real.com”,密码:“ foobar”,password_confirmation:“ foobar”} – fylooi

谢谢fylooi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM