ruby on rails 使用“创建"控制器中的方法作为 GET 不起作用 [英] ruby on rails use "create" method in controller as GET not working

查看:39
本文介绍了ruby on rails 使用“创建"控制器中的方法作为 GET 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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-引用关联

推荐答案

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

You probably configured the create as a POST not a GET.

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

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

This is also the case if you used scaffolding to create a skeleton of your controller. You could change this in the routes configuration. But be aware, that creating stuff via GET is no longer seen as completely conform to the REST paradigm.

这篇关于ruby on rails 使用“创建"控制器中的方法作为 GET 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆