Answer the question
In order to leave comments, you need to log in
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 %>
def track_activity(user, trackable, action = params[:action], author = current_user)
@activity = Activity.create! user: user, trackable: trackable, action: action, author: author
end
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question