topic_id
PUTリクエストを介して外部キー(私の場合)を変更すると、コントローラーは「どこに」投稿が送信されるかを認識していないようです。
なぜこれが起こるのか完全にはわかりませんが、これに数日間苦労した後、私は本当にいくつかの洞察が必要です。このアプリケーションは、と呼ばれるオブジェクトを更新し、post
とりわけ、を割り当てることになっていtopic_id
ます。モデルは、Topic
has_many(投稿)belongs_to(トピック)関係を介してPostsControllerを介して更新されます。
ファクト +オブジェクトは、コンソールおよびブラウザーから作成、編集、および破棄されます+ topic_idを手動で変更すると、投稿がブラウザーに表示されます
私のコントローラーが原因だと思いますが、確かではありません。
質問
- リダイレクトが機能しないのはなぜですか?
問題の例:
次のルートを検討してください
localhost:3000/blog/topics/3/posts/1
。このレコードをtopic_id = 1
ブラウザに更新すると、次の例外が返されます。ブログ::PostsController#showのActiveRecord ::RecordNotFound
id=1の投稿が見つかりませんでした[WHERE"posts"。"topic_id"= 3]
驚かない。しかし、実際にルートに行くと
localhost:3000/blog/topics/1/posts/1
、オブジェクトが存在します。また、トピックが更新されると、投稿オブジェクトはインデックスから消えますが、それはまったく別の問題です。次の方法でリダイレクトをリファクタリングしようとしましたが、すべて上記の同じエラーが再現されます。
- redirect_to blog_topic_post_url([@ topic、@post]).。
- redirect_to blog_topic_post_url([@ post]).。
- redirect_to blog_topic_post_url(@post).。
ルートリダイレクト先の最初の2つのredirect_to呼び出しを試してみると、オブジェクトが両方のトピック(1と3)をhttp://localhost:3000/blog/topics/blog/1/3/posts/3
格納していることを示しています???
オブジェクトパラメータ
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"TibKWeeC8dGkxR8Jb4bQprGvllUBmiQ5+HtYAPlhn1Q=",
"post"=>{"title"=>"---- maxime",
"description"=>"---- et- facere- sunt- et- illo- reprehenderit- dolor- quis- debitis- vel",
"content"=>"---\r\n- Et asperiores eaque rem maxime laboriosam. Quos dolor perferendis in labore fugit.\r\n Delectus quam vero optio cum eius perferendis sed.\r\n- Veniam eum explicabo error minima. Dolore reprehenderit cumque reiciendis. Molestiae\r\n quo est error aut quas ut aperiam quia.\r\n- Et at quo esse aut ut accusantium alias tempore. Accusamus consequuntur sunt mollitia.\r\n Quas ut voluptate quia sit quia iste corporis. Id rerum placeat voluptas sequi non.\r\n",
**"topic_id"=>"1",**
"tags_attributes"=>{"0"=>{"_destroy"=>"0",
"tag_name"=>"---- velit",
"id"=>"1"},
"1"=>{"_destroy"=>"0",
"tag_name"=>"---- ea",
"id"=>"2"},
"2"=>{"_destroy"=>"0",
"tag_name"=>"---- impedit",
"id"=>"3"}}},
"commit"=>"Update Post",
**"topic_id"=>"3",**
"id"=>"1"}
これが私の作業手順です:
コントローラ
before_filter :fetch_topic, only: [:show, :edit, :update, :destroy]
before_filter :fetch_post, only: [:show, :edit, :update, :destroy]
def edit
end
def update
respond_to do |format|
if @topic.update_attributes(params[:post]) and @post.update_attributes(params[:post])
format.html {redirect_to blog_topic_post_url([@post]), success: 'Post was successfully updated.'}
else
format.html {render :edit, error: 'Post was not updated. Please try again.'}
end
end
end
# I call these on both actions with before_filter. Works on all other relevant actions.
def fetch_topic
@topic = Topic.find(params[:topic_id])
end
def fetch_post
@post = @topic.posts.find(params[:id])
end
form.html.erb
<%= form_for([:blog, @topic, @post]) do |f| %>
...
...
<div class="field">
<%= f.fields_for(:topic) do |build_topic| %>
<%= build_topic.label :topic_name, "Select a topic" %>
<%= collection_select(:post, :topic_id, Topic.all - [@post], :id, :topic_name, prompt: true) %>
<%end%>
....
<div class="actions">
<%= f.submit %>
</div>
<% end %>