2014-03-31 29 views
0

我有兩個div容器。一個div有一個表單,另一個div有很多我想隱藏的敏感內容。它們都是用bootstrap構建的。獲取語法錯誤時做一個阿賈克斯帖子形式

這個想法是,第一種形式需要一個人輸入密碼,然後這應該觸發一個Ajax後調用後端來檢查密碼是否正確,如果是,第二個容器將被泄露。

在我的routes.rb

post 'users/trial_signup_validation', to: 'users#trial_signup_validation' 

在我users_controller.rb

def trial_signup_validation 
    @password = params['password'] 
    if @password == 'sales' 
     render :json => { "success" : "true" } 
    else 
     render :json => { "success" : "false" } 
    end 

該方法從阿賈克斯後採取了對象,並檢查它,然後返回一個JSON真或假迴風景。

下面是這個視圖

<script type="text/javascript"> 

$("#protectivepanel").submit(function(event) { 

    $.ajax({ 
     type: "POST", 
     url: "https://stackoverflow.com/users/trial_signup_validation", 
     data: $("#protectivepanel").serialize(), 
     success: function(data){ 
      return data.success 
     } 
    }); 
    event.preventDefault() 
}); 


$(document).ready(function(){ 
    $("#the_submit_button").click(function(e){ 
     e.preventDefault(); 
     if (data.success){ 
      $("#protectivepanel").hide() 
      $("#salespanel > div").removeClass("hide") 
     } 
     else { 
      $("#protectivepanel").show() 
     } 
    }); 
    }); 
</script> 

我的第一種形式是這樣的

<div class="container" id="protectivepanel"> 
    <form role="form"> 
    <div class="form-group"> 
     <label for="representativepassword">Representative's Password</label> 
     <input type="text" class="form-control" id="representativepassword" placeholder=" Enter Password"> 
     <button type="submit" class="btn btn-default" id="the_submit_button">Submit</button> 
    </div> 
    </form> 
</div> 

還當我運行rails s,我越來越指向另一種方法在同一個控制器,這使得語法錯誤對我沒有意義。

SyntaxError at /users/trial_signup 
syntax error, unexpected ':', expecting => 

這個方法在我開始處理jQuery表單發佈之前完美工作。

+0

是你的javascript評論的前綴'#'?這將是一個語法錯誤。 – Seiyria

+0

哦,不,我只是在stackoverflow上添加評論的東西,我的js是壞的。這是紅寶石的方式.... – Jngai1297

+0

對,我沒有看到立即在底部的語法錯誤,所以我不知道它在哪裏。 – Seiyria

回答

1

更新trial_signup_validation如下:

def trial_signup_validation 
    @password = params['password'] 
    if @password == 'sales' 
     render :json => { "success" => "true" } ## Use hash rocket instead of : 
    else 
     render :json => { "success" => "false" } ## Use hash rocket instead of : 
    end 

注:

"success" and "true"Strings,所以你將不得不使用hash rocket(=>)表示哈希語法,你不能使用:因爲沒有符號。

+0

我不明白,這種方法和我的完全一樣嗎? – Jngai1297

+0

@ Jngai1297在我的回答 –

+0

中閱讀筆記太棒了!修復了我的語法錯誤,但我的表單不完全正常工作?現在當我在輸入框中輸入'sales'時,第二個容器不顯示。 – Jngai1297