Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Routes and public_activity
    primarykey
    data
    text
    <p>I've been scratching my head at this all day today. Currently I have nested routes setup like so.</p> <pre><code>resources :activities resources :users do resources :timesheets do end end </code></pre> <p>In my _create.html.erb partial , I want to link back to the user_timesheets. Currently the only link_to I can get working is </p> <pre><code>user(@timesheet) </code></pre> <p>which links to the user show page (users/1)</p> <p>Typically I'd use this code to link back to the user's timesheets. </p> <pre><code>(/users/1/timesheets/) user_timesheets_path(@user, @timesheet) </code></pre> <p>here is the only bit of code i can get to work without throwing errors</p> <pre><code>&lt;%= link_to activity.trackable.user.timesheets.name, activity.trackable.user(@timesheet) %&gt; </code></pre> <p>This throws me undefined method errors. </p> <p>I've tried every combination I can think of. I've also tried defining instance variables of @user and @timesheets in my activities model. Any suggestions? What am I missing?</p> <p><strong>Edit</strong> So I've tried Rails Guy's answers, but still wont work. Here is some more of my code.</p> <pre><code>class ActivitiesController &lt; ApplicationController def index @activities = PublicActivity::Activity.order("created_at desc") end end </code></pre> <p>I'm wondering if it isn't working because I don't specify what @user is or what @timesheet is in my Activities controller. I've attempted to do so but to no avail.</p> <p><strong>Update2</strong></p> <p>Here are my routes.</p> <pre><code> 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 / </code></pre> <p>Here is my timesheet Controller. However, everything works when I work in views within my timesheets controller. What I'm trying to do is have them working in my activities views under my activities controller. (which is what I assume I need to do to make it work).</p> <pre><code> class TimesheetsController &lt; ApplicationController load_and_authorize_resource :user load_and_authorize_resource :timesheet, :through =&gt; :user, :shallow =&gt; 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 =&gt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload