Rails与会话登录和user_id的关联

时间:2013-01-22 04:21:02

标签: ruby-on-rails-3 authentication associations

我已经设置了与用户和事件的关联。我正确设置了关联,并在我的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

0 个答案:

没有答案