我想讓我的AJAX調用與rails 3工作,但我很希望困惑。 我有一個索引頁,其中列出了一個小表中的所有對象,並在其後面有一個詳細鏈接。該HAML頁面看起來是這樣的:AJAX調用不工作在rails 3中
%h1
Step 1: Create/upload your CV
#europasslogo
%table.index
%tr
%th.bottom_border= 'CV ID'
%th.bottom_border= 'CV Title'
%th.bottom_border= 'Links'
- @cvs.each do |cv|
%tr
%td.cell= cv.id
%td.cell= cv.name
%td.cell
= link_to 'Matching', match_resume_url(cv.id)
= link_to "Details", cv_path(cv.id), {:remote => true, :method => :get}
.clear
#details
它,當用戶點擊「詳細信息」鏈接,響應需要的想法在底部的#details DIV來顯示。這是非常基本的,因爲我只想在該div中輸出一個CV作爲純文本。
在我的控制器中我已經創建了以下內容:
class CvsController < ApplicationController
def new
@cvs = Cv.all
end
def show
cv = Cv.find params[:id]
@data = IO.readlines("public/cvs/#{cv.id}.xml", "").to_s
render :layout => false
end
def match
@cv = Cv.find params[:id]
@language = Language.find_or_create_by_code :en
@vacancies = Vacancy.joins(:vacancy_occupations).where('vacancy_occupations.concept_id' => @cv.occupations.collect{|o| o.id}).uniq
end
end
獲取的簡歷和讀取匹配的文件。迄今爲止沒有特別的。 當我打開網頁,我點擊鏈接,我看到了AJAX請求擊中服務器,像這樣:
Started GET "/cvs/2" for 192.168.33.82 at Fri May 13 00:55:49 -0700 2011
Processing by CvsController#show as JS
Parameters: {"id"=>"2"}
Cv Load (0.1ms) SELECT `cvs`.* FROM `cvs` WHERE `cvs`.`id` = 2 LIMIT 1
Rendered cvs/show.js.haml (2.1ms)
Completed 200 OK in 69ms (Views: 4.3ms | ActiveRecord: 0.1ms)
所以我知道AJAX調用由服務器接收併產生響應。使用Firebug,我可以看到服務器在控制檯中的響應:
$('details').html(<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://europass.cedefop.europa.eu/xml/cv_en_GB.xsl" type="text/xsl"?>
<europass:learnerinfo xsi:schemaLocation="http://europass.cedefop.europa.eu/Europass/V2.0 http://europass.cedefop.europa.eu/xml/EuropassSchema_V2.0.xsd" xmlns:europass="http://europass.cedefop.europa.eu/Europass/V2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" locale="en_GB">
<docinfo>
<issuedate>2011-05-05T11:31:55+02:00</issuedate>
<xsdversion>V2.0</xsdversion>
<comment>Automatically generated Europass CV</comment></docinfo>
<prefs>
<field name="step1.firstName" before="step1.lastName"></field>
<field name="step1.addressInfo" keep="true"></field>
<field name="step1.telephone" keep="false"></field>
<field name="step1.mobile" keep="false"></field>
<field name="step1.fax" keep="false"></field>
<field name="step1.email" keep="true"></field>
<field name="step1.nationality" keep="true"></field>
<field name="step1.birthDate" keep="true" format="/numeric/long"></field>
<field name="step1.gender" keep="true"></field>
<field name="step1.photo" keep="false"></field>
<field name="step1.application.label" keep="true"></field>
<field format="/numeric/long" name="step3List[0].period"></field>
<field name="step3List" keep="true" before="step4List"></field>
<field keep="false" name="step3List[0].company.sector.label"></field>
<field keep="false" name="step3List[0].company.addressInfo"></field>
<field keep="false" name="step3List[0].company.name"></field>
<field keep="true" name="step3List[0].activities"></field>
<field keep="true" name="step3List[0].position.label"></field>
<field name="step4List" keep="false"></field>
<field name="step5.motherLanguages" keep="false"></field>
<field name="step5.foreignLanguageList" keep="false"></field>
<field name="step6.socialSkills" keep="false"></field>
<field name="step6.organisationalSkills" keep="false"></field>
<field name="step6.technicalSkills" keep="false"></field>
<field name="step6.computerSkills" keep="false"></field>
<field name="step6.artisticSkills" keep="false"></field>
<field name="step6.otherSkills" keep="false"></field>
<field name="step6.drivingLicences" keep="false"></field>
<field name="step7.additionalInfo" keep="false"></field>
<field name="step7.annexes" keep="false"></field>
<field name="grid" keep="false"></field></prefs>
<identification>
<firstname>Wouter</firstname>
<lastname>ESCOmatch</lastname>
<contactinfo>
<address>
<addressLine>Haachtstesteenweg</addressLine>
<municipality>Kampenhout</municipality>
<postalCode>1910</postalCode>
<country>
<label>BElgium</label></country></address>
<telephone></telephone>
<fax></fax>
<mobile></mobile>
<email>[email protected]</email></contactinfo>
<demographics>
<birthdate>1975-01-26</birthdate>
<gender>M</gender>
<nationality>
<label>belgium</label></nationality></demographics></identification>
<application>
<code>71110</code>
<label>Miner</label></application>
<workexperiencelist>
<workexperience>
<period>
<from>
<year>2000</year>
<month>--02</month>
<day>---02</day>
</from>
<to>
<year>2004</year>
<month>--02</month>
<day>---02</day>
</to>
</period>
<position>
<code>82121</code>
<label>Quartermaster staff, military</label>
</position>
<activities>
process control
diving
military
</activities>
<employer>
<name></name>
<address>
<addressLine></addressLine>
<municipality></municipality>
<postalCode></postalCode>
<country>
<label></label>
</country>
</address>
<sector>
<label></label>
</sector>
</employer>
</workexperience>
</workexperiencelist>
<languagelist>
<language xsi:type="europass:mother">
<label> </label></language></languagelist>
<skilllist>
<skill type="social"></skill>
<skill type="organisational"></skill>
<skill type="technical"></skill>
<skill type="computer"></skill>
<skill type="artistic"></skill>
<skill type="other"></skill>
<structured-skill xsi:type="europass:driving"></structured-skill></skilllist>
<misclist>
<misc type="additional"></misc>
<misc type="annexes"></misc></misclist></europass:learnerinfo>);
對不起,但文件相當大。所以我看到很多Javascript beeing生成。 show.js.haml文件如下所示:
= "$('details').html(#{@data});"
我做錯了什麼?我不是很擅長這個JavaScript的東西,但我所問的不是那麼難嗎? 所有我想要的是,當我點擊鏈接時,@data變量中的所有內容都放置在我的詳細信息div中...
沒有這樣的函數.to_html。而且響應如何格式化並不重要。我只是想得到什麼是返回可見我的div元素 – 2011-05-13 08:16:53
什麼是'細節'?它是一個類還是一個id?它似乎是錯誤的選擇。如果'details'是一個id,那麼嘗試$('#details')如果它是一個類,請嘗試$('。details')。我沒有進入HAML,但你應該可以添加一個id爲':id =>'details' – 2011-05-13 08:21:50
這是一個ID。按照您在show.js.haml文件中所述更改它,但仍然沒有任何內容 – 2011-05-13 08:24:00