简体   繁体   中英

has_one relation controller update rails 3

I have 2 models. Task and Location

Each task has_one location

task.rb

has_one :location

I am trying to create my controller for the Location model. 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.

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; also, if a has_one, it wouldn't be pluralized.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM