Muktadir
Muktadir

Reputation: 303

How to use notify.js with a button in rails 4?

I am new in rails. I want to show notification using notify.js. I have downloaded the notify.js file and put it into app/assets/javascripts. Now <script> $.notify("Hello", "success" ); </script>

works perfectly. But I want to perform this action after clicking a button.

<%= link_to image_tag("done.png", :title => 'Mark as Done', :class => 'controller_images'), task_path(task), :method => :put %>

This is my code for button.

def create
@task = Task.new(task_params)
if @task.save
  redirect_to tasks_path, :notice => "Your new Task is added successfully!"

else
  render "new"
end
end

And this is the code of controller class for create new task. I want to use notify.js when a task is successfully created instead of using :notice

Someone please help me out. Thanks in advance.

Upvotes: 0

Views: 933

Answers (1)

Rubyrider
Rubyrider

Reputation: 3587

This could be an example, but not the best case I must say. I would rather, I inject the notice somewhere in the body and keep looking for any notice / error / other type notice and call the notify.js with the available parse data.

<script type="text/javascript">
     $(function(){
        var notice = "<%= flash[:notice] %>"
         $.notify(notice, "success" );
     })
</script>

Upvotes: 1

Related Questions