我的问题是关于官方Rails 指南的第 6.4 节
我有一篇文章和一个评论模型,它们之间有 has_many 关系。现在,我们编辑文章显示模板 (app/views/articles/show.html.erb),让我们为每篇文章添加新评论:
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Add a comment:</h2>
<%= form_with(model: [ @article, @article.comments.build ], local: true) do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
Run Code Online (Sandbox Code Playgroud)
有人可以 …
如何在where函数中使用变量?
我有一个带有两个字段A和B(都是整数)的Rate模型,这有效:
Rate.where('A = 1 and B = 2')
Run Code Online (Sandbox Code Playgroud)
如何传递我在params哈希中得到的变量?
x = params[:x]
y = params[:y]
Rate.where('A = x and B = y')
Run Code Online (Sandbox Code Playgroud)
这不起作用并返回错误.
我的问题是关于官方Rails 指南的第 5.10 节
我有一个带有标题和文本字段的文章模型
文章.rb :
class Article < ApplicationRecord
validates :title, presence: true, length: { minimum: 5 }
end
Run Code Online (Sandbox Code Playgroud)
文章_controller.rb:
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
Run Code Online (Sandbox Code Playgroud)
导游说
@article = Article.new
需要添加到新 …