【问题标题】:IRB Documentation, Rails 4, Syntax Error, Params, ConsoleIRB 文档、Rails 4、语法错误、参数、控制台
【发布时间】:2016-05-03 20:53:14
【问题描述】:

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

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

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

我对 Rails 没有经验,但我想我明白这句话的意思。让我试试:

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

我正在尝试创建一个新帐户并同时添加所有者属性,因为它应该可以在我的应用程序中使用。以下是与我的问题相关的 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 似乎将 \n 视为“n”而 => 视为无。

这是完整的控制台记录:

2.1.5 :002 > Account.create!名称:“奶酪”,子域:“奶酪”,{所有者属性:[电子邮件:“email@real.com”,密码:“foobar”,密码确认:“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:instart' 来自/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! 来自/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' 从 bin/rails:9:in `'

【问题讨论】:

  • 戴夫,感谢您查看。我添加了完整的控制台脚本。

标签: ruby-on-rails ruby ruby-on-rails-4 devise irb


【解决方案1】:

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

内部散列需要花括号。

【讨论】:

  • fylooi,这就是你的意思吗:Account.create!名称:“flat”,子域:“line”,{owner_attributes:[{email:“email@notreal.com”},{密码:“foobar”},{password_confirmation:“foobar”}]}
  • 这应该是有效的语法,但我不确定 ActiveRecord 是否期望或{owner_attributes: {email: "email@notreal.com", password: "foobar", password_confirmation: "foobar"} }
  • 我尝试了我在评论中询问的方式以及您在最近的评论中向我展示的方式,但我仍然遇到同样的错误。
  • 啊,我错过了之前的哈希值。试试create! name: "Cheese", subdomain: "cheesy", owner_attributes: { email: "email@real.com", password: "foobar", password_confirmation: "foobar" }
【解决方案2】:

fylooi 的回答是正确的:

啊,我错过了之前的哈希值。尝试创建!名称:“奶酪”,子域:“奶酪”,所有者属性:{电子邮件:“email@real.com”,密码:“foobar”,密码确认:“foobar”} – fylooi

谢谢fylooi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多