[英]has_one relation controller update rails 3
I have 2 models. 我有2个型号。 Task and Location
任务和位置
Each task has_one location 每个任务都有一个位置
task.rb task.rb
has_one :location
I am trying to create my controller for the Location model. 我正在尝试为Location模型创建我的控制器。 This is the new form
这是新形式
<%= form_for(@task.build_Location, :url => task_Location_path(@task)) do |f| %>
Fields
<%= f.submit %>
<% end %>
and the edit form 和编辑表格
<%= form_for(@task.Location, :url => task_Location_path(@task)) do |f| %>
Fields
<%= f.submit %>
<% end %>
currently all functions operate properly with the exception of Update. 目前所有功能都在正常运行,但Update除外。
def create
@location = @task.create_Location(params[:location])
end
def update
@location = @task.locations.find(params[:id])
end
What is the proper way to define this method? 定义此方法的正确方法是什么?
Not exactly sure what you are trying to do but... 不完全确定你要做什么,但......
you usually want to access @task via it's id; 你通常想通过它的id访问@task; also, if a has_one, it wouldn't be pluralized.
另外,如果是has_one,则不会复数。
If you were trying to update a task's name to 'something' you could do something like: 如果您尝试将任务的名称更新为“某事”,则可以执行以下操作:
def update
@location=Location.find(params[:id])
@location.task.name="something"
@location.save
end
def update
@location = @task.location.update_attributes(params[:location])
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.