我是Ruby on Rails世界的新手,我正在开发一个首次下载的表单,你可以在_form.html.erb中看到这里的代码:
<%= simple_form_for(@job, html: {class: 'form-horizontal'}) do |f| %>
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Category:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Landscaping','Landscaping'],
['Babysitting','Babysitting'],['Tree planting','Tree planting']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Location:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Dorchester','Dorchester'],
['Roxbury','Roxbury'],['Mattapan','Mattapan']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Status:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['New','New'],
['Pending','Pending'],['Complete','Complete']])) %>
</div>
</div>
<%= f.input :description, label: "Job Description" %>
<%= f.submit 'Add Job', class: 'btn btn-default' %>
<% end %>
当我点击“添加工作”时,它会将我带到一个我无法自行解决的空白屏幕。
我在views / show.html.erb中的视图时间周围更改了代码。我有这个:
<h1><%= @job.poster %></h1>
<p><%= @job.category %></p>
<p><%= @job.location %></p>
和此:
<tbody>
<% @job.each do |job| %>
<tr>
<td><%= job.poster %></td>
</tr>
<% end %>
</tbody>
我继续得到一个空白的屏幕,或者如果我去了我的jobs_controller.rb并像这样开发它:
class JobsController < ApplicationController
before_action :find_job, only: [:show, :edit, :update, :destroy]
def index
# @job = Job.all
end
def show
@job = Job.find_job(jobs_params[:id])
end
def new
@job = Job.new
end
def create
@job = Job.new(jobs_params)
if @job.save
redirect_to @job, notice: 'Your job was successfully added'
else
render "New"
end
end
def edit
end
def update
end
def destroy
end
private
def jobs_params
params.require(:job).permit(:poster, :category, :location, :status, :description)
end
def find_job
@job = Job.find(params[:id])
end
end
我会为ActiveRecord对象获取一个未定义的'poster'方法,但如果我删除show动作中的代码,我只会得到一个空白页面。我在这里没有解决方案而且我已经看过了,但对我来说没有任何解决方案。
我的routes.rb文件如下所示:
Rails.application.routes.draw do
resources :jobs
root 'jobs#index'
end
我更改了show动作以反映这一点:
def show
@job = Job.find(jobs_params[:id])
end
然后我丢失了一个参数或者值为空:job。
我做了一个rails控制台,我有这个:
Job Load (1.1ms) SELECT "jobs".* FROM "jobs" LIMIT ? [["LIMIT", 11]] => #<ActiveRecord::Relation [#<Job id: 1, poster: nil, category: nil, location: nil, status: "New", description: "Your job is to plant trees all over the Dorchester...", created_at: "2017-08-01 10:29:40", updated_at: "2017-08-01 10:29:40">]>
我不明白为什么我对除了描述之外的所有事情都没有。
似乎每个表单输入都是这样的:
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
在rails控制台中给我一个零,只有这一个:
<%= f.input :description, label: "Job Description" %>
成功输入了数据,因此f.select的代码有问题,但我不确定是什么。
答案 0 :(得分:1)
更改此
def show
@job = Job.find_job(jobs_params[:id])
end
到这个
def show
end
由before_action
为您检索@job记录并在show.html.erb中使用它
<h1><%= @job.poster %></h1>
<p><%= @job.category %></p>
<p><%= @job.location %></p>
答案 1 :(得分:0)
从JobsController中的show动作中删除此行:
@job = Job.find_job(jobs_params[:id])
答案 2 :(得分:0)
尝试将_form.html.erb
替换为
<%= simple_form_for(@job, html: {class: 'form-horizontal'}) do |f| %>
<div class="control-group">
<%= f.label "Poster:", class: 'control-label' %>
<div class="controls">
<%= f.select(:poster, options_for_select([['Nick Maloney','Nick Maloney'],
['Peter Brown','Peter Brown'],['Jen Morris','Jen Morris']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Category:", class: 'control-label' %>
<div class="controls">
<%= f.select(:category, options_for_select([['Landscaping','Landscaping'],
['Babysitting','Babysitting'],['Tree planting','Tree planting']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Location:", class: 'control-label' %>
<div class="controls">
<%= f.select(:location, options_for_select([['Dorchester','Dorchester'],
['Roxbury','Roxbury'],['Mattapan','Mattapan']])) %>
</div>
</div>
<div class="control-group">
<%= f.label "Status:", class: 'control-label' %>
<div class="controls">
<%= f.select(:status, options_for_select([['New','New'],
['Pending','Pending'],['Complete','Complete']])) %>
</div>
</div>
<%= f.input :description, label: "Job Description" %>
<%= f.submit 'Add Job', class: 'btn btn-default' %>
<% end %>
在控制器中
def show
end
show.html.erb
<h1><%= @job.poster %></h1>
<p><%= @job.category %></p>
<p><%= @job.location %></p>
你有
工作ID:1,海报:零,类别:零,地点:零,状态: &#34;新&#34;,描述:&#34;你的工作是种植树木 多尔切斯特...&#34;
因为mistek您在:status
select
使用了_form.html.erb