ruby-on-railscallbackpublic-activity

Rails Public Activity Gem - after create action controller


I am trying to make an app with Rails 4.

I have installed the public_activity gem.

I followed the Ryan Bates Railscast and took the controller based approach, and also the lighter Common option (as opposed to tracking the Model).

In my activities_controller I have:

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

In my project.rb, I have:

include PublicActivity::Common

In my projects controller, create action, I have:

def create
    logger.debug "xxx create project"
    #authorise @project
    @project = Project.new(project_params)
    @project.creator_id = current_user.id
    @project.users << current_user
    @project.create_activity :create, owner: current_user

    respond_to do |format|
      if @project.save

        format.html { redirect_to @project }
        format.json { render action: 'show', status: :created, location: @project }
      else
        format.html { render action: 'new' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

In my activity view - index, I have:

<% @activities.each do |activity| %>
        <div class="col-md-4">
          <div class="indexdisplay">

            <span class="indexheading"> 
              <%= link_to activity.owner.name, activity.owner if activity.owner %>
            </span>
            <span class="indexsubtext">                 
              <%= render_activity activity %>
            </span>

In my public activity (view folder)/project/_create.html.erb, I have:

<% if activity.trackable %>
  <%= link_to activity.trackable.name, activity.trackable %>
<% else %>
  which has since been removed
<% end %>

When I try this, it gives me a blank page (no new activities have been created). When I try to create a new project, I get stuck on submitting the form because I get this error:

ActiveRecord::RecordNotSaved at /projects
You cannot call create unless the parent is saved

The problem is this line in the create action of my projects controller.

@project.create_activity :create, owner: current_user

Is there somewhere else I can put this in the controller to make it run after the project is saved? I have read about call backs but they need to be in the model and I'd like to keep this process running from the controller.


Solution

  • As the error state, you are trying to create an activity to an unsaved Project. Move the creation of the activity after saving the @project:

    def create
        logger.debug "xxx create project"
        #authorise @project
        @project = Project.new(project_params)
        @project.creator_id = current_user.id
    
        respond_to do |format|
          if @project.save
    
            @project.users << current_user
            @project.create_activity :create, owner: current_user
    
            format.html { redirect_to @project }
            format.json { render action: 'show', status: :created, location: @project }
          else
            format.html { render action: 'new' }
            format.json { render json: @project.errors, status: :unprocessable_entity }
          end
        end
      end