我今天一整天都在为此挠头。目前我有这样的嵌套路由设置。
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 是什么。我试图这样做,但无济于事。
更新2
这是我的路线。
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