我开始使用 hotwire/turbo 并已设置好一切。Turbo 流工作正常。
在index.html.erb中:
<h1>Tasks</h1>
<%= turbo_stream_from "tasks" %>
<div id="tasks">
<% @tasks.each do |task| %>
<%= render task %>
<% end %>
</div>
Run Code Online (Sandbox Code Playgroud)
在 _task.html.erb 内
<%= turbo_frame_tag dom_id(task) do %>
<%= link_to 'Edit', edit_task_path(task) %>
<%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
<hr>
<% end %>
Run Code Online (Sandbox Code Playgroud)
当按预期按下销毁按钮时,rails 控制台会显示:
Started DELETE "/tasks/41" for ::1 at 2021-12-14 18:40:32 +0000
Processing by TasksController#destroy as TURBO_STREAM
Run Code Online (Sandbox Code Playgroud)
但是当按下编辑按钮时,rails 控制台会显示:
Started GET "/tasks/41/edit" for ::1 at …Run Code Online (Sandbox Code Playgroud) 我一直在运行 rubocop 并遇到了Lint/Void: Literal used in void context offence。
以下代码:
routes_and_postcodes.each do |hash|
2..7.each do |i|
route = Route.where(name: hash[:route]).first if postcode.delete(" ").first(i) == hash[:postcode].delete(" ")
end
end
Run Code Online (Sandbox Code Playgroud)
我已经阅读了文档,但仍然无法理解问题是什么。在上下文中,完整的代码在这里:
def choose_own_van_route(postcode)
route = nil
routes_and_postcodes = []
Route.all.each do |r|
route_postcodes = r.postcode_array
route_postcodes.each do |pc|
routes_and_postcodes << { postcode: pc, route: r.name }
end
end
routes_and_postcodes.each do |hash|
2..7.each do |i|
route = Route.where(name: hash[:route]).first if postcode.delete(" ").first(i) == hash[:postcode].delete(" ")
end
end
route || Route.create(cut_off_time_mins_since_midnight: …Run Code Online (Sandbox Code Playgroud) 我已将 Rails 控制器设置为使用 slugs 而不是 ids 来识别“拟合”(用于显示/编辑操作),因此我可以使用这样的 url 来访问显示页面:
http://localhost:3000/datasheets/slug-attribute-of-fiting
在routes.rb
resources :datasheets, only: [:index, :show, :edit], param: :slug
Run Code Online (Sandbox Code Playgroud)
在 datasheets_controller.rb 中
def show
fitting = Fitting.where(slug:(params[:slug])).first
pdf = DatasheetPdf.new(fitting)
send_data pdf.render, filename: "#{fitting.slug}-datasheet-#{Date.today}.pdf", type: 'application/pdf', disposition: 'inline'
end
Run Code Online (Sandbox Code Playgroud)
对于上面提到的 url 处的正常显示视图,这一切都正常工作,但是在添加 ActiveAdmin gem 时会出现问题(它会生成自己的控制器,据我所知这些控制器无法访问)。如果您尝试在活动管理仪表板中编辑“拟合”,您将被定向到http://localhost:3000/admin/fittings/slug-attribute-of-fiting/edit,它返回以下错误:
ActiveRecord::RecordNotFound in Admin::FittingsController#edit
Couldn't find Fitting with 'id'=slug-attribute-of-fiting
Run Code Online (Sandbox Code Playgroud)
鉴于我无法访问活动的管理控制器(对吗?)我如何拦截 slug 并使用它来提供 id。
谢谢
我有2个模型:Sophead和SopheadIssue。
SopheadIssue belongs to Sophead,
Sophead has many SopheadIssues (可选的)。
我想在Sophead模型上为与2个条件中的EITHER匹配的Sopheads创建范围:
目前,我尝试了以下方法:
scope :no_issue, -> { joins(:sophead_issues).where.not("active = ?", true) }
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它缺少没有任何SopheadIssues的Sophead。
任何帮助,不胜感激。
非常感谢!