嵌套路由和public_activity

时间:2013-08-05 09:01:33

标签: ruby-on-rails ruby nested

今天我一整天都在摸不着头脑。目前我已经设置了嵌套路由。

resources :activities
resources :users do
resources :timesheets do
end
end

在我的_create.html.erb partial中,我想链接回user_timesheets。目前唯一可以使用的link_to是

user(@timesheet) 

链接到用户显示页面(users / 1)

通常我会使用此代码链接回用户的时间表。

(/users/1/timesheets/)
user_timesheets_path(@user, @timesheet)

这是我可以在不抛出错误的情况下开始工作的唯一代码

<%= link_to activity.trackable.user.timesheets.name, activity.trackable.user(@timesheet) %> 

这会抛出未定义的方法错误。

我已经尝试过我能想到的每一种组合。我也尝试在我的活动模型中定义@user和@timesheets的实例变量。有什么建议?我错过了什么?

修改 所以我尝试了Rails Guy的答案,但仍然无法工作。这是我的一些代码。

class ActivitiesController < ApplicationController
def index
@activities = PublicActivity::Activity.order("created_at desc")
end
end

我想知道它是否无效,因为我没有指定@user是什么或@timesheet在我的Activities控制器中。我试图这样做但无济于事。

UPDATE2

这是我的路线。

    new_user_session GET    /users/sign_in(.:format)                        devise/sessions#new
              user_session POST   /users/sign_in(.:format)                        devise/sessions#create
      destroy_user_session DELETE /users/sign_out(.:format)                       devise/sessions#destroy
             user_password POST   /users/password(.:format)                       devise/passwords#create
         new_user_password GET    /users/password/new(.:format)                   devise/passwords#new
        edit_user_password GET    /users/password/edit(.:format)                  devise/passwords#edit
                           PUT    /users/password(.:format)                       devise/passwords#update
  cancel_user_registration GET    /users/cancel(.:format)                         devise/registrations#cancel
         user_registration POST   /users(.:format)                                devise/registrations#create
     new_user_registration GET    /users/sign_up(.:format)                        devise/registrations#new
    edit_user_registration GET    /users/edit(.:format)                           devise/registrations#edit
                           PUT    /users(.:format)                                devise/registrations#update
                           DELETE /users(.:format)                                devise/registrations#destroy
                activities GET    /activities(.:format)                           activities#index
                           POST   /activities(.:format)                           activities#create
              new_activity GET    /activities/new(.:format)                       activities#new
             edit_activity GET    /activities/:id/edit(.:format)                  activities#edit
                  activity GET    /activities/:id(.:format)                       activities#show
                           PUT    /activities/:id(.:format)                       activities#update
                           DELETE /activities/:id(.:format)                       activities#destroy
statistics_user_timesheets GET    /users/:user_id/timesheets/statistics(.:format) timesheets#statistics
       all_user_timesheets GET    /users/:user_id/timesheets/all(.:format)        timesheets#all
  lastweek_user_timesheets GET    /users/:user_id/timesheets/lastweek(.:format)   timesheets#lastweek
           user_timesheets GET    /users/:user_id/timesheets(.:format)            timesheets#index
                           POST   /users/:user_id/timesheets(.:format)            timesheets#create
        new_user_timesheet GET    /users/:user_id/timesheets/new(.:format)        timesheets#new
       edit_user_timesheet GET    /users/:user_id/timesheets/:id/edit(.:format)   timesheets#edit
            user_timesheet GET    /users/:user_id/timesheets/:id(.:format)        timesheets#show
                           PUT    /users/:user_id/timesheets/:id(.:format)        timesheets#update
                           DELETE /users/:user_id/timesheets/:id(.:format)        timesheets#destroy
                     users GET    /users(.:format)                                users#index
                           POST   /users(.:format)                                users#create
                  new_user GET    /users/new(.:format)                            users#new
                 edit_user GET    /users/:id/edit(.:format)                       users#edit
                      user GET    /users/:id(.:format)                            users#show
                           PUT    /users/:id(.:format)                            users#update
                           DELETE /users/:id(.:format)                            users#destroy
                      root        /                                               home#index
                      root        /      

这是我的时间表控制器。但是,当我在我的时间表控制器中查看视图时,一切正常。我想要做的是让他们在我的活动控制器下的活动视图中工作。 (这是我认为我需要做的才能使它工作)。

    class TimesheetsController < ApplicationController

  load_and_authorize_resource :user
  load_and_authorize_resource :timesheet, :through => :user, :shallow => true

  # GET /timesheets
  # GET /timesheets.json
  def index

    @timesheets = @user.timesheets.where('day BETWEEN ? AND ?', Date.today.beginning_of_week(:sunday), Date.today.end_of_week).order('created_at DESC').page(params[:page]).per(7)
    @hours = @timesheets.sum{|p| p.teacher + p.conversation + p.study}
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @timesheets }
    end
  end

  # GET /timesheets/1
  # GET /timesheets/1.json
  def show
    @timesheet = Timesheet.find(params[:id])


    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @timesheet }
    end
  end

  # GET /timesheets/new
  # GET /timesheets/new.json
  def new
    @timesheet = Timesheet.new



    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @timesheet }
    end
  end

  # GET /timesheets/1/edit
  def edit
    @timesheet = @user.timesheets.find(params[:id])

  end

  # POST /timesheets
  # POST /timesheets.json
  def create
    @timesheet = @user.timesheets.build(params[:timesheet])


    respond_to do |format|
      if @timesheet.save
        @timesheet.create_activity :create, owner: current_user
        format.html { redirect_to user_timesheets_path, notice: 'Timesheet was successfully created.' }
        format.json { render json: @timesheet, status: :created, location: @timesheet }
      else
        format.html { render action: "new" }
        format.json { render json: @timesheet.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /timesheets/1
  # PUT /timesheets/1.json
  def update
    @timesheet = @user.timesheets.find(params[:id])
    @hours = @timesheet.hours_studied
    respond_to do |format|
      if @timesheet.update_attributes(params[:timesheet])
        @timesheet.create_activity :update, owner: current_user
        format.html { redirect_to user_timesheet_path(@user, @timesheet), notice: 'Timesheet was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @timesheet.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /timesheets/1
  # DELETE /timesheets/1.json
  def destroy
    @timesheet = @user.timesheets.find(params[:id])
    @timesheet.destroy

    respond_to do |format|
      format.html { redirect_to user_timesheets_path }
      format.json { head :no_content }
    end
  end

   def statistics
        @timesheets = @user.timesheets.all

        respond_to do |format|
      format.html # yours.html.erb
     format.xml  { render json: @user.timesheets }
    end
  end

 def all
        @timesheets = @user.timesheets.order('created_at DESC').page(params[:page])
        @hours = @timesheets.sum{|p| p.teacher + p.conversation + p.study}
        @first = @timesheets.first.day.strftime("%B %-d")
        @last = @timesheets.last.day.strftime("%B %-d")
        respond_to do |format|
      format.html # yours.html.erb
     format.xml  { render json: @user.timesheets }
    end
  end

def lastweek
        @timesheets = @user.timesheets.where(:day => 1.week.ago.beginning_of_week..1.week.ago.end_of_week).page(params[:page]).per(7)
        @hours = @timesheets.sum{|p| p.teacher + p.conversation + p.study}
        @first = @timesheets.first.day.strftime("%B %-d")
        @last = @timesheets.last.day.strftime("%B %-d")
        respond_to do |format|
      format.html # yours.html.erb
     format.xml  { render json: @user.timesheets }
    end


end





end

1 个答案:

答案 0 :(得分:0)

我认为必须这样做:

resources :users do
  resources :timesheets
end

现在您将能够访问时间表索引方法,如:

user_timesheets_path(@user)

并显示如下方法:

user_timesheet_path(@user, @timesheet)

希望它会有所帮助。谢谢