[英]Rails: NoMethodError Undefined method
I'm trying to build a basic Rails/React app which takes in a series of user inputs, then displays them on the screen within a letter template (think MadLibs.) I've followed several React tutorials and I have a decent amount of experience with Rails but I've gotten amazingly hung up with an API I've created. 我正在尝试构建一个基本的Rails / React应用程序,该应用程序会接收一系列用户输入,然后将它们显示在屏幕上的字母模板中(想想MadLibs。)我已经关注了几个React教程,并且具有Rails的经验,但是我已经非常惊讶地迷恋了自己创建的API。 For context I'm using this tutorial here as a guide, but am building my own app.
对于上下文,我在这里使用本教程作为指南,但是正在构建自己的应用程序。
This is my routes file: 这是我的路线文件:
Rails.application.routes.draw do
root to: 'site#index'
namespace :api do
namespace :v1 do
resources :letters, only: [:index, :create]
end
end
end
This is my base_controller.rb file: 这是我的base_controller.rb文件:
class Api::V1::BaseController < ApplicationController
respond_to :json
end
and this is my letters_controller.rb file: 这是我的letters_controller.rb文件:
class Api::V1::LettersController < Api::V1::BaseController
def index
respond_with Letter.all
end
def create
respond_with :api, :v1, Letter.create(letter_params)
end
private
def letter_params
params.require(:letter).permit(:id, :param_one, :param_two)
end
end
The controller files are both stored in controllers/api/v1 and this is reflected in my routes file. 控制器文件都存储在controllers / api / v1中,这反映在我的路由文件中。 I'm able to use the index method no problem and get all Letter objects to display.
我可以使用索引方法没问题,并显示所有Letter对象。 I'm able to use the create method somewhat, in that a new Letter is created and saved to the database (sqlite btw.) Problem comes with the response, as in there isn't one.
我可以某种程度上使用create方法,因为会创建一个新的Letter并将其保存到数据库(sqlite btw)。响应中存在问题,因为没有响应。 Instead of a response coming back to my AJAX call I'm getting the following:
而不是返回到我的AJAX调用响应,我得到以下信息:
NoMethodError (undefined method `api_v1_letter_url' for #<Api::V1::LettersController:0x007f3085f1d300>):
app/controllers/api/v1/letters_controller.rb:7:in `create'
I have no earthy idea why this isn't working. 我不知道为什么这不起作用。 From everything that I've read feeding the respond_with call the parameters I have should cause the returned url to work and come back with a response.
从我读过的所有内容中获取对respond_with调用的反馈,我所拥有的参数应该使返回的URL工作并返回响应。 Instead I get this strange 500.
相反,我得到了这个奇怪的500。
I've never had so much trouble with a program that it's given me a migraine before. 我从来没有遇到过这么麻烦的程序,以前它给我带来了偏头痛。 If anyone can help it will be greatly appreciated.
如果有人可以帮助,将不胜感激。
If you run rake routes
, you will see that there is no declaration for api_v1_letter_url
. 如果运行
rake routes
,将会看到api_v1_letter_url
没有声明。
This is because you do not declare a show method on routes.rb
(you explicitly only use index
and create
). 这是因为您没有在
routes.rb
上声明show方法(显式只使用index
和create
)。 If you want the call in your controller to work as-is, you need a show
method: 如果您希望控制器中的呼叫按原样工作,则需要一种
show
方法:
Rails.application.routes.draw do
root to: 'site#index'
namespace :api do
namespace :v1 do
resources :letters, only: [:index, :create, :show]
end
end
end
Now, run rake routes
, you'll see it's there. 现在,运行
rake routes
,您会看到它在那里。
To implement show
in your controller: 要在控制器中实现
show
:
def show
respond_with Letter.find(params[:id])
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.