Ruby on Rails ajax 模态 window 返回 500 状态码
Ruby on Rails ajax modal window returning 500 status code
我正在尝试学习如何进行自定义模式 window,但我收到错误 500 状态代码。
我认为是这样的:views/dashboard/index.html.haml
.container
- @trips.each do |trip|
= link_to trip.id, quick_view_trips_path, remote: true
.quick-view{style: "display:none;"}
= render "trips/quick_view"
我不确定 quick_view_trips_path 是否正确。我试图在模态 window.
中显示行程内容
另外,我还有这些:
views/trips/quick_view.js.erb
$('body').append('<%= j render partial: "views/trips/quick_view" %>');
views/trips/_quick_view.html.haml
.root-container
= @trip.title
= @trip.image
= @trip.more_details
routes.rb
resources :trips do
collection do
get 'quick_view'
end
end
trips_controller.rb
def quick_view
respond_to do |format|
format.html # quick_view.html.erb
format.js # quick_view.js.erb
format.json { render json: @trip }
end
end
编辑:来自控制台的错误
哦,我刚刚意识到,如果我在控制台中单击link http://localhost:3000/trips/quick_view
,我能够看到错误:
它说 ActionView::MissingTemplate at /trips/quick_view
Missing partial views/trips/_quick_view with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}.
您可以尝试 render partial:
而不是 render
,因为您的观点是 _quick_view.html.haml
而不是 quick_view.html.haml
.container
- @trips.each do |trip|
= link_to trip.id, quick_view_trips_path, remote: true
.quick-view{style: "display:none;"}
= render partial: "trips/quick_view"
并且在 js 中不需要 views
作为
$('body').append('<%= j render partial: "trips/quick_view" %>');
如果你想获取特定模式的内容并像这里一样替换它,每次点击都会将每次旅行的快速视图添加到正文中,每次点击 link remote: true
it将调用控制器方法,它将以 js
的格式响应,因此它将调用 quick_view.js.erb
并通过获取部分来附加行程。
所以这里
link_to trip.id, quick_view_trips_path, remote: true
将调用 trips/quick_view.js.erb
,因为它告诉 ajax 请求由 remote: true
响应
所以在quick_view.js.erb
我们通过 jQuery 方法 append
在 body
中附加 partial
并且我们使用 j render
其中 j
或 escape_javascript
用于 "Escapes carriage returns and single and double quotes for JavaScript segments"(引自 Rails 文档)
我正在尝试学习如何进行自定义模式 window,但我收到错误 500 状态代码。
我认为是这样的:views/dashboard/index.html.haml
.container
- @trips.each do |trip|
= link_to trip.id, quick_view_trips_path, remote: true
.quick-view{style: "display:none;"}
= render "trips/quick_view"
我不确定 quick_view_trips_path 是否正确。我试图在模态 window.
中显示行程内容另外,我还有这些:
views/trips/quick_view.js.erb
$('body').append('<%= j render partial: "views/trips/quick_view" %>');
views/trips/_quick_view.html.haml
.root-container
= @trip.title
= @trip.image
= @trip.more_details
routes.rb
resources :trips do
collection do
get 'quick_view'
end
end
trips_controller.rb
def quick_view
respond_to do |format|
format.html # quick_view.html.erb
format.js # quick_view.js.erb
format.json { render json: @trip }
end
end
编辑:来自控制台的错误
哦,我刚刚意识到,如果我在控制台中单击link http://localhost:3000/trips/quick_view
,我能够看到错误:
它说 ActionView::MissingTemplate at /trips/quick_view
Missing partial views/trips/_quick_view with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :haml, :jbuilder]}.
您可以尝试 render partial:
而不是 render
,因为您的观点是 _quick_view.html.haml
而不是 quick_view.html.haml
.container
- @trips.each do |trip|
= link_to trip.id, quick_view_trips_path, remote: true
.quick-view{style: "display:none;"}
= render partial: "trips/quick_view"
并且在 js 中不需要 views
作为
$('body').append('<%= j render partial: "trips/quick_view" %>');
如果你想获取特定模式的内容并像这里一样替换它,每次点击都会将每次旅行的快速视图添加到正文中,每次点击 link remote: true
it将调用控制器方法,它将以 js
的格式响应,因此它将调用 quick_view.js.erb
并通过获取部分来附加行程。
所以这里
link_to trip.id, quick_view_trips_path, remote: true
将调用 trips/quick_view.js.erb
,因为它告诉 ajax 请求由 remote: true
所以在quick_view.js.erb
我们通过 jQuery 方法 append
在 body
中附加 partial
并且我们使用 j render
其中 j
或 escape_javascript
用于 "Escapes carriage returns and single and double quotes for JavaScript segments"(引自 Rails 文档)