如何使用link_to_remote更新Rails AR中的关联(has_one)?

时间:2010-10-05 04:49:29

标签: ruby-on-rails link-to-remote

这就是我的看法:

  <%= link_to_remote "Responded - Positive",
      :url => contact_path(@contact, :status => 'positive response'),
      :update => "status" %>

这就是我的路线:

  map.resources :contacts, :has_one => :status_contact

以下是我在控制器中使用的内容:

  def create
    @status_contact = StatusContact.new(params[:status_contact])
    if @status_contact.save
      #flash[:notice] = "Successfully created status contact."
      #redirect_to @status_contact
      render :text => "Set status to #{@status_contact.status}."
    else
      render :text => "bomb"
    end
  end

我希望的结果是,对于特定的联系人,它将使用值“肯定响应”更新属性Contact.status,并通过ajax进行更新。

现在,我收到404错误。我需要做些什么才能纠正这个问题?

这是我仍然得到的错误:

POST http://localhost:3000/contacts/24?method=put&status=positive+response 404 Not Found
    312ms

1 个答案:

答案 0 :(得分:0)

你可能用错误的动词发送你的请求(:post而不是:put)。你试图达到控制器的哪个动作?这可能是更新操作...尝试通过指定:put方法修改链接:

<%= link_to_remote "Responded - Positive", :url => contact_path(@contact, :status => 'positive response'), :method => :put, :update => "status" %>