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

发布于 2024-09-26 06:35:28 字数 937 浏览 2 评论 0原文

这就是我的观点:

  <%= 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

This is what I have in my view:

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

This is what I have as a route:

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

Here is what I used in my controller:

  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

My desired outcome is that for the specific Contact, it will update the attribute Contact.status with the value 'positive response' and do so via ajax.

Right now, I am getting a 404 error. What do I need to do to correct this?

This is the error that I am still getting:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2024-10-03 06:35:28

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

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

You're probably sending your request with the wrong verb (:post instead of :put). Which action of your controller are you trying to reach? It's probably the update action… Try to modify your link by specifying the :put method:

<%= link_to_remote "Responded - Positive", :url => contact_path(@contact, :status => 'positive response'), :method => :put, :update => "status" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文