如何使用 link_to_remote 更新 Rails AR 中的关联 (has_one)?
这就是我的观点:
<%= 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能使用错误的动词发送请求(:post 而不是 :put)。您想要执行控制器的哪个操作?这可能是更新操作...尝试通过指定 :put 方法来修改您的链接:
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: