简体   繁体   English

RESTful API Ruby on Rails

[英]RESTful API Ruby on Rails

I'm playing with Ruby on Rails for the first time, and have an app up and running. 我是第一次玩Ruby on Rails,并且有一个应用程序正在运行。 Here's the quick database definition: 这是快速的数据库定义:

Teams
- id: int
- name: char[20]

Answers
- id: int
- answer_value: text

I want to be able to type: "http://localhost:3000/teams/1/answers/purple" in a browser if team 1 answers purple. 我希望能够在浏览器中输入:“ http:// localhost:3000 / teams / 1 / answers / purple”(如果第1组回答为紫色)。

I thought that adding the following to my routes.rb file would allow me to do that, but it hasn't. 我以为将以下内容添加到我的routes.rb文件中可以使我做到这一点,但事实并非如此。

resources :teams do
  resources :answers
  post 'answer_value', :on => :member
end

I can see the first answer by team 1 by going to "http://localhost:3000/teams/1/answers/1" , but I don't know how to actually set values via the URI. 我可以通过转到“ http:// localhost:3000 / teams / 1 / answers / 1”看到第一个团队的第一个答案,但是我不知道如何通过URI实际设置值。

如果我对您的理解正确,则在回答控制器的show动作时,不要执行Answer.find(params[:id]) ,而应做Answer.find_by_answer_value(params[:id])

If you want to create an answer, please post to 如果要创建答案,请发布到

/teams/1/answers / teams / 1 / answers

with parameter :answer => { :answer_value => 'purple' } 使用参数:answer => {:answer_value =>'紫色'}

Don't append answer_value to the url, looks like it's record id. 不要将answer_value附加到URL,看起来像是记录ID。

You need to either over ride the to_param method in the model, which can do what you are looking for although there are some caveats - including needing to use what @Chirantan suggested with Answer.find_by_answer_value(params[:id]) for all your finders. 您需要过度使用模型中的to_param方法,尽管有一些注意事项,该方法仍可以完成您要寻找的东西-包括需要对所有发现者使用@Chirantan与Answer.find_by_answer_value(params[:id])一起建议的内容。 You can find more info in this Railscast on the subject or search for to_param in the Rails API and select the entry under ActiveRecord::Integration. 您可以在此Railscast中找到有关主题的更多信息,或在Rails API中搜索to_param ,然后选择ActiveRecord :: Integration下的条目。

Alternatively you can go with a gem like FriendlyId which provides some more comprehensive functionality and control over the URL construction. 或者,您可以使用类似FriendlyId的gem,它提供了一些更全面的功能并可以控制URL的构造。

Typing things into your browser won't perform a post, regardless of what you put in the routes, it will only perform a get. 在浏览器中输入内容不会执行发布,无论您在路由中输入什么内容,只会执行获取操作。 Creating something by a get is not RESTful, and you really shouldn't do it. 随意创建东西并不是RESTful的,您实际上不应该这样做。 That said, since you say you're just 'playing around with rails', you could do this: 就是说,既然您说您只是在“玩弄铁轨”,则可以这样做:

If you want a get to create the thing, you have to set it that way in the routes. 如果要创建事物,则必须在路线中以这种方式进行设置。 /teams/1/answers/purple / teams / 1 / answers / purple

get '/teams/:team_id/answers/:answer_value' => 'answers#create'

then in your answers controller, create action, you could just check for a params[:team_id] and params[:answer value] and make sure that gets set before the save. 然后在您的答案控制器中,创建操作,您只需检查一个params [:team_id]和params [:answervalue]并确保在保存之前对其进行了设置。

def create
  @answer = Answer.new()
  @answer.team_id=params[:team_id]
  @answer.answer_value=params[:answer_value]


  respond_to do |format|
    if @answer.save
      format.html { redirect_to @answer, notice: 'Answer was successfully created.' }
      format.json { render json: @answer, status: :created, location: @answer }
    else
      format.html { render action: "new" }
      format.json { render json: @answer.errors, status: :unprocessable_entity }
    end
  end
end

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

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