bradpotts bradpotts - 6 months ago 9
Ruby Question

Show curent user's members based on user_id (Index View)

I have a user_id column. Instead of calling for all members how can I call up members based on current user's and user_id?

This is my controller, I tried changing .all to user_id or current_user.id plus many variations based on examples. Still can't get it. I also have no models (using authrocket). The create action also works and inserts the user_id, I have a def current_user at the bottom.

class Members::MainsController < ApplicationController

# Security & Action Filters
before_action :require_user
layout :phcmemberspro_membersmain_layouts_resolver
before_action :set_members_main, only: [:show, :edit, :update, :destroy]

# Member Profile List
def index
@members_mains.user_id = current_user.id
@members_mains = Members::Main.all
end

# Detailed Member Profile
def show
end

# New Member Profile
def new
@members_main = Members::Main.new
end

# Edit Member Profile
def edit
end

# Create Member Profile Action
def create
@members_main = Members::Main.new(members_main_params)
@members_main.user_id = current_user.id
if @members_main.save
redirect_to members_mains_path, notice: 'Member profile has been successfully created.'
else
render :new
end
end

# Update Member Profile Action
def update
if @members_main.update(members_main_params)
redirect_to members_mains_path, notice: 'Member profile has been successfully updated.'
else
render :edit
end
end

# Delete Member Profile Action
def destroy
@members_main.destroy
redirect_to members_mains_path, notice: 'Member profile and all businesses was successfully destroyed.'
end

private

# Grab User Session Key (For ID)
def current_user
@_current_user ||= AuthRocket::Session.from_token(session[:ar_token]).try(:user)
end

# Common Callbacks
def set_members_main
@members_main = Members::Main.find(params[:id])
end

# White List
def members_main_params
params.require(:members_main).permit(:mfirstname, :mlastname, :mtitle, :memail, :mphone, :mnotes, :smfacebook, :smtwitter, :smlinkedin, :user_id)
end

end

Answer

If I got it right, your index action should be something like this:

# Member Profile List
def index
  @current_member = Members::Main.find(current_user.id)
end
Comments