PUTリクエストを介して外部キー(私の場合はtopic_id
)を変更すると、コントローラが更新されずに「どこでポストするか」を知っているようです。コントローラは古い値と新しい値の両方をPUTしますか?
私はこれがどうして起こるのか完全にはわかりませんが、数日間このことに苦労した後、私は本当にいくつかの洞察力が必要です。このアプリケーションは、post
というオブジェクトを更新するとされています。とりわけ、topic_id
を割り当てます。 Topic
モデルは、has_many(posts)belongs_to(topic)関係を介してPostsControllerを介して更新されます。
事実が +オブジェクトは、作成、編集、私は手動でポストは
が、私は私のコントローラが非難している疑いがあるブラウザに表示されますが、ないtopic_idを変更する場合は、コンソール経由して、ブラウザ +から破壊されています確かに。
質問
- なぜ私のリダイレクトが動作しませんか?
問題の例:localhost:3000/blog/topics/3/posts/1
:
には、以下のルートを考えてみましょう。私は
topic_id = 1
にこのレコードを更新すると、ブラウザは次の例外を返します:ブログでのActiveRecord :: RecordNotFoundを:: PostsControllerの#は
を示しIDで「ポスト」 "topic_id = 1 [ポストを見つけることができませんでした。 "= 3]
驚くべきことではありません。しかし、私が実際にルート
localhost:3000/blog/topics/1/posts/1
に行くと、オブジェクトが存在します。また、トピックが更新されたときに、投稿オブジェクトはインデックスから消えますが、それはまったく別の問題です。- redirect_toのblog_topic_post_url([トピック@、@post])..
- redirect_toのblog_topic_post_url([:私は次の方法で私のリダイレクトをリファクタリングしようとしていると、彼らはすべての上に同じエラーを再現し
@post])..
- redirect_toのblog_topic_post_url(@post)..
Iルートがhttp://localhost:3000/blog/topics/blog/1/3/posts/3
にリダイレクトする最初の2つのredirect_toコールを試して、オブジェクトがの両方を保存していることを示してください。トピック(1と3)???
<%= 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 %>
form.html.erb
コントローラ
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
:
オブジェクトパラメータここ
{"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"}
は、私の仕事の手順です
なぜ、あなたの編集方法のブランクがあるべきである現在のトピックを一覧表示したいいけない選択コレクションでは、ビューのフォーム内にありますか? HTMLの 'form.html.erb'を見ると良いでしょう。私は 'params [:post]'が間違っているとしか推測できません。 – deefour
その空白ではなく、before_filterが使用されます。 – rhodee
@SandipRansing:質問に答える場合は、質問を編集することで答えを書いてください。 –