3

I have ajax links on my view, on which I want to check password before they're sent to the actual action, as I am using devise controller, i am restricted to the use of specific password check. Following is the coffee script I wish to use for the validation.

.<%= link_to "CANCEL PAYMENT", { :action => "some_action", :info => n.id },class: "css_class",:remote => true %>

I am using the above link

I am looking for a following kind of code.

$("a.css_class").live "click", -> password_variable = prompt("Enter password", "password") if |ruby-code|current_user.valid_password?(password_variable)|ruby-code| true else alert "You entered wrong password" false 

How'll the ruby code work with coffee script mix.

    2 Answers 2

    4

    If it's a code for you static asset then it's obvious you won't be able to put some server-side dynamics into it. It'll be converted into a plain JavaScript-snippet and placed into your app's public folder.

    If you have your view called *.coffee then you already have done all the preparations. The views named this way will be automatically pre-processed with the ERb engine (via <%= ... %>):

    in views/some/thing.coffee:

    alert "Server's time is <%= Time.now %>" 
    3
    • I think, thenIt'll be better to redirect it to another action first and then call a function from there with render :js => "function();" what say ?CommentedNov 30, 2012 at 18:04
    • @NishutoshSharma I think of making some 'cancel' ajax-action. Then you can to access it via ajax after entering the password at the prompt's field. So, instead of making remote link_to you can make link_to_functionapi.rubyonrails.org/classes/ActionView/Helpers/…. It'll ask for the password and issue some non-GET request to cancel an order.
      – jdoe
      CommentedNov 30, 2012 at 18:18
    • 1
      In Rails 4, I found it necessary to name the file views/some/thing.coffee.erb so that the embedded Ruby statements would be processed. This is quite useful for embedding Rails 4's configuration stored in secrets.yml.
      – scarver2
      CommentedSep 1, 2014 at 19:50
    0

    I had the same doubt.

    Background:

    I am writing an ERP for my company. It uses messages in Spanish, English and Japanese.

    I am using coffeescript, haml, scss, NO ERB

    So, multilingual messages work fine in all my views, but, I added a .js library for replacing the browser's ugly dropbox with a nice combobox with dropdown list and it uses a hash for holding the messages in local languajes.

    So what I did was:

    _form.html.haml

    :coffeescript menssages_for_select2 [ "#{I18n.t('select.formatNoMatches')}" "#{I18n.t('select.formatInputTooShort')}" "#{I18n.t('select.formatInputTooLong')}" "#{I18n.t('select.formatSelectionTooBig')}" "#{I18n.t('select.formatLoadMore')}" "#{I18n.t('select.formatSearching')}" ] 

    I do this in the view, so I can have access to I18n library. If I try to access the I18n library inside a .js.coffee, it fails

    now, in

    mycode.js.coffee

    @mensajes_select2 = (txt) -> $.extend $.fn.select2.defaults, formatNoMatches: -> txt[0] formatInputTooShort: (input, min) -> txt[1] formatInputTooLong: (input, max) -> txt[2] formatSelectionTooBig: (limit) -> txt[3] formatLoadMore: (pageNumber) -> txt[4] formatSearching: -> txt[6] 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.