1

topic_idPUTリクエストを介して外部キー(私の場合)を変更すると、コントローラーは「どこに」投稿が送信されるかを認識していないようです。

なぜこれが起こるのか完全にはわかりませんが、これに数日間苦労した後、私は本当にいくつかの洞察が必要です。このアプリケーションは、と呼ばれるオブジェクトを更新し、postとりわけ、を割り当てることになっていtopic_idます。モデルは、Topichas_many(投稿)belongs_to(トピック)関係を介してPostsControllerを介して更新されます。

ファクト +オブジェクトは、コンソールおよびブラウザーから作成、編集、および破棄されます+ topic_idを手動で変更すると、投稿がブラウザーに表示されます

私のコントローラーが原因だと思いますが、確かではありません。

質問

  • リダイレクトが機能しないのはなぜですか?

問題の例:

  1. 次のルートを検討してくださいlocalhost:3000/blog/topics/3/posts/1。このレコードをtopic_id = 1ブラウザに更新すると、次の例外が返されます。

    ブログ::PostsController#showのActiveRecord ::RecordNotFound

    id=1の投稿が見つかりませんでした[WHERE"posts"。"topic_id"= 3]

  2. 驚かない。しかし、実際にルートに行くとlocalhost:3000/blog/topics/1/posts/1、オブジェクトが存在します。また、トピックが更新されると、投稿オブジェクトはインデックスから消えますが、それはまったく別の問題です。

  3. 次の方法でリダイレクトをリファクタリングしようとしましたが、すべて上記の同じエラーが再現されます。

    • 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 %>
4

3 に答える 3

1

更新コードを観察すると、古いトピックオブジェクトを含むURLを投稿するようにリダイレクトされます。実際には、であるものを更新する必要があり@post.topicます。また、トピックオブジェクトを更新する必要はありません。

def update 
  if @post.update_attributes(params[:post])
    flash[:success]  = 'Post was successfully updated.'
    redirect_to [@post.topic, @post]
  else 
    flash[:error] = 'Post was not updated. Please try again.'
    render :action => :edit 
  end
 end

もう1つの問題は、ビューフォームの内部です。コレクションを選択すると、現在のトピックを一覧表示したくないため、次のようになります。Topic.all - @topic

于 2012-02-25T18:09:16.593 に答える
0

順序付けの問題である可能性があります。どちらもbefore_filterにあるため、fetch_postでは@topicを設定する必要があります。

エラーを投稿できますか?

于 2012-02-24T00:31:04.273 に答える
0

あなたはやるべきです@topic.update_attributes(params[:post])か?それは問題があるようです。それはすべき@topic.update_attributes(params[:post][:topic])ですか?

Topicまた、で見つけたオリジナルを使用しているため、最初のリダイレクトが失敗していると思います。fetch_topicこれは、に関連付けられていませんPost。おそらく、新しい投稿トピックを明示的に渡すことができます。

redirect_to blog_topic_post_url([@post.topic, @post])
于 2012-02-25T16:06:06.090 に答える