Ruby on rails RubyonRails使用;创建;控制器中的方法为GET not working
友谊\u controller.rbRuby on rails RubyonRails使用;创建;控制器中的方法为GET not working,ruby-on-rails,model-view-controller,Ruby On Rails,Model View Controller,友谊\u controller.rb class FriendshipsController < ApplicationController # POST /friendships # POST /friendships.json def create #@friendship = Friendship.new(params[:friendship]) @friendship = current_user.friendships.build(:friend_
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
我遵循了本教程:您可能将创建配置为POST而不是GET
# POST /friendships
# POST /friendships.json
def create
如果使用脚手架创建控制器的骨架,也会出现这种情况。您可以在路由配置中对此进行更改。但是请注意,通过GET创建东西不再被视为完全符合REST范式。如另一篇文章中所述,这可能是由于rails的默认支架将post请求配置为与创建一样工作。要解决这个问题,您可以尝试类似的方法,尽管我不确定它是否有效
match "/friendships" => "friendships#create", :via => [:get]
get "/friendships" => "friendships#create"
缺点是,GET
请求/friendships
不会像默认情况那样为您提供索引
操作
也许你可以用这样的方法来解决这个问题:
match "/all_friendships", :to => "friendships#index", :via => :get, :as => :friendships
我会在这些友情请求中使用ajax,但由于我刚刚开始,我想我会遵循上面列出的教程,但后来我遇到了这个问题。。。我还查阅了教程的源代码,在将create的POST更改为GET的路径上找不到任何奇怪的东西……在这个页面上有一点关于路径的内容:-标准条目可能类似于:resources:friendships-但这将类似于创建标准操作的宏。在这种情况下,标准意味着将POST映射为创建。
match "/all_friendships", :to => "friendships#index", :via => :get, :as => :friendships