我知道这肯定是一个愚蠢的错误,但我一直在试图弄清楚为什么我的对象只保存在控制台内,而不是在应用程序本身。
有人有想法吗?
以下是代码:
控制器:
class JobsController < ApplicationController
def index
@jobs = Job.all
end
def new
@job = Job.new
end
def create
@job = Job.new(job_params)
if @job.save
redirect_to jobs_path, flash: { notice: "You've successfully created a job." }
else
redirect_to new_job_path, flash: { alert: @job.errors.messages }
end
end
def edit
@job = Job.find(params[:id])
end
def update
@job = Job.find(params[:id])
if @job.update
redirect_to jobs_path, notice: "You've updated a job."
else
redirect_to edit_job_path, alert: @job.errors.messages
end
end
def destroy
@job = Job.find(params[:id])
if @job.destroy
redirect_to jobs_path, notice: "You've deleted a job."
else
redirect_to jobs_path, alert: @job.errors.messages
end
end
private
def job_params
params.require(:job).permit(:poster, :category, :location, :status)
end
end
观点:
<h1>Create job</h1>
<form class="form">
<%= form_for @job do |f| %>
<%= f.text_field :poster, placeholder: "Poster", onclick: "placeholder = ''", onblur: "placeholder = 'Poster'", autofocus: true, class: "form-group" %><br>
<%= f.text_field :category, placeholder: "Category", onclick: "placeholder = ''", onblur: "placeholder = 'Category'", class: "form-group" %><br>
<%= f.text_field :location, placeholder: "Location", onclick: "placeholder = ''", onblur: "placeholder = 'Location'", class: "form-group" %><br>
<%= f.select :status, ['New', 'Pending', 'Complete'],placeholder: "Status", onclick: "placeholder = ''", onblur: "placeholder = 'Status'", class: "form-group" %><br><br>
<%= f.submit "Submit", class: "btn btn-secondary" %>
<% end %>
</div>
日志:
Started GET "/jobs/new?utf8=%E2%9C%93&authenticity_token=FStlZTVRMKg%2Bw3Zkw5%2FdpP1w5I%2Fl4vxlQPwAGKqRjIM704p6vXWXEe9z4GmdoB4FJirP6AzUJRkCPEcEwmVNjQ%3D%3D&job%5Bposter%5D=jill&job%5Bcategory%5D=jill&job%5Blocation%5D=jill&job%5Bstatus%5D=New&commit=Submit" for ::1 at 2017-04-16 15:16:08 -0400
Processing by JobsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"FStlZTVRMKg+w3Zkw5/dpP1w5I/l4vxlQPwAGKqRjIM704p6vXWXEe9z4GmdoB4FJirP6AzUJRkCPEcEwmVNjQ==", "job"=>{"poster"=>"jill", "category"=>"jill", "location"=>"jill", "status"=>"New"}, "commit"=>"Submit"}
Rendered jobs/new.html.erb within layouts/application (0.8ms)
Completed 200 OK in 13ms (Views: 12.1ms | ActiveRecord: 0.0ms)
答案 0 :(得分:1)
这一行打破了你的形式
<form class="form">
您无法在HTML中嵌套表单。因此,<form>
定义中的form_for
不会被解释为它应该。
此外,还有一个悬空</div>
。