I'm new to RoR. I'm trying to create a basic web app where a Profile page shows a list of Items, which belongs to a specific profile.
So far I have:
rails generate scaffold Profile name:string
rails generate scaffold Item description:string
rake db:migrate
In the profile model: has_many :items In the item model: belongs_to :profile
When I try to create a new profile, I get the error:
undefined method `item' for #<Profile
Profile controller
class ProfilesController < ApplicationController
# GET /profiles
# GET /profiles.json
def index
#profiles = profile.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #profiles }
end
end
# GET /profiles/1
# GET /profiles/1.json
def show
#profile = profile.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #profile }
end
end
# GET /profiles/new
# GET /profiles/new.json
def new
#profile = profile.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #profile }
end
end
# GET /profiles/1/edit
def edit
#profile = profile.find(params[:id])
end
# POST /profiles
# POST /profiles.json
def create
#profile = profile.new(params[:profile])
respond_to do |format|
if #profile.save
format.html { redirect_to #profile, notice: 'profile was successfully created.' }
format.json { render json: #profile, status: :created, location: #profile }
else
format.html { render action: "new" }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
# PUT /profiles/1
# PUT /profiles/1.json
def update
#profile = profile.find(params[:id])
respond_to do |format|
if #profile.update_attributes(params[:profile])
format.html { redirect_to #profile, notice: 'profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #profile.errors, status: :unprocessable_entity }
end
end
end
# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
#profile = profile.find(params[:id])
#profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url }
format.json { head :no_content }
end
end
end
Profile index.html.erb
<h1>Listing profiles</h1>
<table>
<tr>
<th>Name</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #profiles.each do |profile| %>
<tr>
<td><%= profile.name %></td>
<td><%= link_to 'Show', profile %></td>
<td><%= link_to 'Edit', edit_profile_path(profile) %></td>
<td><%= link_to 'Destroy', profile, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New profile', new_profile_path %>
Profile _show.html.erb
<%= simple_form_for(#profile) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
</div>
<div class="form-inputs">
<%= f.input :item %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Full error trace:
activemodel (3.2.12) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.12) lib/active_record/attribute_methods.rb:149:in `method_missing'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:1161:in `value_before_type_cast'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:1149:in `value_before_type_cast'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:1043:in `block in to_input_field_tag'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:1043:in `fetch'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:1043:in `to_input_field_tag'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:692:in `text_field'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:1284:in `text_field'
simple_form (2.1.0) lib/simple_form/inputs/string_input.rb:13:in `input'
simple_form (2.1.0) lib/simple_form/wrappers/many.rb:29:in `block in render'
simple_form (2.1.0) lib/simple_form/wrappers/many.rb:27:in `each'
simple_form (2.1.0) lib/simple_form/wrappers/many.rb:27:in `render'
simple_form (2.1.0) lib/simple_form/wrappers/many.rb:29:in `block in render'
simple_form (2.1.0) lib/simple_form/wrappers/many.rb:27:in `each'
simple_form (2.1.0) lib/simple_form/wrappers/many.rb:27:in `render'
simple_form (2.1.0) lib/simple_form/wrappers/root.rb:15:in `render'
simple_form (2.1.0) lib/simple_form/form_builder.rb:117:in `input'
app/views/profiles/_form.html.erb:9:in `block in _app_views_profiles__form_html_erb___911765346923063960_2183588380'
actionpack (3.2.12) lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
actionpack (3.2.12) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
actionpack (3.2.12) lib/action_view/helpers/capture_helper.rb:40:in `capture'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:607:in `fields_for'
actionpack (3.2.12) lib/action_view/helpers/form_helper.rb:378:in `form_for'
simple_form (2.1.0) lib/simple_form/action_view_extensions/form_helper.rb:29:in `block in simple_form_for'
simple_form (2.1.0) lib/simple_form/action_view_extensions/form_helper.rb:48:in `with_simple_form_field_error_proc'
simple_form (2.1.0) lib/simple_form/action_view_extensions/form_helper.rb:28:in `simple_form_for'
app/views/profiles/_form.html.erb:1:in `_app_views_profiles__form_html_erb___911765346923063960_2183588380'
actionpack (3.2.12) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.12) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.12) lib/action_view/template.rb:143:in `render'
actionpack (3.2.12) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
actionpack (3.2.12) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
actionpack (3.2.12) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.12) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.12) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.12) lib/action_view/renderer/partial_renderer.rb:237:in `render'
actionpack (3.2.12) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.2.12) lib/action_view/helpers/rendering_helper.rb:27:in `render'
app/views/profiles/new.html.erb:3:in `_app_views_profiles_new_html_erb___3444063506780261827_2182112400'
actionpack (3.2.12) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.12) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.12) lib/action_view/template.rb:143:in `render'
actionpack (3.2.12) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.12) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.12) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.12) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.12) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.12) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.12) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.12) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.12) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.12) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.12) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.12) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.12) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.12) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.12) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.12) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.12) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
activesupport (3.2.12) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.12) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.2.12) lib/action_controller/metal/mime_responds.rb:196:in `respond_to'
app/controllers/profiles_controller.rb:29:in `new'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:414:in `_run__312850119216024862__process_action__607287929354052739__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.12) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.12) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.12) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.12) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.12) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.12) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.12) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.12) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.12) lib/action_dispatch/routing/route_set.rb:601:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.12) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.12) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `_run__1382692569756144731__call__1219384665416315194__callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.12) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.12) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.12) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.12) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.12) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.12) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.12) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.12) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.12) lib/rails/engine.rb:479:in `call'
railties (3.2.12) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.12) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
In order for the has_many :items to work you will need to have a profile_id:integer in your Item model.
You can accomplish by doing the following command
rails g migration add_profile_id_to_items profile_id:integer
rake db:migrate
That should create the foreign key needed for the association.
After that you should be able to do things like this
#profile = Profile.create name: "MyProfile"
#item = #profile.items.build description: "This is my awesome item"
#item.save
There are different ways to create items for a profile but that's the most straight forward way. Then to retrieve those items you would do something like this
#profile = Profile.find(some_id)
#items = Item.where(profile_id: #profile.id).all
or
#profile = Profile.find(some_id)
#items = #profile.items
First of all, your Profile object does not have 'item' field.
Check your object's attributes by executing these commands:
rails console
Profile.connection
Profile
or visit your db/schema.rb file
You will see that there is no attribute :item in your object's hash.
You probably want to show the profile's items in your show.html.erb view. First though, you need to create an Item of the profile that it belongs to like so:
item = Item.new(description: "Test description", profile_id: 1)
Then you can do:
Profile.first.items
Second, you have an error in your controller. Object's class should be capitalized, so:
rails console
profile = profile.new(name: "Test profile")
will raise an error
undefined method `new' for nil:NilClass
Related
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 %>
I have this model:
class ToType < ActiveRecord::Base
attr_accessible :Name, :TYP_CCM, :TYP_CCM_TAX, :TYP_CDS_ID, :TYP_CTM, :TYP_CYLINDERS, :TYP_DOORS, :TYP_HP_FROM, :TYP_HP_UPTO, :TYP_ID, :TYP_KV_ABS_DES_ID, :TYP_KV_ASR_DES_ID, :TYP_KV_AXLE_DES_ID, :TYP_KV_BODY_DES_ID, :TYP_KV_BRAKE_SYST_DES_ID, :TYP_KV_BRAKE_TYPE_DES_ID, :TYP_KV_CATALYST_DES_ID, :TYP_KV_DRIVE_DES_ID, :TYP_KV_ENGINE_DES_ID, :TYP_KV_FUEL_DES_ID, :TYP_KV_FUEL_SUPPLY_DES_ID, :TYP_KV_MODEL_DES_ID, :TYP_KV_STEERING_DES_ID, :TYP_KV_STEERING_SIDE_DES_ID, :TYP_KV_TRANS_DES_ID, :TYP_KV_VOLTAGE_DES_ID, :TYP_KW_FROM, :TYP_KW_UPTO, :TYP_LA_CTM, :TYP_LITRES, :TYP_MAX_WEIGHT, :TYP_MMT_CDS_ID, :TYP_MOD_ID, :TYP_PCON_END, :TYP_PCON_START, :TYP_RT_EXISTS, :TYP_SORT, :TYP_TANK, :TYP_VALVES, :is_in_to
set_primary_key :TYP_ID
belongs_to :to_model
has_many :to_articles, :foreign_key => "to_type_id", :dependent => :destroy
end
class ToArticle < ActiveRecord::Base
attr_accessible :details, :manufacturer, :name, :oem_number, :only_with_vin, :quantity, :to_type_id
belongs_to :to_type, :foreign_key => "TYP_ID"
belongs_to :to_engine
end
(some db is converted from big catalog, so rails conventions are slightly imperfect)
Part of my show view of to_type:
%td
= link_to "Подробнее", admin_catalog_to_to_article_path(c), :class=>'btn btn-primary'
= link_to "Редактирование", edit_admin_catalog_to_to_type_path(c), :class=>'btn btn-warning'
= link_to "Удалить", admin_catalog_to_to_type_path(c), :confirm => "!!!Тип #{c.Name} будет удалён!!!! Вы уверены?", :method => :delete, :class => "btn btn-danger"
my show action works normally same for my controller:
class Admin::Catalog::To::ToTypesController < ApplicationController
respond_to :html
before_filter :auth_user
def auth_user
redirect_to new_admin_session_path unless admin_signed_in?
end
def show
#mod_id = params[:id]
#man = ToType.find(:all, conditions: {:TYP_MOD_ID => #mod_id}, order: "Name ASC")
render :layout => 'admin'
end
def edit
#man = ToType.find(params[:id])
render :layout => 'admin'
end
def update
#man = ToType.find(params[:id])
if #mod.update_attributes(params[:to_type])
redirect_to admin_catalog_to_to_type_path(#man.TYP_MOD_ID)
else
render :layout => 'admin'
end
end
def new
#man = ToType.new
#mod_id = params[:mod_id]
render :layout => 'admin'
end
def create
#man = ToType.new(params[:to_type])
#mod_id = params[:mod_id]
#man.TYP_MOD_ID = #mod_id
if #man.save
redirect_to admin_catalog_to_to_type_path(#mod_id)
else
render :layout => 'admin'
end
end
def destroy
#man = ToType.find(params[:id])
if #man.destroy
redirect_to admin_catalog_to_to_type_path(#man.TYP_MOD_ID)
else
render :layout => 'admin'
end
end
end
and route:
namespace :admin do
namespace :catalog do
namespace :to do
resources :to_manufacturers,
:to_models,
:to_types,
:to_articles
end
end
end
form partial:
= form_for [:admin, :catalog, :to, #man] do |f|
= f.label :id
...
When I try edit or create I get:
undefined method `model_name' for NilClass:Class
I think that something is wrong with connection with model: with update and create. In the log I see that the object is founded in db for edit for example, and in the log I can see that #man is not empty, but I still get undefined methodmodel_name' for NilClass:Class` . Why?
upd: Full trace
NoMethodError in Admin/catalog/to/to_types#edit
Showing /media/sf__Projects/Denis/app/views/admin/catalog/to/to_types/_edit.html.haml where line #1 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #1):
1: = form_for [:admin, :catalog, :to, #man] do |f|
2: = f.label :id
Trace of template inclusion: app/views/admin/catalog/to/to_types/edit.html.haml
Rails.root: /media/sf__Projects/Denis
Application Trace | Framework Trace | Full Trace
activemodel (3.2.8) lib/active_model/naming.rb:163:in `model_name_from_record_or_class'
activemodel (3.2.8) lib/active_model/naming.rb:158:in `param_key'
actionpack (3.2.8) lib/action_view/helpers/form_helper.rb:369:in `form_for'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:146:in `form_for_with_haml'
haml (4.0.3) lib/haml/helpers/action_view_xss_mods.rb:28:in `form_for_with_haml_xss'
app/views/admin/catalog/to/to_types/_edit.html.haml:1:in `_app_views_admin_catalog_to_to_types__edit_html_haml___4584370517694291299_51970960'
actionpack (3.2.8) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.8) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.8) lib/action_view/template.rb:143:in `render'
actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.8) lib/action_view/renderer/partial_renderer.rb:237:in `render'
actionpack (3.2.8) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.2.8) lib/action_view/helpers/rendering_helper.rb:27:in `render'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
haml (4.0.3) lib/haml/helpers.rb:89:in `non_haml'
haml (4.0.3) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
app/views/admin/catalog/to/to_types/edit.html.haml:7:in `_app_views_admin_catalog_to_to_types_edit_html_haml___2140731807560860717_28965400'
actionpack (3.2.8) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.8) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.8) lib/action_view/template.rb:143:in `render'
actionpack (3.2.8) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.8) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.8) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.8) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.8) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.8) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.8) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.8) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.8) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.8) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.8) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/home/pavel/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
activesupport (3.2.8) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:39:in `render'
app/controllers/admin/catalog/to/to_types_controller.rb:18:in `edit'
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.8) lib/active_support/callbacks.rb:447:in `_run__3422493215220642812__process_action__1624115620138968240__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:600:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call'
activerecord (3.2.8) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__2440713806940417433__call__2360308109704352565__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/home/pavel/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/pavel/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/pavel/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Request
Parameters:
{"id"=>"28752"}
Show session dump
Show env dump
Response
Headers:
None
Looks too easy, but where is #typ defined? You have got #typ in partial but #man in controller.
You have this in your form form_for [:admin, :catalog, :to, #typ], but you don't have object #typ - you have #man
I'm working with gmaps4rails and when trying to create a new location I get the error msg: 'undefined method `model_name' for NilClass:Class'
Extracted source (around line #1):
1: <%= form_for(#location) do |f| %>
2: <% if #location.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(#location.errors.count, "error") %> prohibited this location from being saved:</h2>
here is the controller:
class LocationsController < ApplicationController
# GET /locations
# GET /locations.json
def index
#locations = Location.all
#json = Location.all.to_gmaps4rails
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #locations }
end
end
# GET /locations/1
# GET /locations/1.json
def show
#location = Location.find(params[:id])
#json = Location.all.to_gmaps4rails
respond_to do |format|
format.html # show.html.erb
format.json { render json: #location }
end
end
# GET /locations/new
# GET /locations/new.json
def new
##location = Location.new
#json = Location.all.to_gmaps4rails
respond_to do |format|
format.html # new.html.erb
format.json { render json: #location }
end
end
# GET /locations/1/edit
def edit
#location = Location.find(params[:id])
#json = Location.all.to_gmaps4rails
end
# POST /locations
# POST /locations.json
def create
##location = Location.new(params[:location])
#json = Location.all.to_gmaps4rails
respond_to do |format|
if #location.save
format.html { redirect_to #location, notice: 'Location was successfully created.' }
format.json { render json: #location, status: :created, location: #location }
else
format.html { render action: "new" }
format.json { render json: #location.errors, status: :unprocessable_entity }
end
end
end
# PUT /locations/1
# PUT /locations/1.json
def update
#location = Location.find(params[:id])
#json = Location.all.to_gmaps4rails
respond_to do |format|
if #location.update_attributes(params[:location])
format.html { redirect_to #location, notice: 'Location was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #location.errors, status: :unprocessable_entity }
end
end
end
# DELETE /locations/1
# DELETE /locations/1.json
def destroy
#location = Location.find(params[:id])
#location.destroy
#json = Location.all.to_gmaps4rails
respond_to do |format|
format.html { redirect_to locations_url }
format.json { head :no_content }
end
end
the model looks like this:
class Location < ActiveRecord::Base
attr_accessible :college_id, :course_id, :id, :name, :latitude, :longitude, :gmaps, :address
belongs_to :college
has_many :courses
acts_as_gmappable
def gmaps4rails_address
address
end
end
and the full trace:
activemodel (3.2.9) lib/active_model/naming.rb:163:in `model_name_from_record_or_class'
activemodel (3.2.9) lib/active_model/naming.rb:158:in `param_key'
actionpack (3.2.9) lib/action_view/helpers/form_helper.rb:369:in `form_for'
app/views/locations/_form.html.erb:1:in `_app_views_locations__form_html_erb___716241816_58056000'
actionpack (3.2.9) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.9) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.9) lib/action_view/template.rb:143:in `render'
actionpack (3.2.9) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
actionpack (3.2.9) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
actionpack (3.2.9) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.9) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.9) lib/action_view/renderer/partial_renderer.rb:237:in `render'
actionpack (3.2.9) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.2.9) lib/action_view/helpers/rendering_helper.rb:27:in `render'
app/views/locations/new.html.erb:3:in `_app_views_locations_new_html_erb___791287105_46366992'
actionpack (3.2.9) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.9) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.9) lib/action_view/template.rb:143:in `render'
actionpack (3.2.9) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.9) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.9) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.9) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.9) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.9) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.9) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.9) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.9) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.9) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.9) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.9) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.9) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.9) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.9) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.9) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.9) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
C:/Ruby192/lib/ruby/1.9.1/benchmark.rb:310:in `realtime'
activesupport (3.2.9) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.9) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.2.9) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.2.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.9) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.9) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.9) lib/active_support/callbacks.rb:414:in `_run__350059794__process_action__911521287__callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.9) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.9) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.9) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.9) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.9) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.9) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.9) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.9) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.9) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.9) lib/action_dispatch/routing/route_set.rb:601:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.9) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `_run__341585242__call__226313936__callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.9) lib/rails/engine.rb:479:in `call'
railties (3.2.9) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
C:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
C:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
C:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Can someone point me in the right direction please?
thank you!
J
Actually you do not create any location in your controller and #location is nil. These lines should not be commented:
#location = Location.new # in locations#new
#location = Location.new(params[:location]) # in locations#create
end tag was missing at bottom of controller code........say no more......
Quick overview: Clicked on my 'Settings' tab on my app and the following error came up. Haven't seen this one before, hope we can solve it with your help.
Error MSG Below + Log:
NoMethodError in Users#edit
Showing /Users/users/Sites/rails_projects/sample_app1/app/views/users/edit.html.erb where line #2 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #2):
1: <h1>Edit user</h1>
2: <%= form_for(#user) do |f| %>
3: <%= render 'fields', :f => f %>
4: <br />
5: <div class="actions">
Full trace:
activemodel (3.2.11) lib/active_model/naming.rb:163:in `model_name_from_record_or_class'
activemodel (3.2.11) lib/active_model/naming.rb:158:in `param_key'
actionpack (3.2.11) lib/action_view/helpers/form_helper.rb:369:in `form_for'
app/views/users/edit.html.erb:2:in `_app_views_users_edit_html_erb__3405427518964834174_70263460873980'
actionpack (3.2.11) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.11) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.11) lib/action_view/template.rb:143:in `render'
actionpack (3.2.11) lib/action_view/renderer/template_renderer.rb:47:in `block (2 levels) in render_template'
actionpack (3.2.11) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.11) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.11) lib/action_view/renderer/template_renderer.rb:46:in `block in render_template'
actionpack (3.2.11) lib/action_view/renderer/template_renderer.rb:54:in `render_with_layout'
actionpack (3.2.11) lib/action_view/renderer/template_renderer.rb:45:in `render_template'
actionpack (3.2.11) lib/action_view/renderer/template_renderer.rb:18:in `render'
actionpack (3.2.11) lib/action_view/renderer/renderer.rb:36:in `render_template'
actionpack (3.2.11) lib/action_view/renderer/renderer.rb:17:in `render'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:110:in `_render_template'
actionpack (3.2.11) lib/action_controller/metal/streaming.rb:225:in `_render_template'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:103:in `render_to_body'
actionpack (3.2.11) lib/action_controller/metal/renderers.rb:28:in `render_to_body'
actionpack (3.2.11) lib/action_controller/metal/compatibility.rb:50:in `render_to_body'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:88:in `render'
actionpack (3.2.11) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
activesupport (3.2.11) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'
activesupport (3.2.11) lib/active_support/core_ext/benchmark.rb:5:in `ms'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:83:in `cleanup_view_runtime'
activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:24:in `cleanup_view_runtime'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:39:in `render'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.11) lib/active_support/callbacks.rb:436:in `_run__1973298596514767122__process_action__4589837863867957684__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__3212209880191913765__call__3512108980805379927__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
_edit.html.erb file:
<h1>Edit user</h1>
<%= form_for(#user) do |f| %>
<%= render 'fields', :f => f %>
<br />
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<br />
<div>
<%= gravatar_for #user %>
<a href="http://gravatar.com/emails" target=_blank>Change</a>
</div>
// Edit added - Users controller:
class UsersController < ApplicationController
before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
before_filter :current_user, :only => [:edit, :update]
before_filter :admin_user, :only => :destroy
def index
#users = User.paginate(:page => params[:page])
#title = "All users"
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def new
#user = User.new
#title = "Sign up"
end
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
redirect_to #user, :flash => {:success => "Welcome"}
else
#title = "Sign up"
render 'new'
end
end
def edit
#title = "Edit use"
end
def update
if #user.update_attributes(params[:user])
redirect_to #user, :flash => {:success => "Profile updated." }
else
#title = "Edit use"
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
redirect_to users_path, :flash => { :success => "User destroyed." }
end
private
def authenticate
deny_access unless signed_in?
end
def correct_user
#user = User.find(params[:id])
redirect_to(root_path) unless current_user?(#user)
end
def admin_user
user = User.find(params[:id])
redirect_to(root_path) if !current_user.admin? || current_user?(user)
end
end
your edit action in UsersController should be smth like:
def edit
#user = User.find(params[:id])
#title = "Edit use"
end
Looks like you are missing an action for '/users/edit' and therefore #user is nil.
Add something like this to your controller.
def edit
#user = User.find(params[:id])
respond_with(#user) do |format|
format.html
end
end