开发者

Help with executing javascript through ajax via ror

I'm quite new to Ruby on Rails and need to some help building and executing Ajax code.

At present my webapp has an invite function that does not work very well. I'll share the code in a minute, but it might be helpful describing what I have currently, and what I'm attempting to do. Therefore, at present the way invites work is as follows:

There are 2 buttons to invite people to a user-created page. one button for people who are not yet registered users on the site and another button for people who are registered on the site. This current setup requires the user who is inviting people to know whether or not the person they are inviting is a registered user (as you can tell it's a pretty big flaw). Here is what the code looks like:

this code is located in the View that executes the ajax (I'm using HAML by the way, and can post more code if necessary):

.invitepeople_buttons
  = button_to_function 'Add Existing User', 'insert_add_existing_member_form()', :class => "submit"
  = button_to_function 'Add New User', 'insert_invitation_membership_form()', :class => "floatright submit"

this code is in the application.js file:

var insert_invitation_membership_form = function(){
  $('#membership_form_footer').before("<h2>Add New User</h2>")
  var template = $("#invitation_membership_form").clone()
  $("#membership_form_footer").before( template.html().replace(/new_membership/g, new Date().getTime()) )
}

var insert_add_existing_member_form = function(){
  $('#membership_form_footer').before("<h2>Add Existing User</h2>")
  var template = $("#add_existing_member_form").clone()
  $("#membership_form_footer").before( template.html() )
  $('.existing_member_autocomplete').autocomplete({ 
    source: users_for_autocomplete,
    select: function(event, ui){
      insert_membership_form(ui.item)
    }
  })
} 

Here is what I'd like to implement: I would like to create a textfield and a button with a new function. The user would input the email of the person they would like to invite into the new textfield and then click the new button.

This new button would trigger a new function: The new function would compare the email in the textfield to the emails of registered users (probably using the where sql function) and depending on whether or not the user exits (ie: email in textbox matches and email in the database) it would execute one of the already existing functions via if statement.

Unfortunately I don't know how to make this happen correctly. Here is what I have at the moment, but it's not working:

In the view:

.invitepeople_buttons

      = text_field "member_email", "" 
      = button_to_function 'Add Member', 'test_email_for_membership()', :class => "submit"

In the application.js:

var test_email_for_membership = function(){
    if user.where(:em开发者_JS百科ail => member_email).exists? = true
      $('#membership_form_footer').before("<h2>Add Existing User</h2>")
      var template = $("#add_existing_member_form").clone()
      $("#membership_form_footer").before( template.html() )
      $('.existing_member_autocomplete').autocomplete({ 
        source: users_for_autocomplete,
        select: function(event, ui){
          insert_membership_form(ui.item)
        }
      })
    else
      $('#membership_form_footer').before("<h2>Add New User</h2>")
      var template = $("#invitation_membership_form").clone()
      $("#membership_form_footer").before( template.html().replace(/new_membership/g, new Date().getTime()) )
}

Any help would be very much appreciated. I'm grateful for you taking the time to read my question, and if you need clarification on anything (I know that I probably did a poor job explaining it in sufficient detail) please do not hesitate to ask! Thank you again!


It seems like you are mixing Rails code in application.js and that's a no no.
You should trigger an Ajax request (easy with jQuery.ajax) to a controller to check if the user already exists and then based on your response execute your function of choice.

You should prepare your controller for a JS request with a respond_to block. Possible solution:

respond_to do |format|
  format.js do
    if User.where('email = ?', params[:email]).first
      render :text => "true"
    else
      render :text => "false"
    end
  end
end

You should also check Unobtrusive JavaScript Railscast.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜