【问题标题】:Why am I getting Rails syntax error on params permit action in user_controller?为什么我在 user_controller 中的 params permit action 上出现 Rails 语法错误?
【发布时间】:2014-08-06 09:53:25
【问题描述】:

我创建了一个 user_controller,它基本上扩展了设计用户设置,使用以下语法:

class UsersController < ApplicationController
    def index
        @users = User.order('created_at DESC').all
    end
    def create
      @user = User.create(user_params)
    end

    private

    def user_params
      params.permit :user (:avatar, :email, :password, :password_confirmation )
    end
end

在我的模型 user.rb 我有:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


  has_attached_file :avatar, styles: {
        large: "600x450#",
        medium: "250x250#",
        small: "100x100#"
    }, :default_url => "/images/:style/filler.png"
  #validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
  validates :avatar, :email, :password, presence: true


end

这些都可以正常工作,但是当我重新加载页面时,我收到一个奇怪的错误,说存在语法错误,而且很奇怪,因为它在此之前工作。我假设它在 Rails 版本中存在某种语法错误。

错误是:

Error details saved to: /tmp/passenger-error-9mVpXM.html
  Message from application: /home/deployer/staging/releases/20140806094001/app/controllers/users_controller.rb:11: syntax error, unexpected ',', expecting ')'
...require(:user).permit (:avatar, :email, :password, :password...
...                               ^
/home/deployer/staging/releases/20140806094001/app/controllers/users_controller.rb:11: syntax error, unexpected ',', expecting :: or '[' or '.'
...it (:avatar, :email, :password, :password_confirmation )
...                               ^ (SyntaxError)

这有什么明显的错误吗?

【问题讨论】:

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


    【解决方案1】:

    您的user_params 方法错误。应该是:

    params.require(:user).permit(:avatar, :email, :password, :password_confirmation)
    

    【讨论】:

      【解决方案2】:

      user_params 中有语法错误

      你需要这样做:

      params.require(:user).permit(:avatar, :email, :password, :password_confirmation)
      

      您可以阅读more

      【讨论】:

      • 加上它有语法错误和不一致的空格。
      • @MarekLipka 感谢您的指点和反对,但是,我没有得到导致问题的不一致空格在哪里?
      • 现在已修复不一致的空格。尽管您的答案中仍然存在语法错误。
      猜你喜欢
      • 2017-11-03
      • 2019-07-07
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多