Rails + Mongoid + Strong Parms failure -


so, i'm relatively new ruby , rails. i'm playing around mongoid database, though knowledge hold using active record.

i created fresh project, being sure skip active record didn't have perform steps remove later in favor of mongoid.

i created scaffold rails g scaffold user , went on way looking around. since know mongo schemaless db, went model , modified so:

class user   include mongoid::document   include mongoid::timestamps    field :name,         :type => string  end 

pretty simple, 1 field ( ). so, added field form via normal route <%= text_field_tag :name %> , restarted server rails s

i went dev site, , appears work should. so, submit form , submits. looking @ db, document created without name field. remembering strong attributes active record, went users_controller.rb , modified so:

def user_params     params.require(:user).permit(:name) end 

at point, not remembering if need restart server or not -- do, on safe side. once server running go site, , submit form , tells me:

param missing or value empty: user

not sure i've done wrong here, or if mongoid need .require line ( in honesty don't know does, except thought requiring these fields in permit being submitted user model ) removing .require portion , doing params.permit(:name) processes information should.

$ ruby -v ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux] $ rails --version rails 4.2.1 

and gemfile:

source 'https://rubygems.org'   gem 'rails', '4.2.1' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' # use coffeescript .coffee assets , views gem 'coffee-rails', '~> 4.1.0' gem 'jquery-rails' gem 'turbolinks' gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc gem 'mongoid', '~> 4.0.0' # use activemodel has_secure_password gem 'bcrypt', '~> 3.1.7' #gem 'rspec-its' # use unicorn app server #gem 'unicorn'  # use capistrano deployment # gem 'capistrano-rails', group: :development #gem 'font-awesome-rails', '~> 4.3.0.0' #gem 'foundation-rails' group :development, :test   gem 'rspec-rails', '~> 3.2.1'   gem 'byebug'   gem 'web-console', '~> 2.0'   gem 'spring'   gem 'factory_girl_rails', '~> 4.5.0' end group :development   gem 'quiet_assets', '~> 1.1.0' end  group :test   gem 'capybara', '~> 2.4.4'   gem 'capybara-email', '~> 2.4.0'   gem 'shoulda-matchers', '~> 2.8.0' end 

any ideas of i've done wrong? code associated generated rails new , rails g

first off don't need restart rails server of changes you've described.

the error you're getting means params[:user] empty (or missing). if check development.log you'll see params[:name] set instead.

this because you've used text_field_tag, doesn't name input nest value in params[:user].

assuming you're using form_for create form, want instead do

f.text_field :name 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -