Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails中不允许的参数_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails中不允许的参数

Ruby on rails Rails中不允许的参数,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我已经为我的站点创建了一个注册表单,并在我的users\u controller.rb文件中包含了以下参数,以便将输入到相应字段的数据保存到我的数据库中。出于某种原因,当我试图提交我创建的注册表单时,控制台告诉我firstname和lastname是不允许的参数。错误是: 不允许的参数:firstname、lastname 注册成功,其他参数也输入到数据库中;但是,firstname和lastname列仍然为空 用户\u控制器.rb class UsersController < A

我已经为我的站点创建了一个注册表单,并在我的users\u controller.rb文件中包含了以下参数,以便将输入到相应字段的数据保存到我的数据库中。出于某种原因,当我试图提交我创建的注册表单时,控制台告诉我firstname和lastname是不允许的参数。错误是:

不允许的参数:firstname、lastname

注册成功,其他参数也输入到数据库中;但是,firstname和lastname列仍然为空

用户\u控制器.rb

   class UsersController < ApplicationController

def create
  @user = User.new(user_params)
  if @user.save
    flash[:success] = "You signed up successfully"
    flash[:color] = "valid"
    redirect_to @user
  else
    flash[:notice] = "Form is invalid"
    flash[:color] = "invalid"
    render "new"
  end
end

private
def user_params
  params.require(:user).permit(:firstname, :lastname, :email, :password, :password_confirmation)
end


def show
  @user = User.find(params[:id])
end

def new
end

end
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>

    </br> <%= f.text_field :firstname, placeholder: 'First Name' %> 
    </br> <%= f.text_field :lastname, placeholder: 'Last Name' %>
    </br> <%= f.text_field :email, placeholder: 'Email' %> 
    </br> <%= f.password_field :password, placeholder: 'Password' %>
    </br> <%= f.password_field :password_confirmation, placeholder: 'Confirm Password' %>


    <%= f.submit :Register %>
  <% end %>
create_table "users", force: :cascade do |t|
    t.string   "firstname"
    t.string   "lastname"
    t.string   "email"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "password_digest"
    t.string   "password"
  end
class User < ActiveRecord::Base

    has_secure_password

  validates_length_of :password, :in => 6..20, :on => :create
  validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] }

end
user.rb

   class UsersController < ApplicationController

def create
  @user = User.new(user_params)
  if @user.save
    flash[:success] = "You signed up successfully"
    flash[:color] = "valid"
    redirect_to @user
  else
    flash[:notice] = "Form is invalid"
    flash[:color] = "invalid"
    render "new"
  end
end

private
def user_params
  params.require(:user).permit(:firstname, :lastname, :email, :password, :password_confirmation)
end


def show
  @user = User.find(params[:id])
end

def new
end

end
<%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>

    </br> <%= f.text_field :firstname, placeholder: 'First Name' %> 
    </br> <%= f.text_field :lastname, placeholder: 'Last Name' %>
    </br> <%= f.text_field :email, placeholder: 'Email' %> 
    </br> <%= f.password_field :password, placeholder: 'Password' %>
    </br> <%= f.password_field :password_confirmation, placeholder: 'Confirm Password' %>


    <%= f.submit :Register %>
  <% end %>
create_table "users", force: :cascade do |t|
    t.string   "firstname"
    t.string   "lastname"
    t.string   "email"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "password_digest"
    t.string   "password"
  end
class User < ActiveRecord::Base

    has_secure_password

  validates_length_of :password, :in => 6..20, :on => :create
  validates :password_confirmation, presence: true, if: -> { new_record? || changes["password"] }

end

看起来这些参数可能是

{:firstname => 'foo', :lastname => 'baz'}
而不是

{"user" => {"firstname" => "foo", "lastname" => "baz"}}

它可能可以追溯到表单。

我遇到了完全相同的问题,但是使用了update方法。结果表明,这是由我使用的
around\u update
回调引起的。我将更新前后的
回调更改为更新后的
,问题得到了解决。

能否显示用户架构(从schema.rb文件)?显示提交的参数hash@SteveTurczyn请参阅上面添加的架构。@Brittany您可以在
@user=user.new(用户参数)之前放入调试器吗
并打印出
params
是什么?此外,该模型也会有所帮助,因为问题可能就在那里。@JesseWhitham添加了user.rb;请参见上文。对于新手的回复,我感到抱歉-但为什么这会发生在这两个值上,而不是电子邮件值上?我怎样才能解决这个问题?这其实是公平的。你能显示输入的参数吗?它们应该打印在rails Web服务器输出中。请参阅上面的传入参数-非常感谢,非常感谢。np。。虽然从你发布的代码中我看不出它为什么会这样。我很困惑,为什么这会让我感到悲伤-哈哈。当我通过终端创建用户时,它工作正常吗?