Child forms, not nested

Hi,

I’m trying to create forms for child objects that are separate from
the parent objects but that preserve the relation. I have this as my
controller methods but when I submit the “new” form it says :

Couldn’t find Inventory without an ID

Can you please help?

def new
@order = @inventory.orders.build
end

def create
@order = Order.new(params[:order])
@order.inventory_id = params[:inventory_id]

respond_to do |format|
  if @order.save
    flash[:success] = 'Order created successfully'
    logger.debug " order saved successfully"
    format.html { redirect_to

inventory_path(@order.inventory_id) }
else
logger.debug " order not saved"
format.html { render :action => “new” }
end
end

end

def get_inventory
@inventory = Inventory.find(params[:inventory_id])
end

I’m not sure if I understood you correctly. I assume you have a route
like this:

resources :inventories do
resources :orders
end

If so you could use a before filter to automatically load the inventory
in your OrdersController before every action like this:

class OrdersController < ApplicationController
def inventory
@inventory ||= Inventory.find(params[:inventory_id])
end
before_filter :inventory

def new
@order = inventory.orders.build
end

def create
@order = inventory.order.build(params[:order])

if @order.save
flash[:success] = ‘Order created successfully’
logger.debug " order saved successfully"
redirect_to inventory
else
logger.debug " order not saved"
render :new
end
end
end

As you can see I simplified your create Action, since you don’t use
anything else than html in the response you can skip the respond_to.
redirect_to inventory is short for redirect_to inventory_path(inventory), and render :new is a shortcut for render :action => 'new'.

I hope I could help, if not please don’t hesitate to ask again.

Regards, Jens
On Freitag, 6. Mai 2011 at 22:29, Mlle wrote:
def new

@order = inventory.order.build(params[:order])

should be

@order = inventory.orders.build(params[:order])
On Freitag, 6. Mai 2011 at 22:50, Jens Fahnenbruck wrote:
I’m not sure if I understood you correctly. I assume you have a route
like this:

The routes description is different in rails 2.3.5

map.resources :inventories do |inventory|
inventory.resources :orders
end

The rest should work as described.
On Freitag, 6. Mai 2011 at 22:56, Mlle wrote:
Oh, I should have mentioned that I’m using Rails 2.3.5. I’m not sure

Oh, I should have mentioned that I’m using Rails 2.3.5. I’m not sure
if that makes a difference?
I’ll try out what you suggested, thanks!

hmm now I get Only get, put, and delete requests are allowed.

This is in my routes:

map.resources :inventories do |inventory|
inventory.resources :orders
end
map.resources :orders

And this is my controller:

before_filter :login_required, :get_inventory

def new
@order = @inventory.orders.build
end

def create
@order = @inventory.orders.build(params[:order])

if @order.save
    flash[:success] = 'Order created successfully'
    logger.debug "order saved successfully"
    redirect_to @inventory
else
    logger.debug "order not saved"
    render :new
end

end

def get_inventory
@inventory = Inventory.find(params[:inventory_id])
end
end

  1. can you post the full error message and what you did to get it

def get_inventory
@inventory = Inventory.find(params[:inventory_id])
end

should_be

def inventory
@inventory ||= Inventory.find(params[:inventory_id])
end

the the Inventory is cached if you need it multiple time during one
request. Leaving the get_ is more like ruby and rails work on
attribute accessors. And if you cache the inventory you can get rid of
the @ in front of any other call to the inventory, just use the defined
accessor method.

You can even get rid of the before filter if you use the inventory
accessor, so the inventory only get’s loaded if you really need it.
On Freitag, 6. Mai 2011 at 23:11, Mlle wrote:
hmm now I get Only get, put, and delete requests are allowed.

ok I made the change number 2.

THis is the full trace:

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/
recognition_optimisation.rb:64:in recognize_path' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/ route_set.rb:441:inrecognize’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/
route_set.rb:436:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb: 87:indispatch’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:
121:in _call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb: 130:inbuild_middleware_stack’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:
29:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb: 29:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/
abstract/query_cache.rb:34:in cache' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:incache’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:
28:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/ abstract/connection_pool.rb:361:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/actionpack-2.3.5/lib/action_controller/
string_coercion.rb:25:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb: 15:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/actionpack-2.3.5/lib/action_controller/session/
cookie_store.rb:93:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:insynchronize’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb: 114:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in
run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb: 108:incall’
/Users/e/.gem/ruby/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in
call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in each' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:incall’
/Users/e/.gem/ruby/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:
17:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:incall’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/rack-1.0.1/lib/rack/chunked.rb:15:in call' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:64:inprocess’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in process_client' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:ineach’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in process_client' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:inrun’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in initialize' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:innew’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:ininitialize’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in new' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:inrun’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
gems/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:34:in run' /Users/e/.gem/ruby/1.8/gems/rails-2.3.5/lib/commands/server.rb:111 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ 1.8/rubygems/custom_require.rb:31:ingem_original_require’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
1.8/rubygems/custom_require.rb:31:in `require’

sorry, but I can’t work with this error, can you post some more info
from the logfile around this error, especially the request log.

and can you show me the code of the form in the orders#new view
On Freitag, 6. Mai 2011 at 23:47, Mlle wrote:
I was on /inventories/4000/orders/new page and click “submit” to

I was on /inventories/4000/orders/new page and click “submit” to
create a new order before getting the error message.

This is the new view for the order:

<%= error_messages_for :order %>

<% form_for :order do |f| -%>

<%= label_tag 'Quantity Requested by order:' %>
<%= f.text_field :qty_requested %>

<%= f.hidden_field :inventory_id, :value=>@inventory.inventory_id %>

<% if [email protected]?%><%= submit_tag 'Submit', :id => @inventory.inventory_id %>

<% end %> <% end %>

and these are the errors in the log file:

Processing OrdersController#new (for 127.0.0.1 at 2011-05-06 15:25:27)
[POST]
Parameters: {“commit”=>“Submit”,
“authenticity_token”=>“bkIEWE9A5XNqPxE3Hv0et3R4NgHHqLi6D99SYSn8ZkQ=”,
“order”=>{“qty_requested”=>“123”}}
[4;35;1mAdminUser Columns (2.1ms) [0m [0mSHOW FIELDS FROM
admin_users [0m
[4;36;1mAdminUser Load (0.3ms) [0m [0;1mSELECT * FROM
admin_users WHERE (admin_users.id = 39) LIMIT 1 [0m
[4;35;1mOrder Columns (2.4ms) [0m [0mSHOW FIELDS FROM
orders [0m

ActiveRecord::RecordNotFound (Couldn’t find Inventory without an ID):
app/controllers/orders_controller.rb:37:in `new’

Rendered rescues/_trace (34.9ms)
Rendered rescues/_request_and_response (0.5ms)
Rendering rescues/layout (not_found)
[4;36;1mSQL (0.1ms) [0m [0;1mSET SQL_AUTO_IS_NULL=0 [0m

You can try

<% form_for [@inventory, @order] do |f| -%>

this will create the correct url (<form action="/inventories/4000/orders
method=“post”>). And you can get rid of the hidden field with the
inventory_id.

why do you have if [email protected] around your submit tag? If the form is
only available with the nested resource url, than it should be always
available.
On Samstag, 7. Mai 2011 at 00:01, Mlle wrote:
<% form_for :order do |f| -%>

oh my, putting

form_for [@inventory, @order]

worked.
Thanks a million!!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs