简体   繁体   English

Rails中的RESTful API

[英]RESTful API in rails

I am very new to rails and following a tutorial for RESTful API so let me excuse if it is of not very good quality as I am equally a starter for these kind of terminologies as well. 我是Rails的新手,并且正在关注RESTful API的教程,因此,如果它的质量不是很好,请原谅,因为我也同样是这些术语的入门者。

I created a controller kitten with a command rails g controller kitten index 我用命令rails g controller kitten index创建了一个控制器小猫

and in the index method I posted this code - 在索引方法中,我发布了此代码-

class KittenController < ApplicationController
  def index
    require 'open-uri'

    kittens = open('http://placekitten.com/')
    response_status = kittens.status
    response_body = kittens.read[559, 441]

    puts response_status
    puts response_body
  end
end

and un commented match ':controller(/:action(/:id))(.:format)' in routes.rb 和未注释的match ':controller(/:action(/:id))(.:format)' routes.rb match ':controller(/:action(/:id))(.:format)'

When i navigate through this - http://localhost:3000/kitten 当我浏览此内容时http://localhost:3000/kitten

this is what i am getting in my browser - 这就是我在浏览器中得到的-

Kitten#index 小猫#指数

Find me in app/views/kitten/index.html.erb 在app / views / kitten / index.html.erb中找到我

and this in my command line --> 这在我的命令行中->

比利

Now my question why it so although i am expecting it in my browser but the cat is shown in command prompt instead of browser ..i am new to rest resource so please excuse if it is a bad one :( 现在我的问题为什么会这样,尽管我希望在浏览器中看到它,但是猫显示在命令提示符中而不是浏览器中..i是休息资源的新手,所以如果它不好,请原谅:(

I don't know what tutorial you're following, but doing this seems like a very odd thing to do for Rails in general and learning RESTful APIs in particular. 我不知道您要遵循什么教程,但是对于Rails而言,尤其是学习RESTful API时,这样做似乎很奇怪。

Anyway, the puts in your controller outputs text to Ruby's standard out, which is going to be the terminal where the server started. 无论如何,您的控制器中的puts输出符合Ruby标准的文本,这将成为服务器启动的终端。 That's why this is appearing in the console rather than in your browser: puts is putting it there. 这就是为什么它出现在控制台而不是您的浏览器中的原因: puts将其放置在其中。

If you want this to appear in a web page, you'll need to make a view for that controller action. 如果您希望它显示在网页中,则需要查看该控制器操作的视图。 Perhaps following further along your tutorial will get you there: if not, you might want to find a better one. 也许继续学习本教程可以使您到达那里:否则,您可能想找到一个更好的教程。

You should read the Model-View-Controller rails guide . 您应该阅读Model-View-Controller rails guide

Controllers provide the “glue” between models and views. 控制器提供了模型和视图之间的“胶水”。 In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation. 在Rails中,控制器负责处理来自Web浏览器的传入请求,询问数据模型,并将该数据传递给视图以进行呈现。

Define your variable in the controller and display it in the view: 在控制器中定义变量并将其显示在视图中:

class KittenController < ApplicationController
  def index
    @variable = 'Hello World'
  end
end

In your view (app/views/kitten/index.html.erb): 在您的视图(app / views / kitten / index.html.erb)中:

<%= @variable %>

Rails controllers setup responses with a render call . Rails控制器通过render调用设置响应。 When the call is not performed it instantiates the appropriate view and renders that view. 不执行调用时,它将实例化相应的视图并呈现该视图。 In your case that is index.html.erb 在您的情况下就是index.html.erb

Try this: 尝试这个:

render :text => kittens.read[559, 441], :status => kittens.status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM