Hi,all!
I want to submit ‘address’ value in the location model .
And I want to submit ‘address’ value in the room/_form.html.erb .
So,I am using nested_form but it doesn’t work. Please teach me some
advice.
Error message is like this.
ActiveRecord::AssociationTypeMismatch (Location(#70291171806880)
expected,
got ActiveSupport::HashWithIndifferentAccess(#70291193905860)):
app/controllers/rooms_controller.rb:44:in `create'
My db/schema is following…
...
create_table "locations", :force => true do |t|
t.string "address"
t.float "latitude"
t.float "longitude"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "rooms", :force => true do |t|
t.string "room_name"
t.text "description"
t.string "pref_name"
t.string "email"
t.integer "user_id"
t.integer "price"
t.datetime "created_at"
t.datetime "updated_at"
end
...
Room model is …
class Room < ActiveRecord::Base
belongs_to :location
belongs_to :user
accepts_nested_attributes_for :location,:allow_destroy => true
...
#rooms/_form.html.erb
<div>
<%= f.fields_for @location do |loc| %>
<%= loc.label "address" %><br/>
<%= loc.text_field :address %>
<% end %>
</div>
rooms_controller.rb
def new
@room = current_user.rooms.build
@location = current_user.locations.build
respond_to do |format|
format.html # new.html.erb
end
end
....
def create
@room = current_user.rooms.build(params[:room])
@location = current_user.locations.build(params[:location])
respond_to do |format|
if @room.save
format.html { redirect_to(@room, :notice => 'Room was
successfully
created.') }
else
format.html { render :action => "new" }
end
end
end
Thanks!