日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rails4 ajax 例子,Ajax和Rails 4:创建实例变量并更新视图而不刷新

發(fā)布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rails4 ajax 例子,Ajax和Rails 4:创建实例变量并更新视图而不刷新 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我有一個部分的coaching_notes索引和一個用于創(chuàng)建備注的表單。我想創(chuàng)建一個教練筆記,并進(jìn)行部分更新而不刷新頁面。我收到一個未知動作錯誤:CoachingNotesController無法找到'show'動作。如果我添加顯示操作,我會收到缺少的模板錯誤。當(dāng)我嘗試從控制器中刪除format.html部分時,我也遇到了未知格式錯誤。Ajax和Rails 4:創(chuàng)建實例變量并更新視圖而不刷新

我在Rails文檔之后模擬了我的代碼,并回顧了一堆類似的情況,但無法使其工作。

任何幫助非常感謝!

/views/coaching_notes/_index.html.erb

/views/coaching_notes/_coaching_note.html.erb

/視圖/ coaching_notes /創(chuàng)建。 js.erb

$("").appendTo("#coaching_notes");

/controllers/coaching_notes_controller.rb

def create

@coaching_note = CoachingNote.new(coaching_note_params)

respond_to do |format|

if @coaching_note.save

format.html { redirect_to @coaching_note, notice: 'note was successfully created.' }

format.js {}

format.json { render json: @coaching_note, status: :created, location: @coaching_note }

else

format.html { render action: "new" }

format.json { render json: @coaching_note.errors, status: :unprocessable_entity }

end

end

的routes.rb

resources :coaching_notes

UPDATE 17年7月15日 - 我已經(jīng)根據(jù)修改的情侶檔@ Cira的回答:

/views/coaching_no TES/_index.html.erb

true, :method => :delete, :data => {:confirm => "Are you sure?"} %>

// newly added

$('#submit_note').on("click", function() {

var u = $('#user').val()

var m = $('#meet').val()

var a = $('#arch').val()

var c = $('#content').val()

$.ajax({

url: "/coaching_notes/create",

type: "POST",

data: {

user_id: u,

meeting_id: m,

archive: a,

content: c

},

dataType: "json",

success: function(info) {

$('#coaching_notes').data("html", info)

}

});

})

(,我不知道的主要部分是成功的功能與數(shù)據(jù)屬性)

/controllers/coaching_notes_controller.rb

def create

@coaching_note = CoachingNote.new(coaching_note_params)

@html = render_to_string :partial => "coaching_notes/index"

respond_to do |format|

if @coaching_note.save

format.html

format.json { render json: {"html" => @html} }

else

format.html { render action: "new" }

format.json { render json: @coaching_note.errors, status: :unprocessable_entity }

end

end

兩件事: 1.我的成功功能在ajax中看起來是否正確? 2.我需要將if @coaching_note.save放在控制器中嗎?

的錯誤,現(xiàn)在我得到的是“NoMethodError在CoachingNotes#創(chuàng)建 - 未定義的方法`每次”的零:NilClass'強(qiáng)調(diào)此行/ index.html中

然后我得到coaching_notes“失蹤模板指導(dǎo)/索引'

我覺得我們正在接近!任何幫助表示贊賞!

2017-07-14

SVC

總結(jié)

以上是生活随笔為你收集整理的rails4 ajax 例子,Ajax和Rails 4:创建实例变量并更新视图而不刷新的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。