Rails + Mongoid + Strong Parms failure - ruby-on-rails-4

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

Related

Bundler:GemNotFound error even though gem is locally installed

In my application, I am using a cron job to execute a simple model method.
A few gems were required for this task.
I installed a gem by including it in the gemfile and running the command
bundle install
Now when I run the server, I get the following error:
/home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/spec_set.rb:92:in `block in materialize': Could not find mime-types-data-3.2016.0221 in any of the sources (Bundler::GemNotFound)
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/spec_set.rb:85:in `map!'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/spec_set.rb:85:in `materialize'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/definition.rb:140:in `specs'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/definition.rb:185:in `specs_for'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/definition.rb:174:in `requested_specs'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/environment.rb:18:in `requested_specs'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/runtime.rb:13:in `setup'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler.rb:127:in `setup'
from /home/aditya9509/.rvm/gems/ruby-1.9.3-p551/gems/bundler-1.10.5/lib/bundler/setup.rb:18:in `<top (required)>'
from /home/aditya9509/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/aditya9509/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/aditya9509/Desktop/rubystack-2.0.0-33/projects/cricketStockExchange/config/boot.rb:3:in `<top (required)>'
from bin/rails:8:in `require_relative'
from bin/rails:8:in `<main>'
It says that the bundler was not able to locate mime-types-data-3.2016.0221 gem in any of the sources. To check, I ran the following command which gives a list of locally installed gems related to mime:
gem list | grep "mime"
This gave the following output:
mime-types (3.0, 2.99, 2.6.2)
mime-types-data (3.2016.0221)
Clearly the mime-types-data-3.2016.0221 is installed locally but bundler is not able to find it. Is there any fix to this. Below is my Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use mysql as the database for Active Record
gem 'mysql2', '~> 0.3.18'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'whenever', :require => false
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
gem 'mime-types-data','~> 3.2016.0221'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
I am not sure as to what more information would be appropriate so let me know, I will update my question as needed.
Thanks!
Does the issue persist when running the server with bundle exec rails s?

How to deploy Rails App using private_pub to heroku

I am trying to run my app on heroku which is using private_pub gem . Here in my local server I am running it perfectly . How can I push this app in heroku and run there perfectly . Here is my gem file :
source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.3'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'bcrypt', '3.1.7'
gem 'mailboxer'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sdoc', '0.4.0', group: :doc
gem 'devise'
gem 'paperclip', '~> 4.1'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'faker', '1.4.2'
gem 'chosen-rails'
gem 'jquery-turbolinks'
gem 'private_pub'
gem "thin"
#gem 'puma', '2.11.1'
group :development, :test do
#gem 'sqlite3', '1.3.9'
gem 'mysql2'
#gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
gem 'byebug', '3.4.0'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
gem 'puma', '2.11.1'
end
And This is my private_pub.yml file :
development:
server: "http://localhost:9292/faye"
secret_token: "secret"
test:
server: "http://localhost:9292/faye"
secret_token: "secret"
production:
server: "https://sheltered-sea-1191.herokuapp.com/faye"
secret_token: "mysecret key"
signature_expiration: 3600 # one hour
How can I push this app in heroku help me to push this on herok .
For this above question I have created a new heroku app and I put this code as it is told in this tutorial https://webprogramming29.wordpress.com/2013/02/15/setup-privatepub-or-faye-on-heroku/
And then I edited my code using the following tutorial and it is working great

how to show to do lists and images specific to user utilizing current_user helper from devise for RoR projects?

I'm creating a simple project management tool w/ to do lists and image uploads. Currently having an issue where I can't figure out how to display only the current logged in user's to do lists and images. I've tried the below in my dashboard controller:
class DashboardsController < ApplicationController
def index
#user = User.find(current_user.id)
#todo_lists = #user.TodoList.all
##todo_lists = TodoList.find(:all, :conditions => { :user_id => current_user[:id] } )
##todo_list = current_user.TodoList.find(params[:id])
##todo_lists = TodoList.find_all_by_user_id current_user[:id]
##todo_lists = TodoList.all
#paperclip_image = PaperclipImage.last
end
end
I keep getting a no method errors: NoMethodError in DashboardsController#index
undefined method `TodoList' for #. How would you display just items specific to the logged in user? I have user ids successfully assigned to each to do list and image upload in my dev DB.
installed gem versions:
gem 'rails', '4.1.0'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
gem 'paperclip', '~> 4.1'
gem 'devise'
TIA!
With a properly configured :has_many association from user to todo_lists it should just be
current_user.todo_lists
Has Many Association
class User < Active::Record
has_many :todo_lists
end
class TodoList < Active::Record
belongs_to :user
end

Prawn in a Ruby on Rails app, failing on production

I am stuck on an issue with running prawn in production.
I have followed the Railscast (revised http://railscasts.com/episodes/153-pdfs-with-prawn-revised?view=asciicast) pretty much to the letter. It works great in my development environment (Mac OS X 10.9.1) but causes unicorn to falter on my server. My production server is (Redhat => Red Hat Enterprise Linux Server release 6.4 (Santiago)). I have the gem installed via Gemfile.
Gemfile:
gem 'prawn'
gem 'prawn-qrcode'
Controller: (show action)
def show
##classroom = Classroom.find(params[:id])
#classroom = find_classroom
#page_title = #classroom.location.name
#classroom_alt = #classroom.location.name + " - " + #classroom.room_number
#building = find_building(#classroom.location_id)
#owner = Owner.find(#classroom.owner_id)
#room_schedule_contact = RoomScheduleContact.find_by rmrecnbr:(#classroom.rmrecnbr)
#building_image = #building.picture.url(:medium).to_s
#building_sign_image = #building.building_sign.url(:thumb).to_s
#search = Classroom.search(params[:search])
##classroom_herprod = Building.find(params[:location_id]).building_short_code
respond_to do |format|
format.html # show.html.erb
format.png { render :qrcode => "http://rooms.lsa.umich.edu/classrooms/#{#classroom.facility_code_heprod}", :level => :l, :unit => 8 }
format.pdf do
pdf = ClassroomPdf.new(#classroom)
send_data pdf.render, type: "application/pdf",
disposition: "inline"
end
end
end
I have a pdfs directory with a classroom_pdf.rb file that includes the following.
class ClassroomPdf < Prawn::Document
def initialize(classroom)
super(top_margin: 70)
text "This is a pdf"
end
end
(This is stripped down from what I had, but it is the most basic version that works on my dev system and fails on production).
I am running the following
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
gem 'rails', '4.0.2'
The error in the unicorn.stderr.log is as follows.
E, [2014-02-07T12:39:20.400750 #7300] ERROR -- : reaped #<Process::Status: pid 7306 exit 1> worker=1
I, [2014-02-07T12:39:20.401257 #7300] INFO -- : worker=1 spawning...
I, [2014-02-07T12:39:20.463964 #7529] INFO -- : worker=1 spawned pid=7529
I, [2014-02-07T12:39:20.521524 #7529] INFO -- : Refreshing Gem list
E, [2014-02-07T12:39:29.938423 #7326] ERROR -- : uninitialized constant Prawn (NameError)
/var/www/rooms.lsa.umich.edu/html/iris/releases/20140207173433/app/pdfs/classroom_pdf.rb:1:in `<top (required)>'
/var/www/rooms.lsa.umich.edu/html/iris/shared/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/engine.rb:465:in `block (2 levels) in eager_load!'
Here is the gemfile
source 'https://rubygems.org'
##gem 'rails', '3.2.14'
gem 'rails', '4.0.2'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
#gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
gem "haml"
#gem 'anjlab-bootstrap-rails', :require => 'bootstrap-rails'
gem 'bootstrap-sass', '~> 3.1.0'
#gem 'google-analytics-rails'
gem 'font-awesome-rails'
gem 'protected_attributes'
gem 'modernizr-rails'
gem "jquery-rails"
gem "devise", "3.0.3"
gem "cancan", ">= 1.6.9"
gem "rolify", ">= 3.2.0"
gem 'newrelic_rpm'
# QR-Code generation
gem 'rqrcode-rails3'
gem 'mini_magick'
gem 'prawn'
#gem 'prawn', '1.0.0.rc2'
gem 'prawn-qrcode'
#gem 'prawnto'
gem 'capistrano', '2.15.5'
#gem 'capistrano'
gem "simple_form"
gem 'will_paginate', '~> 3.0.5'
gem "therubyracer", :group => :assets, :platform => :ruby
gem 'will_paginate-bootstrap'
#gem 'kaminari'
## Used for uploading and resizing images (Need both paperclip and mini_magick)
gem "paperclip", "3.5.1"
#gem "meta_search"
gem "ransack"
gem 'annotate', ">=2.6.0"
group :production do
gem 'unicorn'
end
Any ideas?
It turns out that I needed to manually reboot unicorn for it to properly load the new gem file.

Active admin in Rails 4 throws error "Invalid option key"

Rails 4 app working just fine until Active Admin is included (from this thread) in the gemfile.
My Gemfile:
gem 'rails', '4.0.0'
gem 'rails_12factor', group: :production
gem 'pg'
gem 'sass-rails', '~> 4.0.0.rc1'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'protected_attributes'
gem 'rails-observers'
gem 'actionpack-page_caching'
gem 'actionpack-action_caching'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
gem 'sdoc', require: false
end
The error is thrown while attempting to start the rails server after a clean bundle install.
/var/lib/gems/1.9.1/gems/actionpack-4.0.0/lib/action_controller/railtie.rb:55:in `block (3 levels) in <class:Railtie>': Invalid option key: page_cache_directory= (RuntimeError)
Any help is greatly appreciated!
Try to use:
http://blog.remarkablelabs.com/2012/12/russian-doll-caching-cache-digests-rails-4-countdown-to-2013
Or:
run bundle install and reinstall actionpack-action_caching
https://github.com/rails/actionpack-action_caching