PandyZhao
PandyZhao

Reputation: 137

Using Administrate gem, how to render edit page via custom update action?

I'm trying to have my custom update action for my user controller render the edit page if they fail to validate, like so:

def update
  user = User.find(params[:id])
  user.update(user_params)
  # zero because checkbox
  user_params[:banned] == "0" ? user.remove_role(:banned) : user.add_role(:banned)
  if user.banned_note
    if user.banned_note.update(content: params[:user][:reason_for_ban])
      redirect_to "/admin/users/#{params[:id]}"
    else
      render "edit.html.erb"                                    # this line
    end
  end
end

def find_resource(param)
  User.find!(id: param)
end

I want to render the edit page with a flash message listing the errors, but what I have doesn't work. I'm not sure how to route to the correct action, since there's no actual view or edit action (which are auto generated by the Administrate gem).

Upvotes: 1

Views: 2318

Answers (1)

MatayoshiMariano
MatayoshiMariano

Reputation: 2116

Pandy, I have not used this gem but looking the source code and this comment of this issue in GitHub, Have you try the following?

def update
  user = User.find(params[:id])
  user.update(user_params)
  # zero because checkbox
  user_params[:banned] == "0" ? user.remove_role(:banned) : user.add_role(:banned)
  if user.banned_note
    if user.banned_note.update(content: params[:user][:reason_for_ban])
      redirect_to "/admin/users/#{params[:id]}"
    else
      #This two lines
      flash.now[:notice] = "Something"
      render :new, locals: {
        page: Administrate::Page::Form.new(dashboard, resource),
      }
    end
  end
end

def find_resource(param)
  User.find!(id: param)
end

Upvotes: 3

Related Questions