2012-02-23 2 views
1

PUTリクエストを介して外部キー(私の場合はtopic_id)を変更すると、コントローラが更新されずに「どこでポストするか」を知っているようです。コントローラは古い値と新しい値の両方をPUTしますか?

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

事実が +オブジェクトは、作成、編集、私は手動でポストは

が、私は私のコントローラが非難している疑いがあるブラウザに表示されますが、ないtopic_idを変更する場合は、コンソール経由して、ブラウザ +から破壊されています確かに。

質問

  • なぜ私のリダイレクトが動作しませんか?

問題の例:localhost:3000/blog/topics/3/posts/1

  1. には、以下のルートを考えてみましょう。私はtopic_id = 1にこのレコードを更新すると、ブラウザは次の例外を返します:ブログで

    のActiveRecord :: RecordNotFoundを:: PostsControllerの#は

    を示し

    IDで「ポスト」 "topic_id = 1 [ポストを見つけることができませんでした。 "= 3]

  2. 驚くべきことではありません。しかし、私が実際にルート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"} 

は、私の仕事の手順です
+0

なぜ、あなたの編集方法のブランクがあるべきである現在のトピックを一覧表示したいいけない選択コレクションでは、ビューのフォーム内にありますか? HTMLの 'form.html.erb'を見ると良いでしょう。私は 'params [:post]'が間違っているとしか推測できません。 – deefour

+1

その空白ではなく、before_filterが使用されます。 – rhodee

+0

@SandipRansing:質問に答える場合は、質問を編集することで答えを書いてください。 –

答えて

1

:あなたはおそらくちょうど明示的に新しいポストのトピックを渡すことができます。実際には@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 

もう一つの問題は、それゆえ、それはTopic.all - @topic

+0

いくつかの_words_を使用して、問題の内容とコードがその問題を解決する方法を説明できますか? –

+0

@LightnessRacesinOrbit詳細な説明のための編集回答 –

+0

それははるかに良いです。 –

0

2つともbefore_filterに入っているので、fetch_postで@topicを設定する必要があるかもしれません。

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

+0

私はちょうど(スタックトレース全体ではなく)エラーを投稿しました。ただし、投稿オブジェクトが実際に保存されるため、ルーティングとは独立しているように見えます。 – rhodee

0

@topic.update_attributes(params[:post])する必要がありますか?それは問題になるようです。 @topic.update_attributes(params[:post][:topic])である必要がありますか?

に見つかった元のTopicを使用しているため、最初のリダイレクトが失敗していると思われます。これはもはやPostに関連付けられていません。古いトピックオブジェクトとURLを投稿する更新コードにそのリダイレクトを観察した場合

redirect_to blog_topic_post_url([@post.topic, @post]) 
関連する問題