【发布时间】:2013-01-01 20:51:35
【问题描述】:
我正在尝试同时创建两个对象,但由于某种原因,我的第二个对象的字段为空白。
模型是:
Users: id, name, last, email, password ...
Locations: address, longitude, latitude.
关系是这样的:
User has_many location
Location belongs_to users
事件控制器创建方法:
def create
@event = current_users.events.build(params[:event])
@location = @event.locations.build(params[:location])
@location.longitude = params[:longitude]
@location.latitude = params[:latitude]
respond_to do |format|
if @location.save
@location.longitude = params[:longitude]
@location.latitude = params[:latitude]
if @location.save
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render json: @event, status: :created, location: @event }
else
format.html { render action: "new" }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
end
end
还有形式:
<%= f.hidden_field :latitude %>
<%= f.hidden_field :longitude %>
但是当我创建我的位置时,longitude 和 latitude 是空白的,但我的 event_id 已填满。有没有更好的方法同时创建两个对象?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 geocoding