Rails:对象保存在控制台中但不在应用程序中
Rails: Object saving in console but not within app
我知道这一定是某个地方的愚蠢错误,但我一直在兜圈子试图弄清楚为什么我的对象只保存在控制台中,而不保存在应用程序本身中。
有人有想法吗?
代码如下:
控制器:
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)
这条线破坏了你的形式
<form class="form">
HTML 中不能有嵌套表单。因此,您的 form_for
定义中的 <form>
未按应有的方式解释。
此外,还有一个悬挂 </div>
。
我知道这一定是某个地方的愚蠢错误,但我一直在兜圈子试图弄清楚为什么我的对象只保存在控制台中,而不保存在应用程序本身中。
有人有想法吗?
代码如下:
控制器:
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)
这条线破坏了你的形式
<form class="form">
HTML 中不能有嵌套表单。因此,您的 form_for
定义中的 <form>
未按应有的方式解释。
此外,还有一个悬挂 </div>
。