开发者

How can I update an association (has_one) in a Rails AR using link_to_remote?

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/cont开发者_JAVA百科acts/24?method=put&status=positive+response 404 Not Found
    312ms


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" %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜