Ruby on Rails: trying to create a new object that belongs to a user... not sure how to do it... correctly
Controllor:
def new(user)
@address = Address.new
@address.user_id = user.id
end
The link I click that triggers the error
<%= link_to "Add Address", new_address_path(current_user)%>
Address model
belongs_to :user
User Model
has_many :addresses
New Address View
<h1>New Address</h1>
<% form_for @address, :url => new_address_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Add Address" %>
<% end %>
Schema for Addresses
create_table "addresses", :force => true do |t|
t.string "number"
t.string "street"
t.string "city"
t.string "state"
t.string "zip_code"
t.integer "user_id"
end
my routes
map.resources :users
map.resources :addresses
And the error is
uninitialized constant AddressesController
Para开发者_高级运维meters:
{"format"=>"#<user:0x105498c60>"}
In what controller is this method placed:
def new(user)
@address = Address.new
@address.user_id = user.id
end
Instead of
new_address_path(current_user)
try using:
new_user_address_path(current_user)
and make sure current_user is a valid user object.
精彩评论