Jobs という新しいモデルで、クライアント モデルから作成されたクライアントのリストを使用しようとしています。
基本的。ユーザーは、現在任意のクライアントに割り当てられているジョブのリストを表示し、さらに詳細な情報にドリルダウンできる必要があります。
client_id というジョブ データベースに新しい列を挿入し、_form
ビューに次のコードを挿入して、すべてのクライアントのドロップダウン リストを表示できるようにしました。
<%= f.label :client_id %><br />
<%= f.collection_select :client_id, @clients, :id, :name, :prompt =>
"Select" %>
でも。jobs/new
送信を押すと、リソースルートに従って、それを POST しようとします。存在しません。
また、ダミー データをデータベースに挿入しましたが、編集しようとすると問題なく表示されます。保存を押しても、レコードには何も起こりません。
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: これが私の Job モデルです。
class Job < ActiveRecord::Base
belongs_to :clients
end