Rails ERB error - ruby-on-rails

I am making a CRUD blog application, and wrote this code:
<h1>New Article</h1>
<%= form_for :article , url:articles_path do|f| %>
<% if #article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize%>
</h2>
</div>
<% end %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br/>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit%>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
My page shows undefined method `errors' for nil:NilClass. What should I fix here?
The controller is:
class ArticlesController < ApplicationController
# new form method
def new
end
# create method
def create
#article = Article.new(params.require(:article).permit(:title,:text))
if #article.save
redirect_to #article
else
render 'new'
end
end
#Show method
def show
#article = Article.find(params[:id])
end
# New method
def new
end
#index method
def index
#articles = Article.all
end
end
And the full backtrace is here:
app/views/articles/new.html.erb:5:in `block in _app_views_articles_new_html_erb___837982527363082027_69849494310700'
actionview (4.2.0) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.0) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionview (4.2.0) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.0) lib/action_view/helpers/form_helper.rb:444:in `form_for'
app/views/articles/new.html.erb:3:in `_app_views_articles_new_html_erb___837982527363082027_69849494310700'
actionview (4.2.0) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:143:in `render'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
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'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/prio/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:5: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:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
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 `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 `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 `call'
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 `call'
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 `call'
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 `each'
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'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) 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.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) 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 `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'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.1.1) lib/web_console/middleware.rb:37: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'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) 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.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) 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.0) lib/rack/lock.rb:17:in `call'
rack (1.6.0) lib/rack/content_length.rb:15:in `call'
rack (1.6.0) lib/rack/handler/webrick.rb:89:in `service'
/home/prio/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/prio/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/prio/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

You need to define the method 'new'. This may work:
def new
#article = Article.new
end
And you have two 'new' methods. Erase one.

When you call new your variable #article is undefined. You don't need that code in your form. You should put that code in your show or what ever view you go to after creating the new article.
Edit: I see you are redirecting back to new if the save fails. At that point it would be defined. I would change the code to check if #article is defined first:
<% if #article.errors %>
this will handle the situation of #article being undefined because it hasn't been called yet.

Related

rails enum, load enum values based on condition is it possible?

lets say i have a model with enum like this :
class Apt < ActiveRecord::Base
enum apt_status: [ :draft, :publish, :unpublish, :waiting ]
end
and in my controller i load my enum like this :
#apt_statuses = Apt.apt_statuses
it's working fine, but what if i want to load my enum values based on user role?
so lets say if i am an admin i want to load all my enum values, and if i am not an admin, i only display 3 enum values
":draft, :unpublish, :waiting"
is it possible? please suggest. many thanks.
btw here is my controller, i really have no idea how to fill my
#apt_statuses
in else condition
ishaveprivilage = Usermaster.joins(:rolemasters, :rolemasters).where(id: #current_user.id, rolemasters: {name: "Super Admin"})
if ishaveprivilage
#apt_statuses = Apt.apt_statuses
else
end
i use my enum in view like this :
<%= f.select :apt_status, options_for_select(#apt_statuses.collect { |s| [s[0].humanize, s[0]] }, selected: #apt.apt_status), {} , class: "form-control" %>
error trace :
NoMethodError in Admin::Apts#new
Showing C:/Users/lenovo/Documents/urbanace/urbanacecode/app/views/admin/apts/_form.html.erb where line #134 raised:
undefined method `humanize' for :draft:Symbol
Trace of template inclusion: app/views/admin/apts/new.html.erb
Rails.root: C:/Users/lenovo/Documents/urbanace/urbanacecode
Application Trace | Framework Trace | Full Trace
app/views/admin/apts/_form.html.erb:134:in `block (2 levels) in _app_views_admin_apts__form_html_erb___8345113_90629556'
app/views/admin/apts/_form.html.erb:134:in `each'
app/views/admin/apts/_form.html.erb:134:in `collect'
app/views/admin/apts/_form.html.erb:134:in `block in _app_views_admin_apts__form_html_erb___8345113_90629556'
actionview (4.2.7) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.7) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.7) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.7) lib/action_view/helpers/form_helper.rb:444:in `form_for'
app/views/admin/apts/_form.html.erb:57:in `_app_views_admin_apts__form_html_erb___8345113_90629556'
actionview (4.2.7) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.7) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:143:in `render'
actionview (4.2.7) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
actionview (4.2.7) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.7) lib/action_view/renderer/partial_renderer.rb:309:in `render'
actionview (4.2.7) lib/action_view/renderer/renderer.rb:51:in `render_partial'
actionview (4.2.7) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/admin/apts/new.html.erb:2:in `_app_views_admin_apts_new_html_erb__809278863_96977256'
actionview (4.2.7) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.7) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:143:in `render'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.7) lib/action_view/renderer/renderer.rb:46:in `render_template'
actionview (4.2.7) lib/action_view/renderer/renderer.rb:27:in `render'
actionview (4.2.7) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.7) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.7) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.7) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.7) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.7) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.7) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.7) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.7) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.7) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:43:in `render'
remotipart (1.2.1) lib/remotipart/render_overrides.rb:14:in `render_with_remotipart'
actionpack (4.2.7) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.7) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.7) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.7) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.7) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.7) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.7) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.7) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.7) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.7) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.7) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.7) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.7) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.7) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.7) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.7) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.7) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:817: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.7) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.7) 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.7) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.7) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.7) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.7) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.7) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.7) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.7) 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.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.7) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.7) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.7) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.7) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.7) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.7) 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.7) 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.7) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.7) lib/rails/engine.rb:518:in `call'
railties (4.2.7) 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'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Request
Parameters:
None
Toggle session dump
_csrf_token: "DK6t1OuVBdMWZLihVgktZFIQSo7gX3qt5Iq5ZHyA7lM="
session_id: "2391dc6faaafcd52ee1815ccbe115daf"
user_id: 7
Toggle env dump
GATEWAY_INTERFACE: "CGI/1.1"
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
HTTP_ACCEPT_ENCODING: "gzip, deflate, sdch"
HTTP_ACCEPT_LANGUAGE: "en-US,en;q=0.8"
HTTP_CACHE_CONTROL: "max-age=0"
REMOTE_ADDR: "127.0.0.1"
REMOTE_HOST: "127.0.0.1"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"
Response
Headers:
None
x
>>
I think I would solve this like so:
class Apt < ActiveRecord::Base
enum status: [:draft, :publish, :unpublish, :waiting]
def self.user_specific_statuses(user)
if user.admin? # define a method in your user model which defines this.
Apt.statuses
else
Apt.statuses.reject { |k, v| k == "publish" } # => all except publish
end
end
end
And then you could call it like this:
Apt.user_specific_statuses(admin_user) # => all statuses
Apt.user_specific_statuses(normal_user) # => only draft, unpublish, waiting
i don't know is this an ideal way to do this or not but i solved it with this approach :
i changed my model enum definition to this :
model : enum apt_status: { draft:0, publish:1, unpublish:2, waiting:3 }
and in my controller i did this :
ishaveprivilage = Usermaster.joins(:rolemasters, :rolemasters).where(id: #current_user.id, rolemasters: {name: "Super Admin"})
if !ishaveprivilage.blank?
#apt_statuses = Apt.apt_statuses
else
temp = Apt.apt_statuses.reject { |k,v| v == 1 } #remove Publish option for non super admin
#apt_statuses = temp
end
if you guys have a better approach, please advise.

Nested attributes with ruby on rails

I'm developing a system to improve my Rails skills, which will allow anyone to create a user. This user will have an inventory, which will contain multiple items.
My question is how can I store all the resources when saving a new user. I'm trying to use cocoon gem, and I got this error message:
undefined method `new_record?' for nil:NilClass
at this part of the code:
<%= link_to_remove_association "remove item", f %>
Models
class User < ActiveRecord::Base
has_one :inventory
delegate :items, to: :inventory, prefix: true
accepts_nested_attributes_for :inventory
end
class Inventory < ActiveRecord::Base
has_many :inventory_items
has_many :items, through: :inventory_items
accepts_nested_attributes_for :inventory_items, reject_if: :all_blank, allow_destroy: true
end
class InventoryItem < ActiveRecord::Base
belongs_to :item
belongs_to :inventory
end
Controller
class UsersController < ApplicationController
def new
#user = User.new.tap do |user|
user.inventory = Inventory.new
user.inventory_items.build
end
end
private
def user_params
params
.require(:user)
.permit(
:name,
:lat,
:long,
:age,
inventory_attributes: [:id, inventory_items_attributes: [:quantity, :inventory_id, :item_id, :_destroy]]
)
end
end
User form
<%= simple_form_for(#user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :lat %>
<%= f.input :long %>
</div>
<h3>Items</h3>
<div id="inventory_items">
<%= f.simple_fields_for :inventory do |inventory| %>
<%= inventory.simple_fields_for :inventory_items do |f| %>
<%= render 'item_fields', f: f %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
item_fields partial
<div class="nested-fields">
<%= f.collection_select :item_id, Item.all, :id, :name %>
<%= f.input :quantity %>
<%= link_to_remove_association "remove item", f %>
</div>
The inventory does not have more attributes than its id and user_id
Stack trace
cocoon (1.2.9) lib/cocoon/view_helpers.rb:29:in `link_to_remove_association'
app/views/users/_inventory_item_fields.html.erb:6:in `_app_views_users__inventory_item_fields_html_erb__1201857070210368281_70056778972140'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:309:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.2.4) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/users/new.html.erb:12:in `block (3 levels) in _app_views_users_new_html_erb___3186868675472137201_70056772986280'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:714:in `fields_for'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:1599:in `fields_for'
simple_form (3.1.1) lib/simple_form/action_view_extensions/builder.rb:27:in `simple_fields_for'
app/views/users/new.html.erb:11:in `block (2 levels) in _app_views_users_new_html_erb___3186868675472137201_70056772986280'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:1925:in `block in fields_for_nested_model'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:714:in `fields_for'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:1924:in `fields_for_nested_model'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:1914:in `fields_for_with_nested_attributes'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:1582:in `fields_for'
simple_form (3.1.1) lib/simple_form/action_view_extensions/builder.rb:27:in `simple_fields_for'
app/views/users/new.html.erb:10:in `block in _app_views_users_new_html_erb___3186868675472137201_70056772986280'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:444:in `form_for'
simple_form (3.1.1) lib/simple_form/action_view_extensions/form_helper.rb:26:in `block in simple_form_for'
simple_form (3.1.1) lib/simple_form/action_view_extensions/form_helper.rb:45:in `with_simple_form_field_error_proc'
simple_form (3.1.1) lib/simple_form/action_view_extensions/form_helper.rb:25:in `simple_form_for'
app/views/users/new.html.erb:1:in `_app_views_users_new_html_erb___3186868675472137201_70056772986280'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.4) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.4) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.4) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/vinicius/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.4) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.4) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.4) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.4) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `call'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821: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.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.4) 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.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.4) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.4) 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.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.4) 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.4) 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.4) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) 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'

Shoppe and kaminari - undefined method `entry_name' for #<ActiveRecord::Relation []> for #orders

I've been working with the shoppe gem, and I'm currently completely stumped by an error I'm getting when I'm opening up the orders page of the admin panel.
I'm using version 1.0.7 because product variants broke on the subsequent ones. Everything was working completely fine until I merged with a different branch in my repo, and now when I try to access the orders page of the admin panel this happens:
ActionView::Template::Error (undefined method `entry_name' for #<ActiveRecord::Relation []>):
6: = link_to t('shoppe.orders.search_orders'), '#', :class => 'button', :rel => 'searchOrders'
7: %h2.orders
8: = t('shoppe.orders.orders')
9: %span= page_entries_info #orders
10:
11: = render 'search_form'
12:
activerecord (4.2.4) lib/active_record/relation/delegation.rb:136:in `method_missing'
activerecord (4.2.4) lib/active_record/relation/delegation.rb:99:in `method_missing'
kaminari (0.16.3) lib/kaminari/helpers/action_view_extension.rb:92:in `page_entries_info'
shoppe (1.0.7) app/views/shoppe/orders/index.html.haml:9:in `block in ___sers_aur______rvm_gems_ruby_______gems_shoppe_______app_views_shoppe_orders_index_html_haml__4424402014120534252_70189058436720'
haml (4.0.7) lib/haml/helpers.rb:368:in `call'
haml (4.0.7) lib/haml/helpers.rb:368:in `block in capture_haml'
haml (4.0.7) lib/haml/helpers.rb:608:in `with_haml_buffer'
haml (4.0.7) lib/haml/helpers.rb:364:in `capture_haml'
haml (4.0.7) lib/haml/helpers/xss_mods.rb:61:in `capture_haml_with_haml_xss'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:45:in `capture_with_haml'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:152:in `content_for'
shoppe (1.0.7) app/views/shoppe/orders/index.html.haml:3:in `___sers_aur______rvm_gems_ruby_______gems_shoppe_______app_views_shoppe_orders_index_html_haml__4424402014120534252_70189058436720'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.4) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.4) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.4) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/aur2103/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.4) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.4) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.4) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.4) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `call'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/railtie.rb:194:in `public_send'
railties (4.2.4) lib/rails/railtie.rb:194:in `method_missing'
actionpack (4.2.4) lib/action_dispatch/routing/mapper.rb:51:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
shoppe (1.0.7) lib/shoppe/settings_loader.rb:10:in `call'
nifty-attachments (1.0.4) lib/nifty/attachments/middleware.rb:23: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.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.4) 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.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.4) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.4) 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.4) 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.4) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) 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/aur2103/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/Users/aur2103/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/Users/aur2103/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
I know that this is happening in the page_entries_info method in kaminari. This one, specifically the second line within the method:
def page_entries_info(collection, options = {})
entry_name = options[:entry_name] || collection.entry_name
entry_name = entry_name.pluralize unless collection.total_count == 1
if collection.total_pages < 2
t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count)
else
first = collection.offset_value + 1
last = collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value
t('helpers.page_entries_info.more_pages.display_entries', :entry_name => entry_name, :first => first, :last => last, :total => collection.total_count)
end.html_safe
end
The strange thing is, I've put in some print statements in both the functioning version and the broken version, and in BOTH of them options[:entry_name] is nil and collection.inspect prints out #<ActiveRecord::Relation []>. However, only the newly merged version throws the error. Why does #<ActiveRecord::Relation []> not have this method in only one of the branches? What could be causing this?
Are you using will_paginate or bootstrap-will_paginate ?
Please see here: https://github.com/tryshoppe/shoppe/issues/189
I will also suggest the following:
1. Kill your rails server
2. type "spring stop"
3. bundle/restart rails
I had this same issue with entry_name without using will_paginate so the second fix worked for me.
Explicitly declaring entry_name in the line that calls page_entries_info is what solved it for me.
<%= page_entries_info #orders, entry_name: "order" =>
See the solution here.

ArgumentError in ConversationsController#show -- wrong number of arguments (3 for 1..2)

With the Messageable gem in my Ruby on Rails app, I can access the New Conversation page successfully and chose a user to send to, a title for the message, and of course the body of the message itself.
However, when I click the send button and the page reloads, I receive the following error message:
ArgumentError in ConversationsController#show
wrong number of arguments (3 for 1..2)
Extracted source (around line #4):
2
3 # Returns the Gravatar for the given user.
4 def gravatar_for(user, options = { size: 80 })
5 gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
6 size = options[:size]
7 gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=# {size}"
What could be causing this issue? I'm having so many issues with the New Relic tutorial for Messageable I've been following.. I'm not sure if this error perhaps indicates some problem with there being two seperate gravatar methods that were for two different functions & tutorials (users_helper.rb gravatar method is implemented for profile pictures, from the Hartl tutorial; application_helper.rb gravatar method is implemented for the Messageable new relic tutorial). I don't believe there is any collision between the two causing an error as I haven't been able to fix it yet, but it's a possibility(?)
Anyways, any solution to this problem would be greatly appreciated.
=>Rails version: 4.1.2, Ruby version: 2.1.4
app/helpers/users_helper.rb:
module UsersHelper
# Returns the Gravatar for the given user.
def gravatar_for(user, options = { size: 80 })
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.username, class: "gravatar")
end
end
Full Backtrace:
app/helpers/users_helper.rb:4:in `gravatar_for'
app/views/conversations/show.html.erb:16:in `block in _app_views_conversations_show_html_erb__1169498260752427397_69885071298420'
app/views/conversations/show.html.erb:12:in `_app_views_conversations_show_html_erb__1169498260752427397_69885071298420'
activerecord (4.2.0) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.2.0) lib/active_record/relation/delegation.rb:46:in `each'
actionview (4.2.0) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:143:in `render'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
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'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:5: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:117:in `call'
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:169: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:151:in `call'
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 `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
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 `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 `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 `call'
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 `call'
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 `call'
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 `each'
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'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) 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.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) 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 `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'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13: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'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) 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.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
rack-timeout (0.2.0) lib/rack/timeout.rb:108: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.0) lib/rack/lock.rb:17:in `call'
rack (1.6.0) lib/rack/content_length.rb:15:in `call'
rack (1.6.0) lib/rack/handler/webrick.rb:89:in `service'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
application_helper:
module ApplicationHelper
# Helper method to easily render Gravatars.
def gravatar_for(user, size = 30, title = user.username)
image_tag gravatar_image_url(user.email, size: size), title: title, class: 'img-rounded'
end
end
_participants.html.erb:
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
messages_helper.rb:
module MessagesHelper
def recipients_options
s = ''
User.all.each do |user|
s << "<option value='#{user.id}' data-img-src='#{gravatar_image_url(user.email, size: 50)}'>#{user.name}</option>"
end
s.html_safe
end
end
app/views/conversations/show.html.erb:
<% provide(:title, 'Conversation') %>
<p>Chatting with
<%= render 'conversations/participants', conversation: #conversation %>
</p>
<div class="panel panel-default">
<div class="panel-heading"><%= #conversation.subject %></div>
<div class="panel-body">
<div class="messages">
<% #conversation.receipts_for(current_user).each do |receipt| %>
<div class="media">
<% message = receipt.message %>
<div class="media-left">
<%= gravatar_for message.sender, 45, message.sender.name %>
</div>
<div class="media-body">
<h6 class="media-heading"><%= message.sender.name %>
says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></h6>
<%= message.body %>
</div>
</div>
<% end %>
</div>
</div>
</div>
In your show template there is code:
<%= gravatar_for message.sender, 45, message.sender.name %>
Unfortunately you have defined gravatar_for in two helpers - one in your UsersHelper, and one in your ApplicationHelper.
It looks like the definition from UsersHelper takes precedence over ApplicationHelper's one. Try removing the one from UserHelper, so the proper method is called.
Good luck!
EDIT
You've mentioned you want to be able to customise the gravatar size, and it is possible with your current implementation of UsersHelper#gravatar_for. Change your code in show as follows:
<%= gravatar_for message.sender, { size: 45 } %>
Please note I removed the third argument so it doesn't cause any errors. Additionally I passed a hash with defined size.
This should do the trick!
Hope that helps!

Rendering partial from another controller breaks app - undefined method `text' for #<Post::ActiveRecord_Relation:0x007fe74893b390>

I'm trying to render partials from another view posts/_index and posts/_new and controller Posts, into another view welcome/index. The posts/_index loads fine, but I can't figure out/understand why the posts/_new throws an error for the :text method which is only defined in the database migration, which I thought would be universally accessible.
This is where it references the error in the welcome/_new partial:
<%= form_for :post, url: posts_path do |f| %>
<p id="post_box">
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
And this is the working posts/_index partial:
<% post.reverse.each do |post| %>
<div class="panel panel-default">
<div class="panel-body">
<%= post.text %>
</div>
</div>
<% end %>
For reference the code is the same as: Why aren't instance variables defined in a controller's methods available in the corresponding partials?
except controllers/welcome_controller.rb
class WelcomeController < ApplicationController
def new
end
def index
#post = Post.all
end
private
def post_params
params.require(:post).permit(:text)
end
end
Full trace:
app/views/posts/_index.html.erb:1:in `_app_views_posts__index_html_erb__2553516224846921857_70315677910360'
actionview (4.1.7) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.7) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.7) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.7) lib/action_view/template.rb:143:in `render'
actionview (4.1.7) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
actionview (4.1.7) lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
actionview (4.1.7) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.7) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.7) lib/action_view/renderer/partial_renderer.rb:278:in `render'
actionview (4.1.7) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.1.7) lib/action_view/renderer/renderer.rb:21:in `render'
actionview (4.1.7) lib/action_view/helpers/rendering_helper.rb:32:in `render'
app/views/welcome/index.html.erb:118:in `_app_views_welcome_index_html_erb___2498066675045436064_70315678370540'
actionview (4.1.7) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.7) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.7) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.7) lib/action_view/template.rb:143:in `render'
actionview (4.1.7) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.7) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.7) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.7) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.7) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.7) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.7) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.7) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.7) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.7) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.7) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.7) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.7) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.7) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.7) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.7) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.7) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/lasernite/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.7) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.7) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.7) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.7) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.7) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.7) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.7) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.7) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.7) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.7) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.7) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.7) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.7) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.7) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.7) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.7) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.7) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.7) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.7) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.7) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.7) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.7) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.7) lib/action_dispatch/routing/route_set.rb:678:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.7) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.7) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.7) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.7) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.7) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.7) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.7) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.7) 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.7) 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.7) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.7) lib/rails/engine.rb:514:in `call'
railties (4.1.7) 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'
/Users/lasernite/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/lasernite/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/lasernite/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
The issue here is you are trying to print the text of all the posts, the text is attribute of single post.. That' why it's unable to find, 'text' attribute of association result(Post.all).
def index
#posts = Post.all
end
#index.html.erb
<% #posts.each do |post| %>
<%= post.text %>
<% end %>
Assuming you are trying to list all the posts & create a new post being in the welcome/index, you need to modify your index action like this :
def index
#posts = Post.all #this one to list posts
#post = Post.new #this initializer will be used in your _new partial.
end

Resources