2

friendships_controller.rb

class FriendshipsController < ApplicationController

  # POST /friendships
  # POST /friendships.json
  def create

    #@friendship = Friendship.new(params[:friendship])
    @friendship = current_user.friendships.build(:friend_id => params[:friend_id])

    respond_to do |format|
      if @friendship.save
        format.html { redirect_to user_profile(current_user.username), notice: 'Friendship was successfully created.' }
        format.json { render json: @friendship, status: :created, location: @friendship }
      else
        format.html { redirect_to user_profile(current_user.username), notice: 'Friendship was not created.' }
        format.json { render json: @friendship.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /friendships/1
  # DELETE /friendships/1.json
  def destroy
    @friendship = Friendship.find(params[:id])
    @friendship.destroy

    respond_to do |format|
      format.html { redirect_to friendships_url }
      format.json { head :no_content }
    end
  end
end

私が行くとき私はhttp://localhost:3000/friendships?friend_id=1得る

Unknown action

The action 'index' could not be found for FriendshipsController

私はこのチュートリアルに従いました:http://railscasts.com/episodes/163-self-referential-association

4

2 に答える 2

3

おそらく、作成をGETではなくPOSTとして構成しました。

  # POST /friendships
  # POST /friendships.json
  def create

これは、スキャフォールディングを使用してコントローラーのスケルトンを作成した場合にも当てはまります。これは、ルート構成で変更できます。ただし、GETを介してものを作成することは、RESTパラダイムに完全に準拠しているとは見なされなくなったことに注意してください。

于 2012-09-14T21:27:24.827 に答える
0

他の投稿で述べたように、これはおそらく、railsのデフォルトのスキャフォールディングがPOSTリクエストを作成として機能するように構成しているために発生します。これを修正するには、このようなことを試すことができますが、うまくいくかどうかはわかりません。

   match "/friendships" => "friendships#create", :via => [:get]
   get   "/friendships" => "friendships#create"

欠点は、デフォルトの場合のように、へのGETリクエストがアクションを提供し/friendshipsないことです。index

たぶんあなたはこれを次のようなもので修正することができます:

match "/all_friendships", :to => "friendships#index", :via => :get, :as => :friendships
于 2012-09-14T21:38:14.990 に答える