ruby-on-rails 相关问题

Ruby on Rails是一个用Ruby编写的开源全栈Web应用程序框架。它遵循流行的MVC框架模型,并以其“面向配置的约定”方法应用程序开发而闻名。

rails:如何在表单验证后使用 Turbo 刷新列表

我有点按照 Rails 教程来学习它,我想用 Turbo 来学习。 这是我的 index.html.erb 视图: <%= turbo_include_tags %> 注释 我有点按照 Rails 教程来学习它,我想用 Turbo 来学习。 这是我的index.html.erb视图: <%= turbo_include_tags %> <h1>Notes</h1> <turbo-frame id="new_note"> <%= form_with(model: Note.new, url: notes_path) do |form| %> <div> <%= form.label :content %> <%= form.text_area :content %> </div> <%= form.submit "Create Note" %> <% end %> </turbo-frame> <turbo-frame id="notes"> <div> <% @notes.each do |note| %> <div> <p><span><%= note.id %></span><%= note.content %></p> <small> <%= note.created_at %> </small> <%= link_to "Destroy", note_path(note), data: { turbo_method: :delete } %> </div> <% end %> <%= link_to "New note", new_note_path %> </div> </turbo-frame> 我的_list.html.erb模板: <% notes.each do |note| %> <div> <p><span><%= note.id %></span><%= note.content %></p> <small> <%= note.created_at %> </small> <%= link_to "Destroy", note_path(note), data: { turbo_method: :delete } %> </div> <% end %> 这是我的控制器: def index @notes = Note.all.order(created_at: :desc) end ... def new @note = Note.new end def create @note = Note.new(note_params) if @note.save @notes = Note.all # Reload all notes or fetch them as needed respond_to do |format| format.turbo_stream do render turbo_stream: turbo_stream.replace("notes", partial: "notes/list", locals: { notes: @notes }) end format.html { redirect_to notes_url } # Fallback in case JavaScript is disabled end else render :new end end ... 我想在单击提交按钮时创建新注释并更新列表。 我的代码中有两个问题。 第一个是验证后的表单不干净。我不知道最好的方法。 第二个是刷新列表。 它仅在我第一次单击提交按钮后有效一次。如果不刷新页面,我无法创建第二个笔记。另一个问题是新注释添加在列表的末尾而不是开头(因为我的列表是按created_at值排序的)。 你要turbo_stream.update而不是turbo_stream.replace,当你replace时你就失去了<turbo-frame id="notes">,这意味着第二次不起作用: render turbo_stream: turbo_stream.update("notes", partial: "notes/list", locals: { notes: @notes }) <%= turbo_include_tags %> 是一个已弃用的助手,不应使用。 <!-- app/views/notes/index.html.erb --> <div id="new_note"> <%= render "form", note: Note.new %> </div> <div id="notes"> <%= render @notes %> </div> <!-- app/views/notes/_note.html.erb --> <div id="<%= dom_id note %>"> <%= note.content %> </div> # app/controller/notes_controller.rb def create @note = Note.new(note_params) respond_to do |format| if @note.save format.turbo_stream do render turbo_stream: [ turbo_stream.update(:new_note, partial: "notes/form", locals: {note: Note.new}), turbo_stream.prepend(:notes, @note), ] end format.html { redirect_to notes_url(@note), notice: "Note was successfully created." } else format.turbo_stream do render turbo_stream: [ turbo_stream.update(:new_note, partial: "notes/form", locals: {note: @note}), ] end format.html { render :new, status: :unprocessable_entity } end end end

回答 1 投票 0

Rails:bash:./bin/rails:在渲染中部署时权限被拒绝

我开发了一个rails项目并将其推送到github上进行部署。但是我在渲染中部署该项目时遇到以下错误..尽管该项目在本地运行良好.. 对于

回答 1 投票 0

干验证。如何将参数发送到宏

json(简单示例): { “人”: { “生日”:“1990年10月10日” }, “成就”:{ “date_appr_r”:“2022-05-21” } } 我的骗局...

回答 1 投票 0

tailwind.css 未在 Heroku 的 Rails 7 项目中生成

我有一个使用 TailwindCSS 部署到 Heroku 的 Rails 7 项目,该项目在 rake asset:precompile 期间没有构建 tailwind.css,我不知道为什么。当我尝试访问该应用程序时,它崩溃了...

回答 4 投票 0

nil:NilClass 的未定义方法 `each'...为什么?

rails 指南示例,单击按钮保存帖子,控制台显示此消息: 于 2013-12-25 22:42:04 +0800 开始为 127001 发布“/posts” 由 PostsController#create 作为 HTML 参数处理...

回答 4 投票 0

GitHub Actions CI - Gem/Rails Engine - Ruby v3.3.1/Bundler 错误

我正在编写一个 Ruby gem/Rails 引擎 blacklight_allmaps,并使用 GitHub Actions 进行 CI。昨天,我的 Ruby v3.2 测试构建顺利通过,但我的 Ruby v3.3 测试开始失败,并出现一个奇怪的错误......

回答 1 投票 0

隐藏部分电子邮件地址

使用 ruby 例如隐藏电子邮件地址@符号之前的 4 个字符的最佳方法是什么 [email protected] = fake####@example.com 当我显示

回答 4 投票 0

`update` 上的 `after_commit` 回调不会触发

我已经定义了更新时的 after_commit 回调。它不会在 rspec 中触发。 这是我的回调: after_commit :notify_trip, :if => Proc.new { |trip| trip.can_send_schedule_notification? },哦...

回答 3 投票 0

仅在创建时使用 after_commit

我有一个带有 after_commit 的 UserObserver (从 after_create 更改以避免出现 id 未找到的竞争条件错误)来更新计数。但我知道每次创建、更新都会执行

回答 3 投票 0

使用 JSONAPI 和 Rails 渲染数组

我正在使用 jsonapi 来渲染对象: 产品 = 产品.find(1) 渲染 json: ProductSerializer.new(product) 我的问题是,我应该如何序列化产品列表? 如果我这样做: 产品 = Pr...

回答 1 投票 0

将 session_store 与 :active_record 一起使用

我正在将一个应用程序从非常旧的 ruby on Rails 升级到 ruby 3.2.4 with Rails 7.1.3.2。我在使用 :active_record 进行会话存储时遇到问题,所以我回到基础知识并生成...

回答 1 投票 0

使用ActiveStorage和MiniMagick为图像添加水印

我正在使用 Rails 6 ActiveStorge 和 MiniMagick 上传图像并调整图像大小。现在我正在尝试向图像添加水印并使用: <%= image_tag file.variant(draw: "image Over 0,0 10...

回答 1 投票 0

Ruby on Rails 6 with Minitest:有没有办法缩短输出错误?

我正在阅读 Michael Hartl 编写的《Ruby on Rails 教程》的第 3.3 节,我们刚刚开始测试部分。在本节中,我们将在控制器中对静态页面进行分组(

回答 1 投票 0

继承的资源和响应程序 gem 抛出 NoMethodError - 受保护的方法“helpers”

升级到 Rails 6、inherited_resources 到 1.14.0 以及响应程序到 3.1.1 后,我在使用继承资源中的控制器方法后开始收到此错误: NoMethodError - 受保护的方法...

回答 1 投票 0

如何将 Tailwind/Regular CSS 与 Wicked PDF 一起使用?

所以我是 Rails 7 的新手,我正在尝试制作一个简单的订单管理 Web 应用程序。我正在使用 Wicked PDF 2.8 (wkhtmltopdf 0.12.6) 根据订单自动生成发票。 pdf 正在工作...

回答 1 投票 0

调查 Karafka 服务器中的超时:CPU 利用率、内存利用率、max.poll.interval.ms

我有一个rails应用程序,其中包括API服务器(rails服务器)和Worker服务器(Kaafka服务器)。 我们从 API 服务器发送一些消息到主题 T1(只有 1 个分区),现在是 Worker

回答 1 投票 0

如何在 Ruby on Rails 中通过 ActiveAdmin 存储字符串数组

我有一个模型,其属性将数组存储到数据库中。它看起来是这样的: // 它有超过 5 个字段,但字段标签是一个字符串数组。其他的都只是字符串。 乔班...

回答 1 投票 0

当动作文本富文本在Post模型中使用时,如何在rails 7中使用ransack进行搜索?

有人可以帮我找出为什么它不起作用吗? 那么故事是什么呢:有一个 Rails 7 (7.1.3) 博客应用程序,其版本为 4.1.1 在搜索表单中,我需要通过 p 搜索文本...

回答 1 投票 0

第二次单击标记时未打开传单弹出窗口

我正在使用 leaflet.js 在 OSM 地图上显示标记。 问题是,第一次单击标记时,弹出窗口会正常打开,但第二次单击同一标记时,弹出窗口不会打开...

回答 1 投票 0

`bin/rails server` 打开文本文件而不是运行本地服务器

开始 Rails 教程:https://guides.rubyonrails.org/getting_started.html。 遵循了每一步,但是当我开始运行命令 bin/rails 服务器时,下面的文本文件将打开而不是运行...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.