ruby - Rails 4 Simple Admin to Update User Attributes -
having trouble getting simple admin interface work , hoping guys open eyes bit. guessing @client variable not quite working how hoped. displays clients have 'paid' on 'generate' page when fill in form_for , submit database not updated.
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) |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 %>
this line @client = user.find_by! paid: 'true' returns list of objects not one. therefore, next line won't work if @client.update_attributes!(user_params). solve, have assign @client 1 object not list of objects like: @client = user.find(params[:id]) in next line change to: if @client.paid? && @client.update_attributes!(user_params)
Comments
Post a Comment