1

私はたくさんのことを試してきましたが、この問題に頭を悩ませることはできません。まず最初に、ファンシーボックス内に部分的なフォームがあります。

これは:* _ form.html.erb *

 <%= form_tag sessions_path , remote:true , id: "login_form" do %>
 <div class="field">
  <%= label_tag :email %>
  <%= text_field_tag :email, params[:email] %>
 </div>
 <div class="field">
  <%= label_tag :password %>
  <%= password_field_tag :password %>
 </div>
  <div class="actions"><%= submit_tag "Inloggen" %></div>
 <% end %>

application.jsのjQuery関数を使用して、このフォーム内にデータを投稿します

jQuery('#login_button').fancybox({
  type: 'ajax'
})


jQuery('#login_form').submit(function() {  
    var data = jQuery(this).serialize();

        $.getJSON('sessions/new.js?callback=?&variable=loginData', { 
        loginData: data 
        }, function(data) {

            if(data == 0){
                alert('Your account is not activated yet');
            }
            if(data == 1){
                alert('Password incorrect');
            }
            // login correct
            if(data == 2){
                 //$.fancybox.close();
                 window.parent.jQuery.fn.fancybox.close();
            } 
        });

    return false;
});

問題は、Iamがfancyboxにいるときにフィードバックが得られないことです。コンソールでXHRリクエストを確認すると、データが送信され、適切な応答が返されますが、ファンシーボックスが閉じず、アラートが表示されません。

最後になりましたが、私のセッションコントローラー

  def create
@user = User.find_by_email(params[:email])

#if there is a user with that e-mail, is it activated?
if @user
  if @user.user_level != '1' 
    @response = 1
  end
end

#check the login information with has_secure_password, user has to be activated
if @user && @user.authenticate(params[:password]) && @user.user_level == '1'
  session[:user_id] = @user.id
  @response = 2
else
  @response = 0
end

respond_to do |format|
  format.json { render :json => @response }
end

応答が2に等しい場合、どうすればファンシーボックスを閉じることができますか?そして、ファンシーボックス内からの入力が正しくないことをユーザーに示すにはどうすればよいですか?

4

1 に答える 1

1
respond_to do |format|
  format.js do
    if @response == 2
      render :js => "$('#fancybox-content').remove();"
    end
  end
end

または、create.js.erbを作成します。

# controller
respond_to do |format|
  format.js { }
end

# create.js.erb

<% if @response == 2 %>
  $('#fancybox-content').remove();
<% end %>

これは基本的なアプローチです。作成に失敗したときに別の動作が必要な場合は、コードを拡張してください。

于 2012-12-17T11:45:17.213 に答える