gpt4 book ai didi

ruby-on-rails - ruby on rails 在 Controller 中使用 "create"方法,因为 GET 不工作

转载 作者:行者123 更新时间:2023-12-04 05:32:14 24 4
gpt4 key购买 nike

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

最佳答案

您可能将创建配置为 POST 而不是 GET。

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

如果您使用脚手架来创建 Controller 的骨架,情况也是如此。您可以在路由配置中更改此设置。但请注意,通过 GET 创建内容不再被视为完全符合 REST 范式。

关于ruby-on-rails - ruby on rails 在 Controller 中使用 "create"方法,因为 GET 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12432149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com