ruby-on-rails - 当我从未明确调用它时,为什么我的实例变量会显示在视图上?
问题描述
我在 Rails 上编码,我已经创建了一个应用程序rails g model
,rails g controller
我已经成功创建了索引、显示、创建和新操作,在运行我的服务器并检查我完成的项目之后,我遇到了这个问题,其中我的实例变量@coupons
包含一个创建的优惠券数组,显示在视图索引页面中。我没有明确地称呼它或它刚刚出现的任何东西。下面是我的代码 myindex.html.erb
和CouponController
.
意见/index.html.erb
<h1> List of Stores with Coupons</h1>
<div>
<%= @coupons.each do |coupon| %>
<h2><%= link_to coupon.store, coupon_path(coupon) %></h2>
<% end %>
</div>
<%= form_tag new_coupon_path, :method => :get do %>
<%= submit_tag "Create New Coupon" %>
<% end %>
优惠券控制器
class CouponsController < ApplicationController
def index
@coupons = Coupon.all
end
def new
end
def create
@coupon = Coupon.create(coupon_code: params[:coupon][:coupon_code], store: params[:coupon][:store])
redirect_to coupon_path(@coupon)
end
def show
@coupon = Coupon.find(params[:id])
end
end
解决方案
<%= @coupons.each do |coupon| %>
应该
<% @coupons.each do |coupon| %>
要在视图上输出的<%=
原因。@coupons
推荐阅读
- ios - 无法设置其中包含可调整大小的图像的 HStack
- javascript - vue.js 异步回调函数
- python - 尽管遵循 Splunk Dev 文档说明,AppInspect 仍无法安装
- typescript - 如何在运行时推断空值的“正确”类型?
- c++ - 如何抑制带参数的 GCC 警告?
- triggers - 如何从两个不同对象中的两个不同字段中减去值并在 Apex 触发器中填充自定义字段?
- flutter - Flutter、Futures、Context 和 Navigator.pop 问题
- ruby-on-rails - Shrine gem - 如何防止从 S3 删除上传的文件
- google-maps - 从谷歌地图中找到两个日期之间循环的总距离
- r - 如何从具有移动固定单元格的总和创建一列,并从 R 中的不同行开始