NoMethodError though method has been defined - ruby-on-rails

I have a Ruby On Rails weblog application and am getting a NoMethodError in PostsController#show issue when opening a post. PostsController is stored under rails/weblog/app/controllers
I get undefined method 'set_current_page' for #<PostsController:0x007fb162831e70> however set_current_page is defined like so:
private
def set_current_page
#current_page = params[:page] || 1
end
end
and then used in a before_action
before_action :set_current_page, except: [:index]
before_action :set_post, only: [:show, :edit, :update, :destroy]
Full stack trace:
activesupport (5.0.0.1) lib/active_support/callbacks.rb:382:in `block in make_lambda'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:150:in `block (2 levels) in halting_and_conditional'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:12:in `block (2 levels) in <module:Callbacks>'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:454:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:454:in `each'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:454:in `call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.1) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.0.1) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0.1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:725:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0.1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.4.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.4.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.4.0) lib/web_console/middleware.rb:18:in `catch'
web-console (3.4.0) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/method_override.rb:22:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'
The posts_controller.rb file:
class PostsController < ApplicationController
before_action :set_current_page, except: [:index]
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.paginate(page: params[:page],
per_page: params[:per_page])
.order('title, user_id')
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
#post.user = User.find_by email: params[:email]
respond_to do |format|
if #post.save
format.html { redirect_to (post_url(#post, page: #current_page)), notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
#post.user = User.find_by email: params[:email]
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to (post_url(#post, page: #current_page)), notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
send
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url(page: #current_page), notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
def set_current_page
#current_page = params[:page] || 1
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :body, :page)
end
end
end

You have a typo in your update action - send instead of end.
This means Ruby is interpreting everything after it as part of that update method - and it is not defined outside the method.
In combination with the extra end that's been included after your list of private methods this means you have the correct number of ends so don't see a syntax error, but the class is not interpreted in the way you expect.

Related

error message: undefined method `create' for #<GymClass:0x000000000d696c18> Did you mean? created_at. Trying to use policy design patter

Im trying to use the policy design pattern but cant seem to get it to work. It's saying that my create method in undefined but cant figure out why. Please help:
This is my policy file:
class GymClassPolicy < ApplicationPolicy
attr_reader :user, :gym_class
def initialize(user, gym_class)
#user = user
#gym_class = gym_class
end
def create?
user.admin?
end
end
This is my controller. I am trying to use the policy design pattern in the create method
require 'observer'
require './app/policies/gym_class_policy'
class GymClassesController < ApplicationController
include Observable
before_action :set_gym_class, only: [:show, :edit, :update, :destroy]
# GET /gym_classes
# GET /gym_classes.json
def index
#gym_classes = GymClass.all
end
# GET /gym_classes/1
# GET /gym_classes/1.json
def show
end
# GET /gym_classes/new
def new
#gym_class = GymClass.new
end
# GET /gym_classes/1/edit
def edit
end
def gym_class_policy
#_gym_class_policy ||= GymClassPolicy.new(gym_class)
end
# POST /gym_classes
# POST /gym_classes.json
def create
#gym_class = GymClass.new(gym_class_params)
authorize #gym_class
if #gym_class.create(gym_class_params)
redirect_to #post
else
render :edit
end
respond_to do |format|
if #gym_class.save
format.html { redirect_to #gym_class, notice: 'Gym class was successfully created.' }
format.json { render :show, status: :created, location: #gym_class }
else
format.html { render :new }
format.json { render json: #gym_class.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /gym_classes/1
# PATCH/PUT /gym_classes/1.json
def update
respond_to do |format|
if #gym_class.update(gym_class_params)
format.html { redirect_to #gym_class, notice: 'Gym class was successfully updated.' }
format.json { render :show, status: :ok, location: #gym_class }
else
format.html { render :edit }
format.json { render json: #gym_class.errors, status: :unprocessable_entity }
end
end
end
# DELETE /gym_classes/1
# DELETE /gym_classes/1.json
def destroy
#gym_class.destroy
respond_to do |format|
format.html { redirect_to gym_classes_url, notice: 'Gym class was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gym_class
#gym_class = GymClass.find(params[:id])
end
# Only allow a list of trusted parameters through.
def gym_class_params
params.require(:gym_class).permit(:Type, :PTName, :Date, :Time)
end
def Time=(new_Time)
#Time = new_Time
notify_observers(self)
end
end
Lastly this is my full trace:
**
activemodel (6.0.2.1) lib/active_model/attribute_methods.rb:431:in method_missing'
app/controllers/gym_classes_controller.rb:43:increate'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in send_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:196:inprocess_action'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:30:in process_action'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:42:inblock in process_action'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:135:in run_callbacks'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:41:inprocess_action'
actionpack (6.0.2.1) lib/action_controller/metal/rescue.rb:22:in process_action'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:inblock in process_action'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:ininstrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in instrument'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:32:inprocess_action'
actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:245:in process_action'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:inprocess_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:in process'
actionview (6.0.2.1) lib/action_view/rendering.rb:39:inprocess'
actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in dispatch'
actionpack (6.0.2.1) lib/action_controller/metal.rb:252:indispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:in dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:inserve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:in block in serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:ineach'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in serve'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:incall'
rack-pjax (1.1.0) lib/rack/pjax.rb:12:in call'
remotipart (1.4.4) lib/remotipart/middleware.rb:32:incall'
warden (1.2.8) lib/warden/manager.rb:36:in block in call'
warden (1.2.8) lib/warden/manager.rb:34:incatch'
warden (1.2.8) lib/warden/manager.rb:34:in call'
rack (2.2.2) lib/rack/tempfile_reaper.rb:15:incall'
rack (2.2.2) lib/rack/etag.rb:27:in call'
rack (2.2.2) lib/rack/conditional_get.rb:40:incall'
rack (2.2.2) lib/rack/head.rb:12:in call'
actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:incall'
rack (2.2.2) lib/rack/session/abstract/id.rb:266:in context'
rack (2.2.2) lib/rack/session/abstract/id.rb:260:incall'
actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:in call'
activerecord (6.0.2.1) lib/active_record/migration.rb:567:incall'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in block in call'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:inrun_callbacks'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:incall'
actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:incall'
web-console (4.0.1) lib/web_console/middleware.rb:132:in call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:inblock in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:incall'
actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in call'
railties (6.0.2.1) lib/rails/rack/logger.rb:38:incall_app'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in block in call'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:inblock in tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:intagged'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:incall'
actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:incall'
rack (2.2.2) lib/rack/method_override.rb:24:in call'
rack (2.2.2) lib/rack/runtime.rb:22:incall'
activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:incall'
actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in call'
rack (2.2.2) lib/rack/sendfile.rb:110:incall'
actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:83:in call'
webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:inperform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in call'
railties (6.0.2.1) lib/rails/engine.rb:526:incall'
puma (4.3.1) lib/puma/configuration.rb:228:in call'
puma (4.3.1) lib/puma/server.rb:681:inhandle_request'
puma (4.3.1) lib/puma/server.rb:472:in process_client'
puma (4.3.1) lib/puma/server.rb:328:inblock in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
**
Change create to save at app/controllers/gym_classes_controller.rb:43
#gym_class = GymClass.new(gym_class_params)
authorize #gym_class
if #gym_class.save(gym_class_params)
redirect_to #post
...

Failed to save the new associated preference has_one association

I try to create a nested model form for the has_one preference. (i'm using Rails 4)
but i get the below error which i do not understand how to resolve. This happens in the case when a user has not completed the preference form (with validations for the attributes of preference in place)
terminal display of error
ActiveRecord::RecordNotSaved - Failed to save the new associated preference.:
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:43:in `block in replace'
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:98:in `block in transaction_if'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:98:in `transaction_if'
activerecord (4.2.0) lib/active_record/associations/has_one_association.rb:33:in `replace'
activerecord (4.2.0) lib/active_record/associations/singular_association.rb:17:in `writer'
activerecord (4.2.0) lib/active_record/associations/builder/association.rb:123:in `preference='
app/controllers/preferences_controller.rb:55:in `create'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:308:in `block (2 levels) in halting'
public_activity (1.5.0) lib/public_activity/utility/store_controller.rb:25:in `store_controller_for_public_activity'
activesupport (4.2.0) lib/active_support/callbacks.rb:427:in `block in make_lambda'
activesupport (4.2.0) lib/active_support/callbacks.rb:307:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
warden (1.2.4) lib/warden/manager.rb:35:in `block in call'
warden (1.2.4) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process'
thin (1.5.1) lib/thin/connection.rb:79:in `pre_process'
thin (1.5.1) lib/thin/connection.rb:54:in `process'
thin (1.5.1) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.9.1) lib/eventmachine.rb:193:in `run'
thin (1.5.1) lib/thin/backends/base.rb:63:in `start'
thin (1.5.1) lib/thin/server.rb:159:in `start'
rack (1.6.4) lib/rack/handler/thin.rb:19:in `run'
rack (1.6.4) lib/rack/server.rb:286:in `start'
railties (4.2.0) lib/rails/commands/server.rb:80:in `start'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:80:in `block in server'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:75:in `server'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
railties (4.2.0) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:9:in `<top (required)>'
spring (1.7.1) lib/spring/client/rails.rb:28:in `call'
spring (1.7.1) lib/spring/client/command.rb:7:in `call'
spring (1.7.1) lib/spring/client.rb:30:in `run'
spring (1.7.1) bin/spring:49:in `<top (required)>'
spring (1.7.1) lib/spring/binstub.rb:11:in `<top (required)>'
/Users/ARTLoe/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
bin/spring:13:in `<top (required)>'
bin/rails:3:in `<main>'
could one advise me where i am going wrong?
In my user, and preference model i have the following :
class User < ActiveRecord::Base
has_one :preference, dependent: :destroy
accepts_nested_attributes_for :preference
end
class Preference < ActiveRecord::Base
validates_presence_of :category_idealethnicity, presence: true, message: "can't be blank"
validates_presence_of :category_idealreligion, presence: true, message: "can't be blank"
belongs_to :user
has_and_belongs_to_many :category_idealethnicity, dependent: :destroy
has_and_belongs_to_many :category_idealreligion, dependent: :destroy
end
users_controller
class UsersController < ApplicationController
respond_to :html, :xml, :json
before_action :authenticate_user!, :set_user, only: [:show, :edit, :update, :destroy]
def index
...
end
def show
....
end
def edit
...
end
def update
...
end
private
def set_user
#user = User.friendly.find(params[:id])
end
def user_params
params.require(:user).permit(:email, :firstname, preference_attributes: [:town, category_idealreligion_ids: [], category_idealsmoke_ids: []])
end
end
preferences_controller
class PreferencesController < ApplicationController
before_action :authenticate_user!, :set_preference, only: [:show, :edit, :update, :destroy]
def index
#preferences = Preference.all
end
def show
...
end
def new
#users = User.random
#user = current_user
#preference = #user.build_preference
end
def edit
...
end
def create
#user = User.friendly.find(params[:user_id])
#user.preference = Preference.new(preference_params)
#preference = #user.preference
respond_to do |format|
if #preference.save
format.html { redirect_to #preference, notice: 'Preference was successfully created.' }
format.json { render :show, status: :created, location: #preference }
else
format.html { render :new }
format.json { render json: #preference.errors, status: :unprocessable_entity }
end
end
end
def update
...
end
def destroy
...
end
private
def set_preference
#preference = Preference.find(params[:id])
end
def preference_params
params.require(:preference).permit(:town, :user_id, category_idealethnicity_ids: [], category_idealreligion_ids: [])
end
end
routes
Rails.application.routes.draw do
devise_for :users
resources :users do
resources :preferences
end
end
preference form
<div>
<%= simple_form_for [#user, #preference] do |f| %>
<%= f.error_notification %>
<%= f.association :category_idealethnicity, collection: CategoryIdealethnicity.all, prompt: "select", label: false, as: :check_boxes %>
<%= f.association :category_idealreligion, collection: CategoryIdealreligion.all, prompt: "select", label: false, as: :check_boxes %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
</div>
The Answer to the question: display of my create method was wrong. below is the correct code
def create
#preference = Preference.new(preference_params)
#preference.user = current_user
respond_to do |format|
if #preference.save
format.html { redirect_to #preference, notice: 'Preference was successfully created.' }
format.json { render :show, status: :created, location: #preference }
else
format.html { render :new }
format.json { render json: #preference.errors, status: :unprocessable_entity }
end
end
end
Your create method looks suspicious to me. Try the below instead
def create
#user = User.friendly.find(params[:user_id])
#preference = #user.preference.new(preference_params)
respond_to do |format|
if #preference.save
format.html { redirect_to #preference, notice: 'Preference was successfully created.' }
format.json { render :show, status: :created, location: #preference }
else
format.html { render :new }
format.json { render json: #preference.errors, status: :unprocessable_entity }
end
end
end

Multiple User Types with Devise and STI - Password Change Issue

I am building out an app using ruby 2.3.1 and rails 5, user authentication is Devise 4.
This project, my instructor wants an app with User, AdminUser ClientUser and SupportUser types, he is not allowing DeviseInvitable to be used, this app will never be used in production.
My workflow, at present is as follows, when a user (of any type is created) a password is generated and sent (this works), a user after confirming the account and logging in is forced to change their password (this works for User but not for AdminUser SupportUser and ClientUser)
here is how this is all setup:
#application_controller.rb
# Force User To Change Password On First Login
def after_sign_in_path_for(resource)
if current_user.pw_changed == "false"
edit_passwords_path
else
authenticated_root_path
end
end
passwords_controller.rb
class PasswordsController < ApplicationController
def edit
#user = current_user
end
def update
if current_user.update_with_password(user_params.merge(pw_changed: "true"))
flash[:notice] = 'Your Password Has Been Sucessfully Updated.'
redirect_to authenticated_root_path
else
flash[:error] = 'Oh No! Something Went Wrong, Please Try Again.'
render :edit
end
end
private
def user_params
params.require(:user).permit(:current_password, :password, :password_confirmation)
end
#views
_form.html.erb
edit.html.erb
# routes.rb
# Force Password Change Routes and Resources
resource :passwords
resources :users, :type => 'User'
resources :admin_users, :type => 'AdminUser'
resources :client_users, :type => 'ClientUser'
resources :support_users, :type => 'SupportUser'
My Models:
class User < ApplicationRecord - This also acts as custom show and user index
end
class AdminUser < User
end
class ClientUser < User
end
class SupportUser < User
end
when I Try to update an AdminUsers Password on first login this is the error and server output I get:
Server Output
Started PATCH "/passwords" for ::1 at 2016-09-26 14:59:28 -0600
Processing by PasswordsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"yEO6cUTLv8x2MU9lf1+bOYrpWTie17wE0TsIDEilbJzMWKSWmlQHC8mC3Bb9bkP3L3OhooY+wuwolFDO6fzxlg==", "admin_user"=>{"current_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update Admin user"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 5], ["LIMIT", 1]]
Completed 400 Bad Request in 3ms (ActiveRecord: 0.9ms)
ActionController::ParameterMissing - param is missing or the value is empty: user:
actionpack (5.0.0.1) lib/action_controller/metal/strong_parameters.rb:308:in `require'
app/controllers/passwords_controller.rb:19:in `user_params'
app/controllers/passwords_controller.rb:8:in `update'
actionpack (5.0.0.1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0.1) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:448:in `block (2 levels) in around'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:267:in `block (2 levels) in halting_and_conditional'
activesupport (5.0.0.1) lib/active_support/core_ext/time/zones.rb:64:in `use_zone'
app/controllers/application_controller.rb:20:in `user_time_zone'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:382:in `block in make_lambda'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:266:in `block in halting_and_conditional'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:447:in `block in around'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0.1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.1) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.0.1) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0.1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.1) lib/action_dispatch/routing/route_set.rb:725:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0.1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.3.1) lib/web_console/middleware.rb:131:in `call_app'
web-console (3.3.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.3.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.3.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/method_override.rb:22:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'
Started POST "/__better_errors/462cce3c438acc05/variables" for ::1 at 2016-09-26 14:59:28 -0600
You can use ActiveModel::Naming to get param keys, I18n keys or humanized versions of the names of any of your models.
class PasswordsController < ApplicationController
def edit
end
def update
if current_user.update_with_password(user_params.merge(pw_changed: "true"))
flash[:notice] = 'Your Password Has Been Sucessfully Updated.'
redirect_to authenticated_root_path
else
flash[:error] = 'Oh No! Something Went Wrong, Please Try Again.'
render :edit
end
end
private
def user_params
param_key = current_user.class.model_name.param_key
params.require(param_key)
.permit(:current_password, :password, :password_confirmation)
end
end
You should also learn how to use the case statement (the Ruby equivalent of switch)
So I think i have found a fix, probably not the most orthodox solution, but its working.. I made the following changes to the passwords controller:
class PasswordsController < ApplicationController
def edit
end
def update
if current_user.type == "User"
if current_user.update_with_password(user_params.merge(pw_changed: "true"))
flash[:notice] = 'Your Password Has Been Sucessfully Updated.'
redirect_to authenticated_root_path
else
flash[:error] = 'Oh No! Something Went Wrong, Please Try Again.'
render :edit
end
elsif current_user.type == "AdminUser"
if current_user.update_with_password(user_params.merge(pw_changed: "true"))
flash[:notice] = 'Your Password Has Been Sucessfully Updated.'
redirect_to authenticated_root_path
else
flash[:error] = 'Oh No! Something Went Wrong, Please Try Again.'
render :edit
end
elsif current_user.type == "ClientUser"
if current_user.update_with_password(user_params.merge(pw_changed: "true"))
flash[:notice] = 'Your Password Has Been Sucessfully Updated.'
redirect_to authenticated_root_path
else
flash[:error] = 'Oh No! Something Went Wrong, Please Try Again.'
render :edit
end
elsif current_user.type == "SupportUser"
if current_user.update_with_password(user_params.merge(pw_changed: "true"))
flash[:notice] = 'Your Password Has Been Sucessfully Updated.'
redirect_to authenticated_root_path
else
flash[:error] = 'Oh No! Something Went Wrong, Please Try Again.'
render :edit
end
end
end
private
def user_params
if current_user.type == "User"
params.require(:user).permit(:current_password, :password, :password_confirmation)
elsif current_user.type == "AdminUser"
params.require(:admin_user).permit(:current_password, :password, :password_confirmation)
elsif current_user.type == "ClientUser"
params.require(:client_user).permit(:current_password, :password, :password_confirmation)
elsif current_user.type == "SupportUser"
params.require(:support_user).permit(:current_password, :password, :password_confirmation)
end
end
end

NoMethodError - undefined method `file' for on simple query

I just want to show a picture but it always throws the same error. The join is correct but he doesn't show the image instead he throws that error.
Within the /series_images/ show and index the image is displayed with
<%= image_tag(#series_image.file, class: "img-responsive",) %>
That's working without any problems.
Here is the Server
Started GET "/series/beyond-the-boundary" for 127.0.0.1 at 2016-01-06 01:44:03 +0100
Processing by SeriesController#show as HTML
Parameters: {"id"=>"beyond-the-boundary"}
Series Load (0.3ms) SELECT "series".* FROM "series" WHERE "series"."slug" = $1 ORDER BY "series"."id" ASC LIMIT 1 [["slug", "beyond-the-boundary"]]
SeriesImage Load (0.3ms) SELECT "series_images".* FROM "series_images" WHERE "series_images"."series_id" = $1 [["series_id", 1]]
Rendered series/show.html.erb within layouts/application (3.4ms)
Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.5ms)
NoMethodError - undefined method `file' for #<SeriesImage::ActiveRecord_Associations_CollectionProxy:0x007fc0e70be6e0>:
activerecord (4.2.5) lib/active_record/relation/delegation.rb:136:in `method_missing'
activerecord (4.2.5) lib/active_record/relation/delegation.rb:99:in `method_missing'
app/views/series/show.html.erb:2:in `_app_views_series_show_html_erb__1653679829410534096_70233272992260'
actionview (4.2.5) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.5) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.5) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.5) lib/action_view/template.rb:143:in `render'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.5) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.5) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.5) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.5) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.5) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.5) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.5) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'
warden (1.2.4) lib/warden/manager.rb:35:in `block in call'
warden (1.2.4) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Started POST "/__better_errors/a376ff2bf7c7dd19/variables" for 127.0.0.1 at 2016-01-06 01:44:03 +0100
The call happen within the series/show.html.erb
<%= image_tag(#series.series_image.file, class: "img-responsive",) %>
<p>
<strong>Name:</strong>
<%= #series.name %>
</p>
Series Controller
class SeriesController < ApplicationController
before_action :set_series, only: [:show, :edit, :update, :destroy]
# GET /series
# GET /series.json
def index
#series = Series.paginate(:page => params[:page], :per_page => 10)
end
# GET /series/1
# GET /series/1.json
def show
end
# GET /series/new
def new
#series = Series.new
end
# GET /series/1/edit
def edit
end
# POST /series
# POST /series.json
def create
#series = Series.new(series_params)
respond_to do |format|
if #series.save
format.html { redirect_to #series, notice: 'Series was successfully created.' }
format.json { render :show, status: :created, location: #series }
else
format.html { render :new }
format.json { render json: #series.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /series/1
# PATCH/PUT /series/1.json
def update
respond_to do |format|
if #series.update(series_params)
format.html { redirect_to #series, notice: 'Series was successfully updated.' }
format.json { render :show, status: :ok, location: #series }
else
format.html { render :edit }
format.json { render json: #series.errors, status: :unprocessable_entity }
end
end
end
# DELETE /series/1
# DELETE /series/1.json
def destroy
#series.destroy
respond_to do |format|
format.html { redirect_to series_index_url, notice: 'Series was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_series
#series = Series.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def series_params
params.require(:series).permit(:name, :tagline, :shortDESC, :longDESC, :rating, :release)
end
end
SeriesImage Controller
class SeriesImagesController < ApplicationController
before_action :set_series_image, only: [:show, :edit, :update, :destroy]
# GET /series_images
# GET /series_images.json
def index
#series_images = SeriesImage.paginate(:page => params[:page], :per_page => 5)
end
# GET /series_images/1
# GET /series_images/1.json
def show
end
# GET /series_images/new
def new
#series_image = SeriesImage.new
end
# GET /series_images/1/edit
def edit
end
# POST /series_images
# POST /series_images.json
def create
#series_image = SeriesImage.new(series_image_params)
respond_to do |format|
if #series_image.save
format.html { redirect_to #series_image, notice: 'Series image was successfully created.' }
format.json { render :show, status: :created, location: #series_image }
else
format.html { render :new }
format.json { render json: #series_image.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /series_images/1
# PATCH/PUT /series_images/1.json
def update
respond_to do |format|
if #series_image.update(series_image_params)
format.html { redirect_to #series_image, notice: 'Series image was successfully updated.' }
format.json { render :show, status: :ok, location: #series_image }
else
format.html { render :edit }
format.json { render json: #series_image.errors, status: :unprocessable_entity }
end
end
end
# DELETE /series_images/1
# DELETE /series_images/1.json
def destroy
#series_image.destroy
respond_to do |format|
format.html { redirect_to series_images_url, notice: 'Series image was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_series_image
#series_image = SeriesImage.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def series_image_params
params.require(:series_image).permit(:series_id, :file, :desc, :head, :headID, :home, :homeID)
end
end
The 2 Models
class Series < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
validates :name, presence: true
has_many :series_images
end
class SeriesImage < ActiveRecord::Base
mount_uploader :file, PictureUploader
belongs_to :series
end
The gemfile
#better IDs
gem 'friendly_id', '~> 5.1'
#Bootstrap
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.6'
#Image Upload
gem 'carrierwave', '~> 0.10.0'
#Bootswatch
gem 'bootswatch-rails', '~> 3.3', '>= 3.3.5'
# Devise User Management
gem 'devise', '~> 3.5', '>= 3.5.3'
#Pagination
gem 'will_paginate', '~> 3.1'
gem 'will_paginate-bootstrap', '~> 1.0', '>= 1.0.1'
The Migration of Series_Images
class CreateSeriesImages < ActiveRecord::Migration
def change
create_table :series_images do |t|
t.belongs_to :series, index:true
t.string :file
t.string :desc
t.boolean :head
t.integer :headID
t.boolean :home
t.integer :homeID
t.timestamps null: false
end
end
end
The problem is that you have a one to many relationship between Series and SeriesImages. So when you call #series.series_image in the view you are getting an active record collection back instead of a specific image. You could try doing #series.series_image.first.try(:file) and that would give you the first series_image if it exist. If this fails it will return nil.
Is a Series supposed to have multiple SeriesImages?

Difficulty allocating dynamic URLs to pages from database

I have a large number of mostly static pages which I've put into a database. I'm trying to retrieve these and dynamically generate a URL (similar to this: http://railscasts.com/episodes/117-semi-static-pages-revised. My model:
class Page < ActiveRecord::Base
validates_uniqueness_of :permalink
def to_param
permalink
end
end
Pages Controller:
class PagesController < ApplicationController
before_action :set_page, only: [:show, :edit, :update, :destroy]
# GET /pages
# GET /pages.json
def index
#pages = Page.all
end
# GET /pages/1
# GET /pages/1.json
def show
#page = Page.find_by_permalink!(params[:id])
end
# GET /pages/new
def new
#page = Page.new
end
# GET /pages/1/edit
def edit
#page = Page.find_by_permalink!(params[:id])
end
# POST /pages
# POST /pages.json
def create
#page = Page.new(page_params)
respond_to do |format|
if #page.save
format.html { redirect_to #page, notice: 'Page was successfully created.' }
format.json { render :show, status: :created, location: #page }
else
format.html { render :new }
format.json { render json: #page.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pages/1
# PATCH/PUT /pages/1.json
def update
respond_to do |format|
if #page.update(page_params)
format.html { redirect_to #page, notice: 'Page was successfully updated.' }
format.json { render :show, status: :ok, location: #page }
else
format.html { render :edit }
format.json { render json: #page.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pages/1
# DELETE /pages/1.json
def destroy
#page.destroy
respond_to do |format|
format.html { redirect_to pages_url, notice: 'Page was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_page
#page = Page.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def page_params
params.require(:page).permit(:name, :permalink, :content)
end
end
Routes:
resources :pages, except: :show
resources :products
root to: 'products#index'
get ':id', to: 'pages#show', as: :page
I know there have been some updates on the way that rails generates scaffolds but I just can;t work out what needs to change to make this work? I'm getting errors thrown up both the routes, but was previously getting an error with #page = Page.find_by_permalink!(params[:id])as well. It was saying that the method was missing but I couldn't see how. I also know I could use something like refinerycms but I really want to try and learn this stuff so any help much appreciated!
ERRORS FROM ROUTES:
Invalid route name, already in use: 'page' You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
FULL TRACE:
ctionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:430:in `add_route'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1489:in `add_route'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1466:in `decomposed_match'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1447:in `block in match'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1437:in `each'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:1437:in `match'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:630:in `map_method'
actionpack (4.1.5) lib/action_dispatch/routing/mapper.rb:591:in `get'
config/routes.rb:5:in `block in <top (required)>'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:337:in `instance_exec'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:337:in `eval_block'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:315:in `draw'
config/routes.rb:1:in `<top (required)>'
activesupport (4.1.5) lib/active_support/dependencies.rb:241:in `load'
activesupport (4.1.5) lib/active_support/dependencies.rb:241:in `block in load'
activesupport (4.1.5) lib/active_support/dependencies.rb:232:in `load_dependency'
activesupport (4.1.5) lib/active_support/dependencies.rb:241:in `load'
railties (4.1.5) lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
railties (4.1.5) lib/rails/application/routes_reloader.rb:40:in `each'
railties (4.1.5) lib/rails/application/routes_reloader.rb:40:in `load_paths'
railties (4.1.5) lib/rails/application/routes_reloader.rb:16:in `reload!'
railties (4.1.5) lib/rails/application/routes_reloader.rb:26:in `block in updater'
activesupport (4.1.5) lib/active_support/file_update_checker.rb:75:in `call'
activesupport (4.1.5) lib/active_support/file_update_checker.rb:75:in `execute'
railties (4.1.5) lib/rails/application/routes_reloader.rb:7:in `execute'
railties (4.1.5) lib/rails/application/finisher.rb:83:in `block (2 levels) in <module:Finisher>'
activesupport (4.1.5) lib/active_support/callbacks.rb:438:in `instance_exec'
activesupport (4.1.5) lib/active_support/callbacks.rb:438:in `block in make_lambda'
activesupport (4.1.5) lib/active_support/callbacks.rb:184:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:184:in `block in simple'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:185:in `block in simple'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:83:in `prepare!'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:71:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.5) lib/rails/engine.rb:514:in `call'
railties (4.1.5) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
This error occurred while loading the following files:
/home/ubuntu/workspace/traditional_cleaning/config/routes.rb

Resources