我创建了一个表单
<%= form_for [current_user,@product,@bid] do |f| %>
<p><%= f.number_field :bid_amount %></p>
<p><%= f.number_field :product_id %>
<p><%= f.submit 'Bid!' %></p>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在:product_id字段中,我想默认添加@ product.id,如何实现?请.提前致谢.:)
我只是在Ruby on Rails Tutorial.org上找到了这个代码.add_index部分到底有什么作用?为什么有3行呢?
class CreateRelationships < ActiveRecord::Migration
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id
t.timestamps
end
add_index :relationships, :follower_id
add_index :relationships, :followed_id
add_index :relationships, [:follower_id, :followed_id], unique: true
end
end
Run Code Online (Sandbox Code Playgroud) 在Rails 4.2.0中,我使用Devise gem进行身份验证 - devise 3.4.1.
创建用户后立即发送确认电子邮件.我们需要跳过此确认电子邮件并发送带有确认链接的自定义电子邮件.请帮我解决这个问题.
:confirmable 用户模型中使用.
有两个表conversations和messages,我想获取对话及其最新消息的内容。
conversations- id(主键)、名称、创建时间
messages- id、内容、created_at、conversation_id
目前我们正在运行此查询来获取所需的数据
SELECT
conversations.id,
m.content AS last_message_content,
m.created_at AS last_message_at
FROM
conversations
INNER JOIN messages m ON conversations.id = m.conversation_id
AND m.id = (
SELECT
id
FROM
messages _m
WHERE
m.conversation_id = _m.conversation_id
ORDER BY
created_at DESC
LIMIT 1)
ORDER BY
last_message_at DESC
LIMIT 15
OFFSET 0
Run Code Online (Sandbox Code Playgroud)
上面的查询返回有效数据,但其性能随着行数的增加而降低。有没有其他方法可以提高性能来编写此查询?例如附加小提琴。
http://sqlfiddle.com/#!17/2decb/2
还尝试了已删除答案之一中的建议:
SELECT DISTINCT ON (c.id)
c.id,
m.content AS last_message_content,
m.created_at AS last_message_at
FROM conversations AS c
INNER JOIN messages AS m …Run Code Online (Sandbox Code Playgroud) sql postgresql greatest-n-per-group postgresql-performance postgresql-13
首先,我意识到这个问题之前已经被问过多次,但是我看到的所有答案都对我不起作用。
我已经使用 bootstrap gem 将 bootstrap 添加到我的 Rails 应用程序中,或者至少我认为我有。bootstrap 的 css 和样式都在那里并且可以工作,但是缺少 javascript 功能
我对 Rails 相当陌生,所以我可能错过了一些非常明显的东西,我在下面附上了我认为相关的文件。
我有以下文件,任何建议或帮助将不胜感激
宝石文件
source 'https://rubygems.org'
ruby '2.7.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.2', '>= 6.0.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', …Run Code Online (Sandbox Code Playgroud) def total
puts requeset.referrer
transactions = UTransaction.all
render json: amount(transactions)
end
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到以下错误
Completed 500 Internal Server Error in 38ms
NameError (undefined local variable or method `requeset' for #<Api::V1::Admin::UTransactionsController:0x007f98d49539d0>)
Run Code Online (Sandbox Code Playgroud) 我需要在 fb uisng graph api 中获取特定帖子的共享计数。我找不到任何方法来这样做。我已经点击了下面的链接。
https://developers.facebook.com/docs/graph-api/reference/v2.3/object/sharedposts
它以分页格式返回共享帖子的数据。有没有办法在不获取整个数据且不在数据内分页的情况下获得共享总数?
请推荐!!
Rails 初学者,\n不知何故,我找不到在创建记录后重定向用户的方法。
\n\n我有这个控制器:
\n\n def create\n @product = Product.new(product_params)\n\n respond_to do |format|\n if @product.save\n format.html { redirect_to @product, notice: \'Product successfully created\' }\n else\n format.html { render :new, notice: \'Error\' }\n end\n end\n end\n\n def product_params\n params.require(:product).permit(:name, :description, :price, :quantity, :status, :image)\n end\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试过一些变体,例如redirect_to product_path(@product)删除format块,但没有任何效果。用于提交值的模型和表单似乎有效(验证已通过,记录已保存)。
日志:
\n\nParameters: {"..."}\n User Load (0.2ms) SELECT ....\n \xe2\x86\xb3 app/controllers/application_controller.rb:15:in \'admin?\'\n (0.1ms) BEGIN\n \xe2\x86\xb3 app/controllers/products_controller.rb:24:in \'block in create\'\n Product Exists? (0.4ms) SELECT 1 ...\n \xe2\x86\xb3 app/controllers/products_controller.rb:24:in \'block …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用devise进行身份验证。当用户注册时,会向该用户发送一封确认电子邮件,其中显示
Welcome user@gmail.com !
You can confirm your account email through the link below:
Confirm my account
Run Code Online (Sandbox Code Playgroud)
但是我想显示用户名而不是电子邮件。我该如何实施?请帮忙。我和我一起设计了viewws。但是它使用@email实例变量来显示电子邮件。它在哪里定义?帮助将不胜感激。
<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @token) %></p>
Run Code Online (Sandbox Code Playgroud) ruby-on-rails actionmailer devise devise-confirmable ruby-on-rails-4
我正在使用 Rails 和 Devise。有一个Admin由 devise 创建的模型。如何禁用创建管理员?(如何禁用create和new操作是设计控制器)
我的 Rails 版本是 6.0.1