这是我的代码
来自index.html.erb
<%= form_for index_path, :method => "POST" do %>
<%= label_tag :name, "Enter your full name: " %>
<%= text_field_tag :name, @name, :placeholder => "Enter your name" %><br/>
<%= label_tag :email, "Enter your e-mail address: " %>
<%= email_field_tag :email, @email, :placeholder => "something@domain.com" %><br/>
<%= label_tag :question, "Type your question: " %>
<%= text_area_tag :question, @question, :size => "30x5", :placeholder => "Your text goes here", :maxlength => "130" %><br/>
<%= submit_tag "Ask Question" %>
来自Blog Controller
class BlogController < ApplicationController
def index
@name = params['name']
@email = params['email']
@question = params['question']
end
def about
end
def contact
end
end
然后从seeds.rb:
Invitation.create(:name => "#{@name}", :email => "#{@email}", :question => "#{@question}")
当我填写所有字段并提交时,它在数据库中创建了空白记录,我在做什么错了?
答案 0 :(得分:3)
您做错了。您无法访问seeds.rb
内部控制器中定义的实例变量。相反,您只需要在index
动作中创建记录
def index
@name = params['name']
@email = params['email']
@question = params['question']
Invitation.create(:name => @name, :email => @email, :question => @question)
end
seeds.rb
用于创建具有默认值的记录以播种数据库。通常,这些值是硬编码的。