Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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/24.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 表单_中的自定义字段用于_Ruby On Rails_Ruby_Forms - Fatal编程技术网

Ruby on rails 表单_中的自定义字段用于

Ruby on rails 表单_中的自定义字段用于,ruby-on-rails,ruby,forms,Ruby On Rails,Ruby,Forms,尝试创建一个配置文件表单,用户可以在其中向其用户配置文件中添加有关其爱好的信息。但是当我尝试这样做时,我得到了以下错误 NoMethodError in Users#edit_profile undefined method `hobbies' for #<User:0x007ff1a8a1f198> NoMethodError在用户中#编辑#配置文件 未定义的方法“嗜好”# 有人知道问题可能是什么,或者可能的解决方案吗?我是rails新手,但我的印象是“tex

尝试创建一个配置文件表单,用户可以在其中向其用户配置文件中添加有关其爱好的信息。但是当我尝试这样做时,我得到了以下错误

    NoMethodError in Users#edit_profile

    undefined method `hobbies' for #<User:0x007ff1a8a1f198>
NoMethodError在用户中#编辑#配置文件
未定义的方法“嗜好”#
有人知道问题可能是什么,或者可能的解决方案吗?我是rails新手,但我的印象是“text_field”是使任何自定义输入工作的安全赌注。安装strong parameters gem会有帮助吗

编辑_profile.html.erb

<h2>Tell us about yourself</h2>

<%= form_for @user do |f| %>

    <%= f.label :first_name %><br />
    <%= f.text_field :first_name, autofocus: true %>

    <%= f.label :last_name %><br />
    <%= f.text_field :last_name %>

    <%= f.label :hobbies %><br />
    <%= f.text_field :hobbies %>

  <div><%= f.submit "Update" %></div>
<% end %>
告诉我们你自己的情况


用户_controller.rb

class UsersController < ApplicationController
  before_filter :authenticate_user!
    def index
      @users = User.all
    end

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

    def new
      @user = User.new
    end

    def create
    end

    def edit
    end

    def update
      @user = User.find(params[:id])
      @user.update!(user_params)
      redirect_to @user
    end

    def destroy
    end

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

    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :hobbies)
    end

end
class UsersController
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

  attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :hobbies

  #validates :first_name, presence: true, length: { maximum: 50 }
  #validates :last_name, presence: true, length: { maximum: 50 }

end
class用户
您没有提到,但我假设您运行的是Rails 4.x

引入了Rails 4.x,因此您需要在控制器中添加一个私有方法来设置允许的参数,并从模型中删除可访问的属性

因此,在您的情况下,它将是:

 def user_params
    params.require(:first_name, :last_name).permit(:email, :password, :password_confirmation, :remember_me, :hobbies)
  end
如果您仍然难以理解这个概念,或者来自以前的Rails版本,请查看

有人知道问题可能是什么,或者可能的解决方案吗

当然-问题是您的
用户
模型中没有
嗜好
属性:)

既然你是新来的,我会在回答这个问题后解释一下Rails。但是,让我解释一下创建正确属性的价值:

#app/models/user.rb
class User < ActiveRecord::Base
   attr_accessor :hobbies
end
迁移应该创建如下内容:

class AddPartNumberToProducts < ActiveRecord::Migration
  def change
    add_column :users, :hobbies, :string
  end
end
class AddPartNumberToProducts
你有兴趣爱好专栏吗?谢谢你如此详尽的回答。当您说数据不会持久化时,这是否意味着在提交表单时数据不会存储在数据库中?是的,没错。数据持久性就像将数据存储在硬盘上一样——它永远在那里如果你想回答你前面的问题,我使用的是rails 4.1,有没有办法使数据持久化?你对使用强参数有什么看法?
class AddPartNumberToProducts < ActiveRecord::Migration
  def change
    add_column :users, :hobbies, :string
  end
end