【发布时间】:2012-05-20 21:41:45
【问题描述】:
我正在尝试在名为 Jobs 的新模型中使用从我的客户端模型创建的客户端列表。
基本上。用户应该能够查看当前分配给任何一个客户的作业列表,然后深入了解更多信息。
我在我的作业数据库中插入了一个名为 client_id 的新列,并在我的 _form 视图中插入了以下代码,以便能够查看所有客户端的下拉列表
<%= f.label :client_id %><br />
<%= f.collection_select :client_id, @clients, :id, :name, :prompt =>
"Select" %>
但是。当我点击提交时,它会根据我的资源路由尝试将其发布到jobs/new。不存在。
我还在数据库中插入了一些虚拟数据,尽管当我尝试编辑它时显示效果很好。按保存不会对记录做任何事情。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0ZVYpM9vTgY+BI55Y9yJDwCJwrwSgGL9xjHq8dz5OBE=", "job"=>{"name"=>"Sample Monthly", "client_id"=>"2", "frequency"=>"Monthly", "owner"=>"Me"}, "commit"=>"Save Job", "id"=>"1"}
Job Load (0.3ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
(0.5ms) UPDATE "jobs" SET "name" = 'Sample Monthly', "frequency" = 'Monthly', "updated_at" = '2012-05-12 17:04:23.818967' WHERE "jobs"."id" = 1
(108.3ms) commit transaction
Redirected to http://localhost:3000/jobs/1
这是我的控制器..
class JobsController < ApplicationController
before_filter :load_clients, :only => [ :new, :create, :edit ]
def index
@job = Job.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @job }
end
end
def new
@job = Job.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @Job }
end
end
def create
@job = Job.new(params[:job])
respond_to do |format|
if @job.save
format.html { redirect_to @job, notice: 'Job was successfully created.' }
format.json { render json: @job, status: :created, location: @job }
else
format.html { render action: "new" }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
def show
@job = Job.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @job }
end
end
def update
@job = Job.find(params[:id])
respond_to do |format|
if @job.update_attributes(params[:job])
format.html { redirect_to @job, notice: 'Job was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
private
def load_clients
@client = Client.find(:all)
end
end
我想得到这个工作是一个相对容易的修复,但这是我的 forst Rails 应用程序,我不知道从哪里开始。
编辑:
根据要求。这是我的工作表:
<%= form_for(:job) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :client_id %><br />
<%= f.collection_select :client_id, @client, :id, :name, :prompt =>
"Select" %>
</div>
<div class="field">
<%= f.label :frequency %><br />
<%= f.text_field :frequency %>
</div>
<div class="field">
<%= f.label :owner %><br />
<%= f.text_field :owner %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Edit2:这是我的工作模型。
class Job < ActiveRecord::Base
belongs_to :clients
end
【问题讨论】:
-
需要有关您的表单的更多信息
-
将我的工作 _form 添加到第一个帖子
-
您是否也可以使用作业模型源代码更新您的问题?你使用 attr_accessible 吗?
-
完成。 attr_accessible 方法有什么作用?
-
attr_accessible 指定可以通过批量分配设置的模型属性白名单。 bit.ly/KhZRC6 但我认为不是这种情况,因为您没有质量分配错误。
标签: ruby-on-rails ruby ruby-on-rails-3.2 foreign-keys database-relations