I migrated my devise version from 1.4.9 to 4.2.0. In the sessions/new form, I am also using passwords/new in a modal. I am facing error when I run the sessions new form. Please help:
This is the error stack:
ArgumentError - wrong number of arguments (given 3, expected 0..1):
bartt-ssl_requirement (1.4.2) lib/url_for.rb:9:in
url_for_with_secure_option' actionpack (4.2.6)
lib/action_dispatch/routing/route_set.rb:280:incall' actionpack
(4.2.6) lib/action_dispatch/routing/route_set.rb:223:in call'
actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:345:in
block (2 levels) in define_url_helper' actionpack (4.2.6)
lib/action_dispatch/routing/routes_proxy.rb:32:in user_password_path'
devise (4.2.0) lib/devise/controllers/url_helpers.rb:51:inblock (4
levels) in generate_helpers!' app/views/sessions/new.html.erb:45:in
_app_views_sessions_new_html_erb__3241459270597214406_43798180'
actionview (4.2.6) lib/action_view/template.rb:145:inblock in
render' activesupport (4.2.6)
lib/active_support/notifications.rb:166:in instrument' actionview
(4.2.6) lib/action_view/template.rb:333:ininstrument' actionview
(4.2.6) lib/action_view/template.rb:143:in render' actionview
(4.2.6) lib/action_view/renderer/template_renderer.rb:54:inblock (2
levels) in render_template' actionview (4.2.6)
lib/action_view/renderer/abstract_renderer.rb:39:in block in
instrument' activesupport (4.2.6)
lib/active_support/notifications.rb:164:inblock in instrument'
activesupport (4.2.6)
lib/active_support/notifications/instrumenter.rb:20:in instrument'
activesupport (4.2.6) lib/active_support/notifications.rb:164:in
instrument' actionview (4.2.6)
lib/action_view/renderer/abstract_renderer.rb:39:in instrument'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:53:in
block in render_template' actionview (4.2.6)
lib/action_view/renderer/template_renderer.rb:61:in
render_with_layout' actionview (4.2.6)
lib/action_view/renderer/template_renderer.rb:52:inrender_template'
actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:14:in
render' actionview (4.2.6)
lib/action_view/renderer/renderer.rb:46:inrender_template'
actionview (4.2.6) lib/action_view/renderer/renderer.rb:27:in render'
actionview (4.2.6) lib/action_view/rendering.rb:100:in
_render_template' actionpack (4.2.6)
lib/action_controller/metal/streaming.rb:217:in _render_template'
actionview (4.2.6) lib/action_view/rendering.rb:83:inrender_to_body'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:32:in
render_to_body' actionpack (4.2.6)
lib/action_controller/metal/renderers.rb:37:inrender_to_body'
actionpack (4.2.6) lib/abstract_controller/rendering.rb:25:in render'
actionpack (4.2.6) lib/action_controller/metal/rendering.rb:16:in
render' actionpack (4.2.6)
lib/action_controller/metal/instrumentation.rb:44:in block (2 levels)
in render' activesupport (4.2.6)
lib/active_support/core_ext/benchmark.rb:12:inblock in ms'
/home/osiuser/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in
realtime' activesupport (4.2.6)
lib/active_support/core_ext/benchmark.rb:12:inms' actionpack
(4.2.6) lib/action_controller/metal/instrumentation.rb:44:in block in
render' actionpack (4.2.6)
lib/action_controller/metal/instrumentation.rb:87:in
cleanup_view_runtime' activerecord (4.2.6)
lib/active_record/railties/controller_runtime.rb:25:in
cleanup_view_runtime' actionpack (4.2.6)
lib/action_controller/metal/instrumentation.rb:43:inrender'
remotipart (1.2.1) lib/remotipart/render_overrides.rb:14:in
render_with_remotipart' responders (2.2.0)
lib/action_controller/responder.rb:238:indefault_render'
responders (2.2.0) lib/action_controller/responder.rb:170:in to_html'
responders (2.2.0) lib/action_controller/responder.rb:163:inrespond'
responders (2.2.0) lib/action_controller/responder.rb:156:in call'
responders (2.2.0) lib/action_controller/respond_with.rb:205:in
respond_with' devise (4.2.0)
app/controllers/devise/sessions_controller.rb:12:in `new'
These are the lines I am facing error:
sessions/new (from line 45):
<%= semantic_form_for(resource, :as => resource_name, :url => password_path(resource_name), :remote => true, :format => :json, :html => { :id => 'password_reset' }) do |f| %>
<%= f.inputs do %>
<%= f.input :email, :label => 'Your email address', :input_html => { :placeholder => "Enter your email..."}%>
<% end %>
<%= f.buttons do %>
<%= f.commit_button :label => 'Send me that link', :button_html => {:class => 'submit button'}%>
<% end %>
<% end %>
devise/session_controller:
def new
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
yield resource if block_given?
respond_with(resource, serialize_options(resource)) #this is the line it's showing error.
end
In my sessions controller, there is no new method, only create and redirect methods and I am not having passwords controller.
This is my routes:
devise_for :users, :controllers => {:sessions => 'sessions'}, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout" }
It is working in older version,so I didn't changed the routes.
Please help
Related
I'm following this tutorial (authentication section) and I encountered this error when trying to access localhost:3000/authors/new
views/authors/_form.html.erb where line #2 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #2):
<%= form_with(model: author, local: true) do |form| %>
<% if author.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(author.errors.count, "error") %> prohibited this author from being saved:</h2>
<ul>
The error appeared when I added the before_filter and zero_authors_or_authenticated method in my authors_controller file.
class AuthorsController < ApplicationController
before_action :set_author, only: [:show, :edit, :update, :destroy]
before_filter :zero_authors_or_authenticated, only: [:new, :create]
def zero_authors_or_authenticated
unless Author.count == 0 || current_user
redirect_to root_path
return false
end
end
# GET /authors
# GET /authors.json
def index
#authors = Author.all
end
# GET /authors/1
# GET /authors/1.json
def show
end
# GET /authors/new
def new
#author = Author.new
end
# GET /authors/1/edit
def edit
end
# POST /authors
# POST /authors.json
def create
#author = Author.new(author_params)
respond_to do |format|
if #author.save
format.html { redirect_to #author, notice: 'Author was successfully created.' }
format.json { render :show, status: :created, location: #author }
else
format.html { render :new }
format.json { render json: #author.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /authors/1
# PATCH/PUT /authors/1.json
def update
respond_to do |format|
if #author.update(author_params)
format.html { redirect_to #author, notice: 'Author was successfully updated.' }
format.json { render :show, status: :ok, location: #author }
else
format.html { render :edit }
format.json { render json: #author.errors, status: :unprocessable_entity }
end
end
end
# DELETE /authors/1
# DELETE /authors/1.json
def destroy
#author.destroy
respond_to do |format|
format.html { redirect_to authors_url, notice: 'Author was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_author
#author = Author.find(params[:id])
end
# Only allow a list of trusted parameters through.
def author_params
params.require(:author).permit(:username, :email, :password, :password_confirmation)
end
end
Here is the whole views/authors/_form.html.erb if needed
<%= form_with(model: author, local: true) do |form| %>
<% if author.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(author.errors.count, "error") %> prohibited this author from being saved:</h2>
<ul>
<% author.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :username %>
<%= form.text_field :username %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :password %>
<%= form.text_field :password %>
</div>
<div class="field">
<%= form.label :password_confirmation %>
<%= form.text_field :password_confirmation %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
I tried moving the before_filter part further down but then it just does nothing and I looked through for any spelling errors as far as I can tell.
authors/new.html.erb
<h1>New Author</h1>
<%= render 'form', author: #author %>
<%= link_to 'Back', authors_path %>
Full trace from the error message
actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (6.0.2.1) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (6.0.2.1) lib/action_view/helpers/form_helper.rb:755:in `form_with'
app/views/authors/_form.html.erb:1
actionview (6.0.2.1) lib/action_view/base.rb:274:in `_run'
actionview (6.0.2.1) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.2.1) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.2.1) lib/action_view/template.rb:386:in `instrument_render_template'
actionview (6.0.2.1) lib/action_view/template.rb:183:in `render'
actionview (6.0.2.1) lib/action_view/renderer/partial_renderer.rb:358:in `block in render_partial'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:89:in `block in instrument'
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:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:88:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/partial_renderer.rb:347:in `render_partial'
actionview (6.0.2.1) lib/action_view/renderer/partial_renderer.rb:317:in `render'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:65:in `render_partial_to_object'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:53:in `render_partial'
actionview (6.0.2.1) lib/action_view/helpers/rendering_helper.rb:38:in `render'
app/views/authors/new.html.erb:3
actionview (6.0.2.1) lib/action_view/base.rb:274:in `_run'
actionview (6.0.2.1) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.2.1) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.2.1) lib/action_view/template.rb:386:in `instrument_render_template'
actionview (6.0.2.1) lib/action_view/template.rb:183:in `render'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:59:in `block (2 levels) in render_template'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:89:in `block in instrument'
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:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:88:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:58:in `block in render_template'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:66:in `render_with_layout'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:57:in `render_template'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:13:in `render'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.0.2.1) lib/action_view/rendering.rb:118:in `block in _render_template'
actionview (6.0.2.1) lib/action_view/base.rb:304:in `in_rendering_context'
actionview (6.0.2.1) lib/action_view/rendering.rb:117:in `_render_template'
actionpack (6.0.2.1) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionview (6.0.2.1) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.0.2.1) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.0.2.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/home/xxx/.rbenv/versions/2.6.4/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:85:in `cleanup_view_runtime'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
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:in `process_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:in `block 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:in `process_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:in `block 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:in `instrument'
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:in `process_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:in `process_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:in `process'
actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in `dispatch'
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:in `serve'
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:in `each'
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:in `call'
rack (2.2.2) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.2) lib/rack/etag.rb:27:in `call'
rack (2.2.2) lib/rack/conditional_get.rb:27:in `call'
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:in `call'
rack (2.2.2) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.2) lib/rack/session/abstract/id.rb:260:in `call'
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:in `call'
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:in `run_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:in `call'
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:in `call'
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:in `block 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:in `call'
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:in `call_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:in `block 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:in `tagged'
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:in `call'
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:in `call'
rack (2.2.2) lib/rack/method_override.rb:24:in `call'
rack (2.2.2) lib/rack/runtime.rb:22:in `call'
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:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.2) lib/rack/sendfile.rb:110:in `call'
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:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.2.1) lib/rails/engine.rb:526:in `call'
puma (4.3.1) lib/puma/configuration.rb:228:in `call'
puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
puma (4.3.1) lib/puma/server.rb:472:in `process_client'
puma (4.3.1) lib/puma/server.rb:328:in `block in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
The solution was to change before_filter to before_action, in case anyone runs into this too.
I'm trying to link associate blog posts with categories through the following simple_form where the label_method: :nameand value_method: :id are both pointing to the blog_categories table values:
<div class="container">
<div class="row">
<div class="col-xs-12">
<%= render 'shared/exitBlocker' %>
<h1 class="page-section">New Blog Post</h1>
<div class="wellington center news-form">
<%= simple_form_for #post, url: posts_path do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :title, input_html: { maxlength: 51 }, class: "form-control center" %>
<%= f.input :summary, input_html: { maxlength: 300 }, class: "form-control center" %>
<%= f.input :category_id, collection: #categories, as: :grouped_select, label_method: :name, value_method: :id , input_html: { class: "form-control center" } %>
<%= f.input :thumbnail_link, class: "form-control center" %>
<%= f.input :body, :as => :ckeditor, input_html: {:ckeditor => {:toolbar => 'FULL'}}, class: "form-control left" %>
<%= f.submit "Post", id: "saveChange", class: "btn btn-success btn-block" %>
<% end %>
</div>
</div>
</div><!-- Content row -->
</div><!-- Content container -->
The form works fine without applying the blog categories, but it raises an error when I load the posts/new page: nil is not a symbol nor a string
BlogCategory has_many :posts | Post belongs_to :blog_category
My posts and blog_categories tables:
create_table "posts", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.text "body"
t.string "summary"
t.string "thumbnail_link"
t.integer "category_id"
end
create_table "blog_categories", force: :cascade do |t|
t.string "name"
t.integer "parent_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Full trace:
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:458:in `block in option_groups_from_collection_for_select'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:456:in `map'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:456:in `option_groups_from_collection_for_select'
actionview (5.1.5) lib/action_view/helpers/tags/grouped_collection_select.rb:23:in `render'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:258:in `grouped_collection_select'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:841:in `grouped_collection_select'
simple_form (3.5.1) lib/simple_form/inputs/grouped_collection_select_input.rb:10:in `input'
simple_form (3.5.1) lib/simple_form/wrappers/leaf.rb:20:in `call'
simple_form (3.5.1) lib/simple_form/wrappers/leaf.rb:20:in `render'
simple_form (3.5.1) lib/simple_form/wrappers/many.rb:29:in `block in render'
simple_form (3.5.1) lib/simple_form/wrappers/many.rb:27:in `each'
simple_form (3.5.1) lib/simple_form/wrappers/many.rb:27:in `render'
simple_form (3.5.1) lib/simple_form/wrappers/root.rb:16:in `render'
simple_form (3.5.1) lib/simple_form/form_builder.rb:123:in `input'
app/views/posts/new.html.erb:11:in `block in _app_views_posts_new_html_erb__847991209_125594352'
actionview (5.1.5) lib/action_view/helpers/capture_helper.rb:39:in `block in capture'
actionview (5.1.5) lib/action_view/helpers/capture_helper.rb:203:in `with_output_buffer'
actionview (5.1.5) lib/action_view/helpers/capture_helper.rb:39:in `capture'
actionview (5.1.5) lib/action_view/helpers/form_helper.rb:450:in `form_for'
simple_form (3.5.1) lib/simple_form/action_view_extensions/form_helper.rb:27:in `block in simple_form_for'
simple_form (3.5.1) lib/simple_form/action_view_extensions/form_helper.rb:46:in `with_simple_form_field_error_proc'
simple_form (3.5.1) lib/simple_form/action_view_extensions/form_helper.rb:26:in `simple_form_for'
app/views/posts/new.html.erb:7:in `_app_views_posts_new_html_erb__847991209_125594352'
actionview (5.1.5) lib/action_view/template.rb:157:in `block in render'
activesupport (5.1.5) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.1.5) lib/action_view/template.rb:352:in `instrument_render_template'
actionview (5.1.5) lib/action_view/template.rb:155:in `render'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:52:in `block (2 levels) in render_template'
actionview (5.1.5) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.1.5) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:51:in `block in render_template'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:59:in `render_with_layout'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:50:in `render_template'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (5.1.5) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (5.1.5) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (5.1.5) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.1.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (5.1.5) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.1.5) lib/action_controller/metal/renderers.rb:141:in `render_to_body'
actionpack (5.1.5) lib/abstract_controller/rendering.rb:24:in `render'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (5.1.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
activesupport (5.1.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:29:in `cleanup_view_runtime'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:43:in `render'
meta-tags (2.8.0) lib/meta_tags/controller_helper.rb:20:in `render'
actionpack (5.1.5) lib/action_controller/metal/implicit_render.rb:33:in `default_render'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
The way I'm trying to populate the selector dropdown via Posts_controller's new method: #categories = BlogCategory.all
I'm not quite sure if I am doing this properly. If so, what is the best approach to this?
The error is in this line:
<%= f.input :category_id, collection: #categories, as: :grouped_select, label_method: :name, value_method: :id , input_html: { class: "form-control center" } %>
The following solutions might work for you. Replace the line with any of the solutions:
<%= f.association :category %>
<%= f.input :category_id, collection: BlogCategory.all, value_method: :id, label_method: :name %>
I'm building an app that features workouts models, each workout has_many exercises, and each exercise has_many reports.
My routes are as follows:
workouts GET /workouts(.:format) workouts#index
POST /workouts(.:format) workouts#create
new_workout GET /workouts/new(.:format) workouts#new
edit_workout GET /workouts/:id/edit(.:format) workouts#edit
workout GET /workouts/:id(.:format) workouts#show
PATCH /workouts/:id(.:format) workouts#update
PUT /workouts/:id(.:format) workouts#update
DELETE /workouts/:id(.:format) workouts#destroy
exercises GET /exercises(.:format) exercises#index
POST /exercises(.:format) exercises#create
new_exercise GET /exercises/new(.:format) exercises#new
edit_exercise GET /exercises/:id/edit(.:format) exercises#edit
exercise GET /exercises/:id(.:format) exercises#show
PATCH /exercises/:id(.:format) exercises#update
PUT /exercises/:id(.:format) exercises#update
DELETE /exercises/:id(.:format) exercises#destroy
reports POST /reports(.:format) reports#create
report DELETE /reports/:id(.:format) reports#destroy
I toyed with nesting the routes, but it caused more problems than it solved, so they remain like this:
resources :workouts
resources :exercises
resources :reports, only: [:create, :destroy]
On my workouts#show page I have a link to add exercises to that workout:
<%= link_to 'Add/Edit Exercises', exercises_path(#workout) %>
When I click the link, I get the following error:
undefined method `id' for nil:NilClass
When I click the link, it SHOULD go to my exercises#index page:
<h1>Current Exercises:</h1>
<% #exercises.each do |exercise| %>
<p><%= exercise.name %> (<%= link_to "Delete #{exercise.name}", exercise_path(exercise), method: :delete, data: { confirm: 'Are you sure?' } %>)</p>
<% end %>
<h1>Add New Exercises:</h1>
<%= render 'exercises/form' %>
It is on the exercises/_form.html.erb that the error is being called, on the indicated line:
<div class="row">
<div class="col-xs-10 col-xs-push-1">
<%= form_for #exercise,
:url => { :controller => "exercises",
:action => :create,
:workout_id => #workout.id } do |f| %> <!-- error called on this line -->
<div class="form-group">
<%= f.label :name, class: 'sr-only' %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter exercise name" %>
</div>
<div class="form-group col-xs-4">
<p><%= f.label :needs_seconds, class: 'sr-only' %>
<%= f.check_box :needs_seconds, class: 'check_box' %> Report seconds?</p>
</div>
<div class="form-group col-xs-4">
<p><%= f.label :needs_reps, class: 'sr-only' %>
<%= f.check_box :needs_reps, class: 'check_box' %> Report reps?</p>
</div>
<div class="form-group col-xs-4">
<p><%= f.label :needs_weight, class: 'sr-only' %>
<%= f.check_box :needs_weight, class: 'check_box' %> Report weight?</p>
</div>
<div class="text-center"><%= f.submit "Create Exercise", class: 'btn btn-primary' %></div>
<% end %>
</div>
</div>
Any insight as to what is causing this error? Here's my workouts controller:
class WorkoutsController < ApplicationController
def index
#workouts = Workout.all
end
def show
#workout = Workout.friendly.find(params[:id])
#exercise = Exercise.new
#report = Report.new
end
def new
#workout = Workout.new
#workout.user_id = current_user
end
def create
#workout = Workout.new(workout_params)
#workout.user = current_user
if #workout.save
flash[:notice] = "Workout was saved successfully."
redirect_to #workout
else
flash.now[:alert] = "Error creating workout. Please try again."
render :new
end
end
def edit
#workout = Workout.friendly.find(params[:id])
#workout.user_id = current_user
end
def update
#workout = Workout.friendly.find(params[:id])
#workout.name = params[:workout][:name]
#workout.workout_type = params[:workout][:workout_type]
#workout.teaser = params[:workout][:teaser]
#workout.description = params[:workout][:description]
#workout.video = params[:workout][:video]
#workout.difficulty = params[:workout][:difficulty]
#workout.trainer = params[:workout][:trainer]
#workout.user_id = params[:workout][:user_id]
if #workout.save
flash[:notice] = "Workout was updated successfully."
redirect_to #workout
else
flash.now[:alert] = "Error saving workout. Please try again."
render :edit
end
end
def destroy
#workout = Workout.friendly.find(params[:id])
if #workout.destroy
flash[:notice] = "\"#{#workout.name}\" was deleted successfully."
redirect_to action: :index
else
flash.now[:alert] = "There was an error deleting the workout."
render :show
end
end
private
def workout_params
params.require(:workout).permit(:name, :workout_type, :teaser, :description, :video, :difficulty, :trainer, :user_id)
end
end
And here's the exercises controller:
class ExercisesController < ApplicationController
before_action :authenticate_user!
def index
#exercises = Exercise.all
end
def new
#exercise = Exercise.new
end
def create
#workout = Workout.friendly.find(params[:workout_id])
exercise = #workout.exercises.new(exercise_params)
exercise.user = current_user
if exercise.save
flash[:notice] = "Results saved successfully."
redirect_to [#workout]
else
flash[:alert] = "Results failed to save."
redirect_to [#workout]
end
end
def destroy
#workout = Workout.friendly.find(params[:workout_id])
exercise = #workout.exercises.find(params[:id])
if exercise.destroy
flash[:notice] = "Exercise was deleted successfully."
redirect_to [#workout]
else
flash[:alert] = "Exercise couldn't be deleted. Try again."
redirect_to [#workout]
end
end
private
def exercise_params
params.require(:exercise).permit(:name, :needs_seconds, :needs_weight, :needs_reps)
end
def authorize_user
exercise = Exercise.find(params[:id])
unless current_user == current_user.admin?
flash[:alert] = "You do not have permission to create or delete an exercise."
redirect_to [exercise.workout]
end
end
end
And here's the server log:
Started GET "/exercises.abs-0002" for ::1 at 2016-06-01 13:10:33 -0700
Processing by ExercisesController#index as
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Workout Load (0.1ms) SELECT "workouts".* FROM "workouts" WHERE "workouts"."id" IS NULL LIMIT 1
Exercise Load (0.1ms) SELECT "exercises".* FROM "exercises"
Rendered exercises/_form.html.erb (9.6ms)
Rendered exercises/index.html.erb within layouts/application (18.4ms)
Completed 500 Internal Server Error in 33ms (ActiveRecord: 0.3ms)
NoMethodError - undefined method `id' for nil:NilClass:
app/views/exercises/_form.html.erb:4:in `block in _app_views_exercises__form_html_erb___1550785662008061170_70245396037240'
actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.5) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.5) lib/action_view/helpers/form_helper.rb:444:in `form_for'
app/views/exercises/_form.html.erb:3:in `_app_views_exercises__form_html_erb___1550785662008061170_70245396037240'
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/partial_renderer.rb:339:in `render_partial'
actionview (4.2.5) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
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/partial_renderer.rb:309:in `render'
actionview (4.2.5) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.2.5) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/exercises/index.html.erb:7:in `_app_views_exercises_index_html_erb___2271140629366515446_70245347113980'
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/elizabethbayardelle/.rbenv/versions/2.2.4/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'
meta-tags (2.1.0) lib/meta_tags/controller_helper.rb:26:in `render_with_meta_tags'
remotipart (1.2.1) lib/remotipart/render_overrides.rb:14:in `render_with_remotipart'
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.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) 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'
remotipart (1.2.1) lib/remotipart/middleware.rb:27: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'
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'
You link_to in workouts#show can be like below:
<%= link_to 'Add/Edit Exercises', exercises_path(workout_id: #workout.id) %>
exercises_controller.rb
def index
#workout = Workout.find_by_id(params[:workout_id])
#exercise = Exercise.new
#exercises = Exercise.all
end
You can pass the workout_id in hidden field of exercises/_form.html.erb as follows:
<%= form_for #exercise do |f| %>
<%= f.hidden_field :workout_id, #workout.id %>
<% end %>
And, include workout_id to your strong param as follows:
def exercise_params
params.require(:exercise).permit(:name, :needs_seconds, :needs_weight, :needs_reps, :workout_id)
end
Within my application I am trying to create a nested resource (a step for a given goal) and I am having trouble with the creation at the url "/goals/1/steps/new". When i navigate to that page i get the error "First argument in form cannot contain nil or be empty"
I really cannot figure out why I am not able to create a step that it is, upon creation, assigned to the specific it was created for. Any help is greatly appreciated.
Below are my steps controller and my steps form
Steps Controller:
class StepsController < ApplicationController
#before_action :set_step, only: [:show, :edit, :update, :destroy]
before_filter :authorize
# GET /steps
# GET /steps.json
def index
#steps = Goal.find(params[:goal_id]).steps.all
end
# GET /goals/1/steps/new
def new
#step = Goal.find(params[:goal_id]).steps.new
end
# GET /steps/1
# GET /steps/1.json
def show
end
# GET /steps/1/edit
def edit
end
def create
#step = Goal.find(params[:goal_id]).steps.new(step_params)
respond_to do |format|
if #step.save
format.html { redirect_to #step, notice: 'Step was successfully created.' }
format.json { render :show, status: :created, location: #step }
else
format.html { render :new }
format.json { render json: #step.errors, status: :unprocessable_entity }
end
end
redirect_to(goal_steps_url(#goal))
end
def update
#step.update(step_params)
respond_to do |format|
if #step.update(step_params)
format.html { redirect_to #step, notice: 'Step was successfully updated.' }
format.json { render :show, status: :ok, location: #step }
else
format.html { render :edit }
format.json { render json: #step.errors, status: :unprocessable_entity }
end
end
end
# POST /steps
# POST /steps.json
def destroy
#step.destroy
respond_to do |format|
format.html { redirect_to steps_url, notice: 'Step was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_step
#step = Goal.find(params[:goal_id]).Step.find(params[:id])
end
def step_params
params.require(:step).permit(:requirement, :completionTime, :goal_id)
end
end
Steps form:
<%= form_for(#steps) do |f| %>
<% if #step.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#step.errors.count, "error") %> prohibited this step from being saved:</h2>
<ul>
<% #step.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :requirement %><br>
<%= f.text_field :requirement %>
</div>
<div class="field">
<%= f.label :completionTime %><br>
<%= f.number_field :completionTime %>
</div>
<div class="field">
<%= hidden_field_tag :goal_id, goal.id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes file:
Rails.application.routes.draw do
resources :goals do
resources :steps
end
resources :signups
get 'welcome/index'
resources :calendar
resources :todolist
resources :to_dos
root 'welcome#index'
# These routes will be for signup. The first renders a form in the browse, the second will
# receive the form and create a user in our database using the data given to us by the user.
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
get '/logout' => 'sessions#destroy'
get '/signup' => 'users#new'
post '/users' => 'users#create'
end
Stack Trace:
actionview (4.2.5.1) lib/action_view/helpers/form_helper.rb:432:in `form_for'
app/views/steps/_form.html.erb:1:in `_app_views_steps__form_html_erb__4323915558924223187_70260809990920'
actionview (4.2.5.1) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.5.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.5.1) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.5.1) lib/action_view/template.rb:143:in `render'
actionview (4.2.5.1) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
actionview (4.2.5.1) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
actionview (4.2.5.1) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.5.1) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.5.1) lib/action_view/renderer/partial_renderer.rb:309:in `render'
actionview (4.2.5.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.2.5.1) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/steps/new.html.erb:3:in `_app_views_steps_new_html_erb__538834525682328601_70260798181140'
actionview (4.2.5.1) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.5.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.5.1) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.5.1) lib/action_view/template.rb:143:in `render'
actionview (4.2.5.1) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.5.1) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.5.1) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.5.1) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.5.1) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.5.1) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.5.1) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.5.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.5.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.5.1) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.5.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.5.1) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.5.1) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.5.1) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.5.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.5.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.5.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (4.2.5.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.5.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.5.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.5.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.5.1) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.5.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.5.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.5.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.5.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.5.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.5.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.5.1) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.5.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.5.1) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.5.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.5.1) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:815: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.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.5.1) 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.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.5.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.5.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.5.1) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.5.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.5.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.5.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.5.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.5.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.5.1) 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 `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.5.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.5.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.5.1) 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.1) 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.1) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.5.1) lib/rails/engine.rb:518:in `call'
railties (4.2.5.1) 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'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
In Controller:-
def new
#goal = Goal.find(params[:goal_id])
#step = #goal.steps.build
end
def edit
#goal = Goal.find(params[:goal_id])
end
def create
#goal = Goal.find(params[:goal_id])
#step = #goal.steps.new(step_params)
respond_to do |format|
if #step.save
format.html { redirect_to goal_steps_path(#goal), notice: 'Step was successfully created.' }
format.json { render :show, status: :created, location: #step }
else
format.html { render :new }
format.json { render json: #step.errors, status: :unprocessable_entity }
end
end
redirect_to(goal_steps_url(#goal))
end
def update
#goal = Goal.find(params[:goal_id])
#step.update(step_params)
respond_to do |format|
if #step.update(step_params)
format.html { redirect_to goal_steps_path(#goal), notice: 'Step was successfully updated.' }
format.json { render :show, status: :ok, location: #step }
else
format.html { render :edit }
format.json { render json: #step.errors, status: :unprocessable_entity }
end
end
end
In the form:-
Replace the line
<%= form_for(#steps) do |f| %>
with
<%= form_for[#goal,#step] do |f| %>
First argument in form cannot contain nil or be empty
Firstly, the error is due to your form having #steps instead of #step and also you are creating a form for nested resources, so your form_for needs two arguments. The below changes should get you going
def new
#goal = Goal.find(params[:goal_id])
#step = #goal.steps.build
end
And in the form, change
<%= form_for(#steps) do |f| %>
to
<%= form_for [#goal, #step] do |f| %>
trying to create a videogame review website for practice. ran into this problem of missing key / no route matches.
below are my Games, and Reviews contrller. Review is nested inside Games.
Many thanks in advance :)
edit:
the error is:
No route matches {:action=>"update", :controller=>"reviews", :game_id=>3, :id=>nil} missing required keys: [:id]
full error code below:
No route matches {:action=>"update", :controller=>"reviews", :game_id=>3, :id=>nil} missing required keys: [:id]
actionpack (4.2.1) lib/action_dispatch/journey/formatter.rb:46:in `generate'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:727:in `generate'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:758:in `generate'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:801:in `url_for'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:280:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:223:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:345:in `block (2 levels) in define_url_helper'
app/views/games/show.html.erb:22:in `block in _app_views_games_show_html_erb___707335474_75793908'
c:in `each'
c:in `each'
app/views/games/show.html.erb:20:in `_app_views_games_show_html_erb___707335474_75793908'
actionview (4.2.1) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.1) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.1) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.1) lib/action_view/template.rb:143:in `render'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.1) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.1) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.1) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.1) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.1) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.1) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_process_action_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.1) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.1) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.1) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.1) lib/action_dispatch/routing/route_set.rb:819:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) 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.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.1) 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.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
activerecord (4.2.1) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
activesupport (4.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.1.3) lib/web_console/middleware.rb:37:in `call'
actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.1) 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.1) 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.1) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.1) lib/rails/engine.rb:518:in `call'
railties (4.2.1) lib/rails/application.rb:164: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'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
c:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
** also having problems with the Show view. i think its the delete action
*** also can you take a look at my form partial for the review submission? i'm basically trying to render have the text area rendered in the Show view, then once submited, appear below the text area.
I can add reviews and save them, but once submitted, I get an error. i go to rails console, and see that the reviews are saved -- but just curious if my logic is any good.
Routes.rb
resources :games do
resources :reviews, except: [:show, :index]
end
Games_controller.rb
class GamesController < ApplicationController
before_action :set_game, only: [:show, :edit, :update, :destroy]
def index
#games = Game.all
end
def show
#reviews = Review.where(game_id: #game.id)
end
def create
#game = Game.new(game_params)
#game.save
redirect_to #game
end
def new
#game = Game.new
set_platforms
set_genres
end
def edit
#platforms = Platform.order(:system)
set_platforms
set_genres
end
def update
#game.update(game_params)
redirect_to #game
end
def destroy
#game.destroy
redirect_to action: :index
end
private
def game_params
params.require(:game).permit(:title, :image, :release_date, :genre_id, :platform_id)
end
def set_game
#game = Game.find(params[:id])
end
def set_platforms
#platforms = Platform.order(:system)
end
def set_genres
#genres = Genre.order(:category)
end
end
Reviews_controller.rb
class ReviewsController < ApplicationController
before_action :authenticate_user!, :set_review, only: [:show, :update, :edit, :destroy]
before_action :set_game
def index
end
def show
end
def new
#review = Review.new
end
def create
#review = Review.new(review_params)
#review.user_id = current_user.id
#review.game_id = #game.id
#review.save
end
def edit
#review.update(review.params)
end
def destroy
#review.destroy
redirect_to games_path
end
private
def review_params
params.require(:review).permit(:comment)
end
def set_review
#review = Review.find(params[:id])
end
def set_game
#game = Game.find(params[:game_id])
end
end
show.html.erb
<p><%= link_to "<< Home", games_path %></p>
<span><%= link_to "Edit", edit_game_path %></span>
<span><%= link_to "Delete", game_path(#game), method: :delete %></span>
<p><%= link_to "Add Steelbook", new_steelbook_path %></p>
<div class="game_summary">
<h2><%= #game.title %></h2>
<%= image_tag #game.image %>
<p>Release Date: <%= #game.release_date %> </p>
<p>Genre: <%= #game.genre_id %> </p>
<p>Platforms: <%= #game.platform_id %></p>
</div>
<div class="game_review submit">
<%= render "review" %>
</div>
<% #reviews.each do |review| %>
<p><%= review.comment %></p>
<p><%= link_to "delete comment", game_review_path(review.game_id), method: :delete %></p>
<p><%= review.game_id %></p>
<% end %>
Form partial
<%= form_for [#game, #reviews.new] do |r| %>
<h3>Review this game</h3>
<p>
<%= r.text_area :comment %>
</p>
<p>
<%= r.hidden_field :game_id, value: #game.id %>
<p>
<%= r.submit %>
<% end %>
I think the error is happening here: edit_game_path
Edit path requires a model id to work, so you need to pass it:
edit_game_path(#game)
You have to change this line in your show.html.erb
<%= link_to "delete comment", game_review_path(review.game_id), method: :delete %>
to
<%= link_to "delete comment", game_review_path(review, review.game_id), method: :delete %>