Question

I'm making a Rails app that uses Mongoid and devise. Currently, the functionality's pretty basic, but it's not working like I expect it to. In particular, update isn't working at all.

I can create a user, but when I go back to update it, it doesn't raise any errors, but also doesn't save. This may be clearer with some code. I've got this in routes:

devise_for :users
resources :users

And this in the controller:

## WORKS PERFECTLY FOR SIGNING USERS UP, AND FLASHES CORRECTLY
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save!
      flash[:success] = "Welcome!"
      sign_in @user
      redirect_to @user
    else
      render 'new'
    end
  end

## DOES NOT UPDATE THE USER RECORD, AND DOES NOT FLASH SUCCESS
## IT DOES, HOWEVER, REDIRECT TO SHOW, INSTEAD OF RENDERING EDIT
  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.save!
      flash[:success] = "Profile edited successfully!"
      redirect_to @user
    else
      render 'edit'
    end 
  end

So yeah. As the code suggests, going through my views to edit an existing user does NOT edit that user, nor does it give the flash saying the users was edited, but it DOES redirect correctly. I currently have NO user validations (though devise might have some) -- but changing all devise-relevant fields doesn't get around the issue, so I don't think it's a silent validation fail.

The form is of the basic

<%= form_for @user do |f| %>
  <%= f.label: name %>
  <%= f.text_field :name %>
  ...
  <%= f.submit %>
<% end %>

sort.

Not sure what other code could be helpful here. Please let me know. I'm super stumped. Thanks!

Was it helpful?

Solution

you have to pass params you can use update_attributes or update_attributes!

@user =  User.find(params[:id])
  if @user.update_attributes!(params[:user])
 ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top