[問題] Rails內建的Ajax傳導問題
看不懂$ajax的寫法,所以用rails內建的方式使用
卻不知道哪裡出問題,看terminal也有顯示有傳正確的id進去,卻無法更改..
照著這個影片實作的 http://happycasts.net/episodes/66
我先說明一下我的流程,就是首頁顯示所有Toy的列表,
首頁最後有個render顯示當前current的toy (c_toy)
而會依照點選的toy.name改變最下面的current.toy的id跟name
而黃色是我覺得有出問題的地方...
==toys_controller.rb //params有id就設為該id 無則使用第一個
def index
@toys = Toy.all
if params[:toy_id]
@c_toy = Toy.find(params[:toy_id])
else
@c_toy = Toy.first
end
respond_to do |format|
format.html
fotmat.js
end
end
==index.html.erb (部分) 產生一整排連結,最後render
<% toys.each do |toy| %>
<% link_to toy.name , toy , :remote => true %>
<% end %>
<%=render 'ida' , :c_toy => @c_toy %>
==index.js.erb //用於replace render
$('.ida').replaceWith('j <%=render 'ida',:c_toy => @c_toy %>')
==_ida.html.erb //顯示當前toy的id 及name
<div class="ida">
<%= @c_toy.id %>
<%= @c_toy.name %>
</div>
照理來說應該是會成功,隨著我按哪個toy的名字下面就會render toy的名字出現
但是目前的情況就是,他卡在第一個toy,也就是controller第一次設定之後
後面就抓不到丟進去的id了...
請板上的高手解惑一下,拜託了...謝謝...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.46.128
推
12/15 23:56, , 1F
12/15 23:56, 1F
→
12/15 23:56, , 2F
12/15 23:56, 2F
抱歉...看著另一個螢幕用手打竟然漏打了...以修正,謝謝!
→
12/16 00:53, , 3F
12/16 00:53, 3F
→
12/16 08:40, , 4F
12/16 08:40, 4F
→
12/16 08:49, , 5F
12/16 08:49, 5F
推
12/16 11:46, , 6F
12/16 11:46, 6F
謝謝指正,但修改後仍是無法動作..
以下是我點選之後 terminal出現的訊息
Started GET "/toys/45" for 127.0.0.1 at 2013-12-16 12:52:05 +0800
Processing by ToysController#show as JS
Parameters: {"id"=>"45"}
Toy Load (0.6ms) SELECT "toys".*
FROM "toys" WHERE "toys"."id" = $1 LIMIT 1 [["id", "45"]]
Rendered toys/show.html.erb within layouts/application (0.5ms)
Completed 200 OK in 18ms (Views: 15.7ms | ActiveRecord: 0.6ms)
parameters 有取得到id,但無法更改ˊˋ
推
12/16 13:04, , 7F
12/16 13:04, 7F
→
12/16 13:05, , 8F
12/16 13:05, 8F
→
12/16 13:05, , 9F
12/16 13:05, 9F
感謝,但是他卻跑出很神秘的找不到"id"
Started GET "/toys/45" for 127.0.0.1 at 2013-12-16 14:26:01 +0800
Processing by ToysController#show as JS
Parameters: {"id"=>"45"}
Toy Load (0.6ms) SELECT "toys".* FROM "toys"
WHERE "toys"."id" = $1 LIMIT 1 [["id", "45"]]
Rendered toys/_ida.html.erb (1.4ms)
Rendered toys/show.js.erb (3.7ms)
Completed 500 Internal Server Error in 11ms
ActionView::Template::Error (undefined method `id' for nil:NilClass):
1: <div class="ida">
2: <%= @c_toy.id %>
3: <%= @c_toy.name %>
3: </div>
app/views/revos/_ida.html.erb:2:
in `_app_views_toys__ida_html_erb__1527320142082688770_70320262549140'
app/views/revos/show.js.erb:2:
in `_app_views_toys_show_js_erb__3810931547537904217_70320251433280'
app/controllers/toys_controller.rb:29:in `show'
改程式改到頭有點暈...感謝板上高手...初學者真的有點糟,沒想到Ajax這麼難實現
推
12/16 15:40, , 10F
12/16 15:40, 10F
→
12/16 15:41, , 11F
12/16 15:41, 11F
馬上來,https://github.com/viflin/tezarail
進入 http://localhost:3000/toys 就有三個toy,和最下面有問題的顯示地方
應該要點選列表上的“Obama” 然後下面一開始預設的Boozer就會變成Obama...
感謝......
推
12/16 17:13, , 12F
12/16 17:13, 12F
→
12/16 17:13, , 13F
12/16 17:13, 13F
→
12/16 17:14, , 14F
12/16 17:14, 14F
→
12/16 17:15, , 15F
12/16 17:15, 15F
→
12/16 17:15, , 16F
12/16 17:15, 16F
推
12/16 17:26, , 17F
12/16 17:26, 17F
→
12/16 17:27, , 18F
12/16 17:27, 18F
→
12/16 17:27, , 19F
12/16 17:27, 19F
謝謝!已經merge了! 感謝解惑! 困擾了我好久==
初學者真的太多東西不懂...rails真的頗困難(對於只寫過簡單html的我來說...)
而且不像IDE之類的會告訴你哪裡錯了...
慢慢在研究到底他背後怎麼傳遞的...不知道用$ajax的那種寫法是怎麼處理XD
謝謝johnlinvc ,hellolucky 跟|TERRA|板友,感謝你們
※ 編輯: Vinn 來自: 61.230.45.210 (12/16 19:37)
Ruby 近期熱門文章
PTT數位生活區 即時熱門文章
-1
12