D
D
dexdev2014-10-10 16:55:37
Ruby on Rails
dexdev, 2014-10-10 16:55:37

How to attach a js file to an action if the action has no form?

There is a track_activity.js.erb file

<% publish_to "/notifications/new" do %>
  $("#activity").append("<%= j render(@activity) %>");
<% end %>

and there is an action in application_controller.rb
def track_activity(user, trackable, action = params[:action], author = current_user)
    @activity =  Activity.create! user: user, trackable: trackable, action: action, author: author
  end

but it does not have a form, it is called, for example, when commenting on a post
comment_controller.rb
def create
        @post = Post.find(params[:post_id])
        @comment = @post.comments.build(comment_params)
        @comment.user = current_user
        if @comment.save
            track_activity @post.user, @comment
            redirect_to post_path @post
        else
            render 'new'
        end
    end

and here is the question how to call track_activity.js.erb always when using the track_activity action or some alternative method for passing data from Private_pub?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2014-10-11
@vsuhachev

If you do not have a route to track_activity, then this is not an action but just a function, call it wherever you want.
Next, put your js in app/viwes/shared with the prefix _ in the file name and also call from those views from which you need. It's called partial

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question