开发者

:confirm and :method not working in rails button_to

i am having a problem with my rails 3 app

In my view i have

<%= button_to("Accept Post", {}, {:confirm => "Are you sure?", :method => :accept_question, :remote => true}) %>

The generated html is

<form method="post" action="/questions/show/1" data-remote="true" class="button_to">
    <div>
        <input data-confirm="Are you sure?" type="submit" value="Accept Post" />
        <input name="authenticity_token" type="hidden" value="Rv1CbqkE+61Fn4wb836eOENjyGkNpzzRrwMTywLZPf0=" />
    </div>
</form>

my gem file is loading this

gem 'jquery-rails'

The following .js files are appearing in the generated html

<script src="/javascripts/jquery.js?1309961714" type="text/javascript"></script> 
<script src="/javascripts/jquery-ui.js?1309961714" type="text/javascript"></script> 
<script src="/javascripts/jquery-ui.min.js?1305044364" type="text/javascript"></script> 
<script src="/javascripts/jquery.min.js?1309961714" type="text/javascript"></script> 
<script src="/javascripts/jquery.tools.min.js?1309338309" type="text/javascript"> </scipt>
<script src="/javascripts/rails.js?1309961714" type="text/javascript"></script> 
<script src="/javascripts/application.js?1309424326" type="text/javascript"></script> 

I have seen other posts on this topic, but nothing works, i get no error, just a redirect to the same page, without b开发者_运维技巧eing prompted. I have other bits of code that use jquery, which work fine. the problem i have is that the confirm and method does not prompt be before continuing. What is a good way to implement jquery into an app, i have tried to include jrails, however all my other code breaks thanks for the help


I've seen this problem before, and I just found a workaround, using pure JavaScript. If you are in a hurry this might just do for now.

You can try this and you get the same effect:

<%= button_to("Accept Post", {}, {:onclick => "return confirm('Are you sure?')", :method => :accept_question, :remote => true}) %>


I was just having this problem and realized that I didn't have the jquery_ujs.js file in my public directory (rails 3.0.x).

The :confirm => "Message..." functionality requires the unobtrusive scripting adapter for jQuery if you decided to scrap Prototype for jQuery. This adapter can be installed by adding gem "jquery-rails" to your Gemfile, running bundle install and then rails generate jquery:install (add --ui if you want to use jquery_ui) from terminal. Then add javascript_include_tag :defaults to your application layout and you're good.

The generate command automatically fetches the jquery and jquery_ujs files for you (and jquery_ui files if you ask for them). My mistake was that I manually added the jquery and jquery_ui files, but didn't have the jquery_ujs file. So all of the jquery functionality was working, but the :confirm => "Message..." was not. Once I installed it, everything worked flawlessly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜