2014-10-05 87 views
-1

我有一個靜態控制器和用戶控制器。Ruby on Rails ajax 500語法錯誤

我有一個用戶配置文件頁面,我想用ajax來替換現有的代碼。在頁面上,我有一個編輯按鈕,可以轉到用戶控制器中的編輯操作並進入edit.js.erb視圖。在這個觀點,我有以下代碼:

$("<%= escape_javascript(render 'static/profile_form'%>").appendTo("#ajax-profile"); 

每次我試圖使它的工作,我得到這樣的錯誤:

Started GET "https://stackoverflow.com/users/edit" for 127.0.0.1 at 2014-10-05 17:42:19 -0400 

Started GET "https://stackoverflow.com/users/edit" for 127.0.0.1 at 2014-10-05 17:42:19 -0400 

Processing by UsersController#edit as JS 

Processing by UsersController#edit as JS 

    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]] 

    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]] 

    Rendered users/edit.js.erb (1.0ms) 

    Rendered users/edit.js.erb (1.0ms) 
Completed 500 Internal Server Error in 61ms 
Completed 500 Internal Server Error in 61ms 

SyntaxError (/Users/UserNameRedacted/Desktop/magazine_alive/yourcoolapp/app/views/users/edit.js.erb:1: syntax error, unexpected tLABEL, expecting ')' 
...tput_buffer.append=(j (render: 'static/profile_form');@out... 
...        ^): 
    app/views/users/edit.js.erb:1: syntax error, unexpected tLABEL, expecting ')' 



SyntaxError (/Users/UserNameRedacted/Desktop/magazine_alive/yourcoolapp/app/views/users/edit.js.erb:1: syntax error, unexpected tLABEL, expecting ')' 
...tput_buffer.append=(j (render: 'static/profile_form');@out... 
...        ^): 
    app/views/users/edit.js.erb:1: syntax error, unexpected tLABEL, expecting ')' 

我一直在通過控制檯創建我的所有用戶,因此我還沒有填寫任何用戶的表單。這可能是一個問題嗎?沒有要編輯的信息?我不想要新的/創建視圖,因爲我有一個用戶登錄頁面。

在我的用戶控制我有:

class UsersController < ApplicationController 

    def create 

    @user= User.find_by(session[:user_id]) 

    @user.create(user_params) 

    end 

    def edit 

    @user= current_user 

    end 

    def update 

    @user=current_user 

    @user.update(user_params) 

    end 

private 

    def user_params 

    params.require(:user).permit(:first_name,:last_name,:email,:bio,:avatar) 

    end 

end 

靜態控制器

def profile 

    @my_articles = Article.all.order('created_at desc').where(:user_id => current_user).limit(5); 

    @user= current_user 

end 
+0

顯示我們'應用程序/視圖/用戶/ edit.js.erb'。根據錯誤的問題是 – JTG 2014-10-05 22:15:33

+0

$('#ajax-profile')。prepend(「<%= j(render:'static/profile_form'%>」); – Kohl 2014-10-05 22:21:43

回答

0

所以你的錯誤說:

app/views/users/edit.js.erb:1: syntax error, unexpected tLABEL, expecting ')' 

它期待一個右括號。如果你看看你的代碼:

$('#ajax-profile').prepend(" <%= j (render: 'static/profile_form' %> "); 

你會注意到你有不匹配的括號。

我懷疑你需要的代碼更改爲這個

$('#ajax-profile').prepend(" <%= j (render: 'static/profile_form')%> ");