問題描述
Respond_to 沒有調用正確的格式 (Respond_to does not call the right format)
Rails 2.3.18
I have a Answer
controller with an index function like this :
def index
respond_to do |format|
format.js { }
format.all { redirect_to ... }
end
end
I have a anwers/index.js.erb
associated to this index action, for :js
format.
I just enter URL /answers/
in my browser to call the index function, and the result is surprising : this is the index.js
displayed as a text.
Something is wrong with this, but I don't understand what !
I log the mime type on the index action, and this is "text/html", corresponding to :html format. Why :js
format is called instead of :all
format ?
Regards
參考解法
方法 1:
Try using format.html
and format.js
without passing a block as shown below. I've never seen format.all used before.
def index
respond_to do |format|
format.js
format.html
end
end
方法 2:
Try this
def index
respond_to do |format|
format.html{redirect_to ...}
format.js {}
end
end
(by pierallard、Kyle Decot、Debadatt)