So, I'm relatively new to ruby and rails. I'm playing around with mongoid for my database, though the only knowledge I hold is using active record.
I created a fresh project, being sure to skip active record so that I didn't have to perform the steps to remove it later in favor of mongoid.
I created a scaffold rails g scaffold user
and went on my way looking around. So since I know that mongo is a schemaless db, I went to the model and modified it like so:
class User
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
end
pretty simple, just one field ( for now ). So, I added the field to my form via the normal route <%= text_field_tag :name %>
and restarted my server rails s
I went to the dev site, and everything appears to work as it should. So, I submit the form and it submits. Looking at the DB, a document is created only without the name field. Remembering about strong attributes
for active record, I went to the users_controller.rb
and modified it like so:
def user_params
params.require(:user).permit(:name)
end
At this point, not remembering if I need to restart the server or not -- I do, just to be on the safe side. Once the server is running I go to the site, and submit the form and it tells me:
param is missing or the value is empty: user
Not sure what I've done wrong here, or if with mongoid I even need the .require line ( in all honesty I don't know what it does, except I thought it was requiring that these fields in permit are being submitted to the user model ) as removing the .require portion and just doing params.permit(:name)
processes the information as it should.
$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
$ rails --version
Rails 4.2.1
and my GemFile:
source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and 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 as the app server
#gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
#gem 'font-awesome-rails', '~> 4.3.0.0'
#gem 'foundation-rails'
group :development, :test do
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 do
gem 'quiet_assets', '~> 1.1.0'
end
group :test do
gem 'capybara', '~> 2.4.4'
gem 'capybara-email', '~> 2.4.0'
gem 'shoulda-matchers', '~> 2.8.0'
end
Any ideas of what I've done wrong? Only code associated with this is what was generated by rails new
and rails g
First off you don't need to restart the rails server for any of the changes you've described.
The error you're getting just means that params[:user]
is empty (or missing). If you check your development.log you'll see that params[:name]
is set instead.
This is because you've used text_field_tag
, which doesn't name the input so as to nest the value in params[:user].
Assuming you're using form_for
to create your form, you want to instead do
f.text_field :name