4

I want to ask how to directly update a single attribute in rails. I've already tried many ways to solve it but none of them works. Currently, in my view I have this line of code:

<%= link_to("Ban", admin_update_path(user), :confirm => "Are you sure?")%>

Then in my controller:

def update 
       @user = User.first

        if @user.update_attributes(:reputation =>1)
            redirect_to admin_viewAllUsers_path, :notice => "User banned!"
        else
            render "index"
        end  

    end

Then in my routes:

get "admin/edit"
put "admin/update"

Note: If I used put "admin/update" in my routes, I would get "No route matches [GET] "/admin/update.2" error, but when I used get "admin/update" I didn't get any error but my :reputation wouldn't update. Btw, :reputation is an integer

Moreover, If I use User.find(params[:id]) instead of User.first, I'll get "Couldn't find User without an ID" error.

Pls help. What should I do? Where did I go wrong?

5
  • what exactly is your error? Commented Feb 12, 2013 at 3:37
  • currently, I don't have an error, but my user won't update. The user_attributes didn't work. Commented Feb 12, 2013 at 3:42
  • what is your :reputatation field type? Commented Feb 12, 2013 at 3:51
  • check what error is returned by #update_attributes. try adding a debug line before the render 'index' line Commented Feb 12, 2013 at 3:55
  • Look at the log file to see if you get a warning about reputation not being mass-assignable then check what you have for attr_accessible or attr_protected in your user model. Commented Feb 12, 2013 at 4:57

3 Answers 3

6

For single attribute you can use:

@user.update_attribute(:reputation,1)

For my opinion update action it is smth different(change name,surname etc.) not only ban, I think you should create action ban:

    def ban 
      @user = User.find(params[:id])
      @user.update_attribute(:reputation,1)
      redirect_to admin_viewAllUsers_path, :notice => "User banned!"
    end

and routes for it:

match 'admin/users/:id/ban', :to => 'users#ban', :as => 'admin_user_ban', :via => :post

and finally link:

<%= link_to("Ban", admin_user_ban_path(user), :confirm => "Are you sure?", :method => :post)%>
Sign up to request clarification or add additional context in comments.

2 Comments

it didn't work. is it because of my routes? If I used put "admin/update" in my routes, I would get "No route matches [GET] "/admin/update.2" error, but when I used get "admin/update" I didn't get any error but my :reputation wouldn't update. Btw, :reputation is an integer
you are right, I updated my answer and you can look to @Swards answer
1

why not

@user.reputation = 1
if @user.save
  #do something
else
  puts "Did not save"
end

is reputation in your accessible_attributes?

2 Comments

unless its an api i recomment using "POST" or add :method => :put
is it because I used User.first instead of User.find(params[:id])?
0

Your route is expecting the PUT method. Something along these lines...

<%= link_to("Ban", admin_path(user), :confirm => "Are you sure?", :method => :put)%>

The edit and put methods that you have in your routes file don't indicate an :id attribute. In your case, something like this:

get "admin/:id/edit"
put "admin/:id"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.