我已经登陆了一个用户可以提交电子邮件的目标网页
我有以下数据库:
create_table "subscriptions", force: true do |t|
t.string "fname"
t.string "lname"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
end
以下表格在我的页面/主页视图中:
<div class="form-subscribe">
<%= form_for(@subscription, { :class => 'form-horizontal' }) do |f| %>
<div class="form-inputs subscription-box">
<div class: "form-space">
<div class: "form-group">
<%= f.text_field :fname, class: "form-control sub-field", placeholder: "First Name" %>
</div>
</div>
<div class: "form-space">
<div class: "form-group">
<%= f.text_field :lname, class: "form-control sub-field", placeholder:"Last Name" %>
</div>
</div>
<div class: "form-space">
<div class: "form-group">
<%= f.email_field :email, class: "form-control sub-field", placeholder:"Email" %>
</div>
</div>
</div>
<div class="form-actions">
<%= f.button :submit, class: "btn btn-default"%>
</div>
<% end %>
</div>
每当我尝试填写表格时,它似乎都有效,但随后我访问控制台,我看到正在形成一条新记录,但所有字段都是零,除了id字段。为什么会这样?
这是我的subscritpions控制器:
类SubscriptionsController&lt; ApplicationController中
def new
@subscription = Subscription.new
end
def create
@subscription = Subscription.new
if @subscription.save
redirect_to root_path
end
end
端
这是我的页面控制器:
class PagesController < ApplicationController
def home
@subscription = Subscription.new
end
end
我对此非常陌生,所以不确定这里发生了什么,有什么想法吗?
答案 0 :(得分:0)
当您使用 Rails4
时,您必须使用 Strong Parameters
来允许所需的属性。
像这样更新你的SubscriptionsController
class SubscriptionsController < ApplicationController
def new
@subscription = Subscription.new
end
def create
@subscription = Subscription.new(subscription_params)
if @subscription.save
redirect_to root_path
else
render 'new'
end
end
private
def subscription_params
params.require(:subscription).permit(:fname,:lname,:email)
end
在这些 Guides
中查看Strong Parameters