2017-03-03 70 views
0

我有一個:remote => true按預期工作。我在js迴歸中遇到了麻煩。Rails |過程json返回在js文件

def myfunc 
... 
    respond_to do |format| 
       if !response.nil? 
        r = response.to_hash 
        format.js { render :json => {:IsLoggedIn => true, :response => r }} 
        format.html 
       else 
        @error = "Try again" 
        format.js { render :json => {:IsLoggedIn => false, :response => @error }} 
        format.html 
       end 
      end 

end 

然後在myfunc.js.erb我怎樣才能得到IsLoggedIn & response值?

回答

0

根據你的情況,你只在js中使用這個函數。所以寫它象下面這樣:

def myfunc 
... 
    @r = response 
end 

在你myfunc.js.erb,寫出如下:

<% if @r.present? %> 
    {:IsLoggedIn => true, :response => @r } 
<% else %> 
    {:IsLoggedIn => true, :response => "Try Again" } 
<% end %> 

@r is available in js.erb , so you set it your html div like $("#yourdivid").val("<%= @r.id" %>")