mysql - Users creating rota -
i utilize devise gem , seek create application have user , rota table. want allow users create, show, edit, destroy own rota allow admin everything. controllers are:
class userscontroller < applicationcontroller before_filter :authenticate_user! after_action :verify_authorized def index @users = user.all authorize user end def show @user = user.find(params[:id]) authorize @user end def update @user = user.find(params[:id]) authorize @user if @user.update_attributes(secure_params) redirect_to users_path, :notice => "user updated." else redirect_to users_path, :alert => "unable update user." end end def destroy user = user.find(params[:id]) authorize user user.destroy redirect_to users_path, :notice => "user deleted." end def edit @rotum = @user.rota.find_by id: params[:id] # returns nil in case record not exist or not belong @user redirect_to "somewhere", alert: 'you cannot edit element' if @rotum.blank? end private def secure_params params.require(:user).permit(:role) end end class rotacontroller < applicationcontroller respond_to :html, :xml, :json before_action :set_rotum, only: [:show, :edit, :update, :destroy] def index @rota = rotum.all respond_with(@rota) end def show respond_with(@rotum) end def new @rotum = rotum.new respond_with(@rotum) end def edit end def create @rotum = rotum.new(rotum_params) @rotum.save respond_with(@rotum) end def update @rotum.update(rotum_params) respond_with(@rotum) end def destroy @rotum.destroy respond_with(@rotum) end private def set_rotum @rotum = current_user.rota.find(params[:id]) if @rotum.nil? render :html => "not authorized", :status => 401 end end def rotum_params params.require(:rotum).permit(:name, :email, :mobile, :category) end end
my models like: class user < activerecord::base has_many :rota, dependent: :destroy
and
class rotum < activerecord::base belongs_to :user
my terminal executes:
select rota
.* rota
rota
.user_id
= 3
and error on page
activerecord::recordnotfound @ /rota/31
couldn't find rotum 'id'=31 [where rota
.user_id
= ?]
so it's creating them fields when check in irb nil happening on page. have user_id column on rota table it's nil every rota. how alter every user has unique rota created can play own rota.
.. error on page .. recordnotfound
.. have user_id column on rota table it's nil every rota.
you have asked activerecord find rotum
query this.
select * rota id = 31 , user_id = ? ;
if all records in rota
table have null
user_id
above query homecoming zero records. when activerecord unable find
records, raises recordnotfound
error.
how alter every user has unique rota created can play own rota?
first, recommend adding not-null constraint on rota.user_id
.
alter table rota alter user_id set not null ;
to require every user have rota little more complicated. seek adding rotum_id
users
table , apply foreign key constraint.
mysql ruby-on-rails ruby activerecord devise
No comments:
Post a Comment