Rails 4简单管理员更新用户属性

时间:2014-03-10 20:39:16

标签: ruby-on-rails ruby

无法让我的简单管理界面工作,并希望你们可以睁开我的眼睛。我猜我的@client变量并不完全正常,我希望如何。它显示在我的'生成'页面上已付款的客户,但是当我填写form_for并提交数据库时,不会更新。

class UsersController < ApplicationController
  before_action :signed_in_user,
                only: [:index, :edit, :update, :destroy, :generate]
  before_action :correct_user,   only: [:edit, :update]
  before_action :admin_user,     only: [:destroy, :generate, :update]
  before_filter :setup_negative_captcha, :only => [:new, :create]

...

def generate
   @client = User.find_by! paid: 'true'
   if @client.update_attributes!(user_params)
    @client.comment = nil
    @client.paid = nil
    render 'generate'
   else
    flash[:error] = "Client not Saved"
    render 'generate'
  end
  end

...

 def correct_user
      @user = User.find(params[:id]) or User.find_by! admin: 'true'
    end

...

<% if current_user.admin?  %>
<%= form_for(@client) do |f| %>
<%= f.text_area :hours1, value: "", rows:1 %><%= f.text_area :hours1_percent, value: "", rows:1 %><br>
<%= f.text_area :hours2, value: "", rows:1 %><%= f.text_area :hours2_percent, value: "", rows:1 %><br>
<%= f.submit " Generate Schedule" ,  class: 'btn-small btn-primary' %> 
<% end %>
<% else %>
<%= link_to "Signin", signin_path %>
<% end %>

1 个答案:

答案 0 :(得分:0)

此行@client = User.find_by! paid: 'true'返回的对象列表不是一个。因此,下一行不适用于if @client.update_attributes!(user_params)。要解决此问题,您必须为@client一个对象分配不是对象列表,例如:@client = User.find(params[:id]),然后在下一行中更改为:if @client.paid? && @client.update_attributes!(user_params)