friendships_controller.rbRuby on Rails的使用 「創建」,在控制器方法如GET工作不
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
我將通過AJAX對這些友好的要求,但由於我起步,我想我會按照上面列出的教程,但後來我得到成問題......也查看了教程源代碼,我找不到將路由創建成GET的路由上的任何奇怪的東西...... – fxuser
本頁有一些路由選擇:http:// guides .rubyonrails.org/routing.html - 標準條目可能類似於:資源:友誼 - 但這將像創建標準動作的宏一樣。標準意味着在這種情況下,POST被映射到創建。 –