Rails flash on ajax request (Without reload)How do I pass Rails5 flash notification to toastr in a form_for with remote: true without reloading the page?A concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?ruby on rails has_many relation form validation of childrenActiveModel::ForbiddenAttributesError in CommentsController#createNo Method error : Login attempts using cookies in Rails DeviseNested Forms with Devise/Simple_Form/Rails 4.0Devise - how to show user's postIs it possible to check if flash message exists in javascript (rails)

How do I delete cookies from a specific site?

Matlab fmincon for a problem with many nonlinear constraints

How to calculate the power level of a Commander deck?

How to make Unity sprite animation not lerp?

Professor refuses to write a recommendation letter to students who haven't written a research paper with him

Do 643,000 Americans go bankrupt every year due to medical bills?

Why are some hotels asking you to book through Booking.com instead of matching the price at the front desk?

Why does the seven segment display have decimal point at the right?

Translate English to Pig Latin | PIG_LATIN.PY

Why is a pressure canner needed when canning?

Pronunciation of "sincero" and "sinceramente"

How can I hint that my character isn't real?

"syntax error near unexpected token" after editing .bashrc

Magento 2: Set order history page as default after login

Was the lunar landing site always in the same plane as the CM's orbit?

How do German speakers decide what should be on the left side of the verb?

Book where main character comes out of stasis bubble

Draw the ☣ (Biohazard Symbol)

Why would one hemisphere of a planet be very mountainous while the other is flat?

Why would image resources loaded from different origins triggering HTTP authentication dialogs be harmful?

Infinitely many Primes

What is the material of snubber brakes?

Why does the UK Prime Minister need the permission of Parliament to call a general election?

If I have an accident, should I file a claim with my car insurance company?



Rails flash on ajax request (Without reload)


How do I pass Rails5 flash notification to toastr in a form_for with remote: true without reloading the page?A concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?ruby on rails has_many relation form validation of childrenActiveModel::ForbiddenAttributesError in CommentsController#createNo Method error : Login attempts using cookies in Rails DeviseNested Forms with Devise/Simple_Form/Rails 4.0Devise - how to show user's postIs it possible to check if flash message exists in javascript (rails)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Im building sort of a facebook clone, and one of the requirements for the project is being able to "like" posts.



I have the liking down, (im also using devise fwiw on the user authentication). But when I try to flash that a user has already liked a post (since you can't like more than once) nothing happens.



My controller code:



 def like
post = Post.find(params[:post_id])
puts post
like = post.likes.new(user_id: current_user.id)
if like.save
#nothing for now
else
flash.now[:alert] = "You have already liked that post"
end
end


It works by being on a users "homepage" and going through all of their posts and having a "like" option on each one. (Liking does work, but figured it was worth noting).



If I just do flash it'll work if I refresh the page, but currently does nothing right now.



The "Like" link looks like this:



<% @posts.each do |post| %>
<p>
<%= post.content %>
</p>
<%= link_to "Like", like_path(@user, post_id: post), remote: true, method: :post %>
<% end %>









share|improve this question
























  • If my answer solved your issue, you can mark it as accepted answer

    – ashvin
    Mar 28 at 13:51

















0















Im building sort of a facebook clone, and one of the requirements for the project is being able to "like" posts.



I have the liking down, (im also using devise fwiw on the user authentication). But when I try to flash that a user has already liked a post (since you can't like more than once) nothing happens.



My controller code:



 def like
post = Post.find(params[:post_id])
puts post
like = post.likes.new(user_id: current_user.id)
if like.save
#nothing for now
else
flash.now[:alert] = "You have already liked that post"
end
end


It works by being on a users "homepage" and going through all of their posts and having a "like" option on each one. (Liking does work, but figured it was worth noting).



If I just do flash it'll work if I refresh the page, but currently does nothing right now.



The "Like" link looks like this:



<% @posts.each do |post| %>
<p>
<%= post.content %>
</p>
<%= link_to "Like", like_path(@user, post_id: post), remote: true, method: :post %>
<% end %>









share|improve this question
























  • If my answer solved your issue, you can mark it as accepted answer

    – ashvin
    Mar 28 at 13:51













0












0








0








Im building sort of a facebook clone, and one of the requirements for the project is being able to "like" posts.



I have the liking down, (im also using devise fwiw on the user authentication). But when I try to flash that a user has already liked a post (since you can't like more than once) nothing happens.



My controller code:



 def like
post = Post.find(params[:post_id])
puts post
like = post.likes.new(user_id: current_user.id)
if like.save
#nothing for now
else
flash.now[:alert] = "You have already liked that post"
end
end


It works by being on a users "homepage" and going through all of their posts and having a "like" option on each one. (Liking does work, but figured it was worth noting).



If I just do flash it'll work if I refresh the page, but currently does nothing right now.



The "Like" link looks like this:



<% @posts.each do |post| %>
<p>
<%= post.content %>
</p>
<%= link_to "Like", like_path(@user, post_id: post), remote: true, method: :post %>
<% end %>









share|improve this question














Im building sort of a facebook clone, and one of the requirements for the project is being able to "like" posts.



I have the liking down, (im also using devise fwiw on the user authentication). But when I try to flash that a user has already liked a post (since you can't like more than once) nothing happens.



My controller code:



 def like
post = Post.find(params[:post_id])
puts post
like = post.likes.new(user_id: current_user.id)
if like.save
#nothing for now
else
flash.now[:alert] = "You have already liked that post"
end
end


It works by being on a users "homepage" and going through all of their posts and having a "like" option on each one. (Liking does work, but figured it was worth noting).



If I just do flash it'll work if I refresh the page, but currently does nothing right now.



The "Like" link looks like this:



<% @posts.each do |post| %>
<p>
<%= post.content %>
</p>
<%= link_to "Like", like_path(@user, post_id: post), remote: true, method: :post %>
<% end %>






ruby-on-rails devise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 4:44









msmith1114msmith1114

7651 gold badge13 silver badges34 bronze badges




7651 gold badge13 silver badges34 bronze badges















  • If my answer solved your issue, you can mark it as accepted answer

    – ashvin
    Mar 28 at 13:51

















  • If my answer solved your issue, you can mark it as accepted answer

    – ashvin
    Mar 28 at 13:51
















If my answer solved your issue, you can mark it as accepted answer

– ashvin
Mar 28 at 13:51





If my answer solved your issue, you can mark it as accepted answer

– ashvin
Mar 28 at 13:51












2 Answers
2






active

oldest

votes


















2
















In rails there is one gem for ajax call flash messages
Toaster gem



Create one method in application_helper.rb



def custom_bootstrap_flash
flash_messages = []
flash.each do |type, message|
type = 'success' if type == 'notice'
type = 'error' if type == 'alert'
text = "
<script>
$(function ()
toastr.#type("#message");
);
</script>
"
flash_messages << text.html_safe if message
end
flash_messages.join("n").html_safe
end


Include it in your layout application.html.erb



<%= custom_bootstrap_flash %>


And in your action.js.erb show toast message using toaster method



toastr.success('Success.')
toastr.error('error')


I hope this what you are looking.






share|improve this answer
































    1
















    The flash method on rails shows only on successful redirect. If you want to show the message on the same page it would be better to show the message using JS.






    share|improve this answer



























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );














      draft saved

      draft discarded
















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390312%2frails-flash-on-ajax-request-without-reload%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2
















      In rails there is one gem for ajax call flash messages
      Toaster gem



      Create one method in application_helper.rb



      def custom_bootstrap_flash
      flash_messages = []
      flash.each do |type, message|
      type = 'success' if type == 'notice'
      type = 'error' if type == 'alert'
      text = "
      <script>
      $(function ()
      toastr.#type("#message");
      );
      </script>
      "
      flash_messages << text.html_safe if message
      end
      flash_messages.join("n").html_safe
      end


      Include it in your layout application.html.erb



      <%= custom_bootstrap_flash %>


      And in your action.js.erb show toast message using toaster method



      toastr.success('Success.')
      toastr.error('error')


      I hope this what you are looking.






      share|improve this answer





























        2
















        In rails there is one gem for ajax call flash messages
        Toaster gem



        Create one method in application_helper.rb



        def custom_bootstrap_flash
        flash_messages = []
        flash.each do |type, message|
        type = 'success' if type == 'notice'
        type = 'error' if type == 'alert'
        text = "
        <script>
        $(function ()
        toastr.#type("#message");
        );
        </script>
        "
        flash_messages << text.html_safe if message
        end
        flash_messages.join("n").html_safe
        end


        Include it in your layout application.html.erb



        <%= custom_bootstrap_flash %>


        And in your action.js.erb show toast message using toaster method



        toastr.success('Success.')
        toastr.error('error')


        I hope this what you are looking.






        share|improve this answer



























          2














          2










          2









          In rails there is one gem for ajax call flash messages
          Toaster gem



          Create one method in application_helper.rb



          def custom_bootstrap_flash
          flash_messages = []
          flash.each do |type, message|
          type = 'success' if type == 'notice'
          type = 'error' if type == 'alert'
          text = "
          <script>
          $(function ()
          toastr.#type("#message");
          );
          </script>
          "
          flash_messages << text.html_safe if message
          end
          flash_messages.join("n").html_safe
          end


          Include it in your layout application.html.erb



          <%= custom_bootstrap_flash %>


          And in your action.js.erb show toast message using toaster method



          toastr.success('Success.')
          toastr.error('error')


          I hope this what you are looking.






          share|improve this answer













          In rails there is one gem for ajax call flash messages
          Toaster gem



          Create one method in application_helper.rb



          def custom_bootstrap_flash
          flash_messages = []
          flash.each do |type, message|
          type = 'success' if type == 'notice'
          type = 'error' if type == 'alert'
          text = "
          <script>
          $(function ()
          toastr.#type("#message");
          );
          </script>
          "
          flash_messages << text.html_safe if message
          end
          flash_messages.join("n").html_safe
          end


          Include it in your layout application.html.erb



          <%= custom_bootstrap_flash %>


          And in your action.js.erb show toast message using toaster method



          toastr.success('Success.')
          toastr.error('error')


          I hope this what you are looking.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 28 at 5:05









          ashvinashvin

          1,4928 silver badges23 bronze badges




          1,4928 silver badges23 bronze badges


























              1
















              The flash method on rails shows only on successful redirect. If you want to show the message on the same page it would be better to show the message using JS.






              share|improve this answer





























                1
















                The flash method on rails shows only on successful redirect. If you want to show the message on the same page it would be better to show the message using JS.






                share|improve this answer



























                  1














                  1










                  1









                  The flash method on rails shows only on successful redirect. If you want to show the message on the same page it would be better to show the message using JS.






                  share|improve this answer













                  The flash method on rails shows only on successful redirect. If you want to show the message on the same page it would be better to show the message using JS.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 28 at 6:03









                  Nikhil KumarNikhil Kumar

                  1237 bronze badges




                  1237 bronze badges































                      draft saved

                      draft discarded















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390312%2frails-flash-on-ajax-request-without-reload%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현