我已经建立了与用户和事件的关联。我正确设置了关联,并在我的 Rails 控制台中对其进行了测试,使用 user = User.find(1) user.events 会显示我的关联事件。我的问题与身份验证有关。我有一个登录名,当它登录时直接进入主页并显示所有打开的事件(如管理页面):
会话控制器:
def create
if user = User.authenticate(params[:name], params[:password])
session[:user_id] = user.id
redirect_to events_url
else
redirect_to login_url, :alert => "Invalid user/password combination"
end
end
我希望能够创建一个登录名并将其与该人的所有事件相关联。例如,员工使用姓名首字母 JM 登录,我希望所有以该人姓名首字母打开的事件都显示在他们的队列中。以下是我认为可能适用的其他一些应用程序。
class EventsController < ApplicationController
# GET /events
# GET /events.json
def index
@events = Event.order("events.initials ASC")
@archived = CompleteEvent.order("complete_events.created_at ASC")
respond_to do |format|
format.html # index.html.erb
format.json { render json: @events }
end
end
# GET /events/1
# GET /events/1.json
def show
@event = Event.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @event }
end
end
# GET /events/new
# GET /events/new.json
def new
@event = Event.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @event }
end
end
# GET /events/1/edit
def edit
@event = Event.find(params[:id])
end
# POST /events
# POST /events.json
def create
@event = Event.new(params[:event])
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render json: @event, status: :created, location: @event }
else
format.html { render action: "new" }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# PUT /events/1
# PUT /events/1.json
def update
@event = Event.find(params[:id])
@complete = CompleteEvent.new(:initials => @event.initials, :name => @event.name, :event_name => @event.event_name, :complete => @event.complete, :event_description => @event.event_description, :comment => @event.comment)
respond_to do |format|
if @event.update_attributes(params[:event])
if @event.complete == true
@complete
@complete.save
@event.destroy
format.html { redirect_to :controller => :events, :action => :index}
format.json { head :no_content }
elsif @event.complete == false
format.html { redirect_to :controller => :events, :action => :index}
format.json { head :no_content }
end
end
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
@event = Event.find(params[:id])
@event.destroy
respond_to do |format|
format.html { redirect_to events_url }
format.json { head :no_content }
end
end
end