t56k
t56k

Reputation: 7001

Rails: updating record in controller

I've put this method in my controller so I can lock users documents, but the record of each document remains unchanged.

def lock
  @doc = Doc.find(params[:id])
    respond_to do |format|
      params[:locked] = true
      format.html { redirect_to share_path(@doc) }
      format.json { render json: @doc }
    end
end

I know the params[:locked] is the part that isn't working. What would the correct syntax be? I've tried @doc.update_attribute(:locked, true) without success, too.

Cheers.

Upvotes: 0

Views: 1693

Answers (1)

Simon Perepelitsa
Simon Perepelitsa

Reputation: 20649

Params is a hash of attributes submitted through your form. It is not related to database and is not saved anywhere. In fact, you are only supposed to read data from it, not modify it. To update a record you should call methods on the record itself such as save or update_attribute.

So @doc.update_attribute(:locked, true) should work in your case.

Upvotes: 2

Related Questions