所以我有兩種型號Topic
,Client
。客戶has_and_belongs_to_many :topics
和主題has_and_belongs_to_many :clients
。如何讓我的創建操作爲嵌套和非嵌套資源工作?
基本上,我希望發生的是什麼?當有人去我Topic#index
,這取決於他們是如何到達那裏(即無論是通過client/:id/topics
或只是/topics
),我想要的行爲創造&新是不同的。即在/topics
,它只是創建一個話題。在client/:id/topics
它創建一個主題並將其分配給該客戶端。
我的路線是這樣的:
resources :topics
resources :clients do
resources :topics
end
我的主題控制器看起來是這樣的:
def new
if params[:client_id]
@client = Client.find(params[:client_id])
@topic = @client.topics.build
else
@topic = Topic.new
end
respond_to do |format|
format.html # new.html.erb
format.json { render json: @topic }
end
end
def create
if params[:client_id]
@client = Client.find(params[:client_id])
@topic = @client.topics.build(params[:topic])
else
@topic = Topic.new(params[:topic])
end
respond_to do |format|
if @topic.save
format.html { redirect_to @topic, notice: 'Topic was successfully created.' }
format.json { render json: @topic, status: :created, location: @topic }
else
format.html { render action: "new" }
format.json { render json: @topic.errors, status: :unprocessable_entity }
end
end
end
我views/topics/_form.html.erb
看起來是這樣的:
<%= form_for([@client, @topic]) do |f| %>
...
<% end %>
然而,當我執行來自client/:id/topics
的行爲這是日誌的樣子:
Started GET "/clients/1/topics/new" for 127.0.0.1 at 2012-09-10 14:33:06 -0500
Processing by TopicsController#new as HTML
Parameters: {"client_id"=>"1"}
Client Load (0.2ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT 1 [["id", "1"]]
Rendered topics/_form.html.erb (3.6ms)
Rendered topics/new.html.erb within layouts/application (4.7ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.2ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
Rendered layouts/_navigation.html.erb (7.2ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 61ms (Views: 57.0ms | ActiveRecord: 0.7ms)
這看起來不錯......一切似乎都是爲了這裏。但正是在這件事似乎不工作POST
:
Started POST "/clients/1/topics" for 127.0.0.1 at 2012-09-10 14:33:13 -0500
Processing by TopicsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"J172LuZQPv8=", "topic"=>{"name"=>"AMZN"}, "commit"=>"Create Topic", "client_id"=>"1"}
Client Load (0.2ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT 1 [["id", "1"]]
(0.1ms) begin transaction
SQL (186.2ms) INSERT INTO "topics" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Mon, 10 Sep 2012 19:33:13 UTC +00:00], ["name", "AMZN"], ["updated_at", Mon, 10 Sep 2012 19:33:13 UTC +00:00]]
(4.6ms) commit transaction
Redirected to http://localhost:3000/topics/4
Completed 302 Found in 198ms (ActiveRecord: 191.1ms)
你會發現沒有新的話題給客戶端的分配。
我錯過了什麼?
謝謝!
編輯1
新增把調試語句我創建行動,這是我後,執行POST操作的結果 - 這表明,越來越的params[:client_id]
並不僅僅是params[:id]
:
Served asset /application.js - 304 Not Modified (1ms)
**************************************************
This is the params[:client_id] => {3}
**************************************************
This is the params[:id] => {}
Started POST "/clients/3/topics" for 127.0.0.1 at 2012-09-10 15:06:31 -0500
Processing by TopicsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"J172LuZQc5NYoiMSzDD3oY9vGmxxCX0OdxcGm4GSPv8=", "topic"=>{"name"=>"TEST2"}, "commit"=>"Create Topic", "client_id"=>"3"}
Client Load (0.2ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = ? LIMIT 1 [["id", "3"]]
(0.1ms) begin transaction
SQL (0.7ms) INSERT INTO "topics" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Mon, 10 Sep 2012 20:06:31 UTC +00:00], ["name", "TEST2"], ["updated_at", Mon, 10 Sep 2012 20:06:31 UTC +00:00]]
(3.3ms) commit transaction
Redirected to http://localhost:3000/topics/6
Completed 302 Found in 11ms (ActiveRecord: 4.3ms)
編輯2:
所以,我想別的事情,似乎工作,但我會愛知道爲什麼上述不起作用。
如果在我的Topic#create
我只是這樣做:
@client = Client.find(params[:client_id])
@topic = Topic.new(params[:topic])
@client.topics << @topic
它工作正常。
但是,再次...我想知道爲什麼.build
不適合HABTM或在這種情況下。
這不是問題。我更新了問題以包含該輸出。 – marcamillion
另外,在我的日誌輸出I列入問題,你可以看到它是找到正確的'PARAMS [:CLIENT_ID]'和正確的,包括它在params哈希表。它只是沒有做正確的事情。 – marcamillion
嗯......看來你EDIT2後很奇怪.... 你可以嘗試一次寫在你的舊create方法在軌控制檯的步驟..看看實際的問題是什麼... –