简体   繁体   English

Rails 3:通过URL创建新记录

[英]Rails 3: Create a new record by URL

All: 所有:

What's the correct syntax for a new record to be created by passing all the parameters inside a URL, not from a Web form? 通过在URL中而不是从Web表单传递所有参数来创建新记录的正确语法是什么?

I'd like a different computer system to be able to concatenate a URL and create new records in my Rails app. 我希望其他计算机系统能够连接URL并在Rails应用程序中创建新记录。

But, when I try to make a new record in "Scores", Rails won't add it. 但是,当我尝试在“得分”中创建新记录时,Rails不会添加它。

    http://myapp.heroku.com/scores?message_id=51&subscriber_id=167&amount=3&method=post

Where the desired attributes to add to a new scores record are message_id, subscriber_id, and amount. 要添加到新分数记录的所需属性是message_id,subscriber_id和数量。

Here's the Heroku log 这是Heroku日志

    2012-01-26T18:27:34+00:00 app[web.1]: Started GET "/scores?message_id=51&subscriber_id=167&amount=3&method=submit" for 122.17.234.171 at 2012-01-26 10:27:34 -0800
    2012-01-26T18:27:34+00:00 app[web.1]:   Processing by ScoresController#index as HTML
    2012-01-26T18:27:34+00:00 app[web.1]:   Parameters: {"message_id"=>"51", "subscriber_id"=>"167", "amount"=>"3", "method"=>"post"}
    2012-01-26T18:27:34+00:00 app[web.1]: Rendered scores/index.html.erb within layouts/application (3.4ms)
    2012-01-26T18:27:34+00:00 app[web.1]: Completed 200 OK in 16ms (Views: 4.2ms | ActiveRecord: 10.6ms)
    2012-01-26T18:27:34+00:00 heroku[router]: GET postopmobile.heroku.com/scores dyno=web.1 queue=0 wait=0ms service=55ms status=200 bytes=7605

Here's my Scores model 这是我的分数模型

    class Score < ActiveRecord::Base

     belongs_to :message
     belongs_to :subscriber

    end

Scores controller has all the standard Rails CRUD scaffolding, plus this (I'm running Devise): Scores控制器具有所有标准的Rails CRUD脚手架,以及以下功能(我正在运行Devise):

    before_filter :authenticate_user!, :except => [:new, :create] 

Lastly, trying method=submit at the end of the above URL doesn't work either. 最后,在上述网址末尾尝试method = submit也不起作用。

Any thoughts? 有什么想法吗? I'm trying to figure out if it's just a syntax issue or a deeper Rails create issue. 我试图弄清楚这仅仅是语法问题还是更深层次的Rails创建问题。

Thanks. 谢谢。

New records by default in Ruby on Rails are created using POST . 默认情况下,Ruby on Rails中的新记录是使用POST创建的。 If you want to have them created using a GET request (which I would advise against) you need to create a custom route to your create action. 如果你想拥有他们创建使用GET请求(这我会建议反对),你需要创建一个自定义路由到您create行动。 For more info on the routes see: http://guides.rubyonrails.org/routing.html . 有关路线的更多信息,请参见: http : //guides.rubyonrails.org/routing.html

You need a POST request instead of GET. 您需要POST请求而不是GET。 It will not work with passing 'method' at url. 不能通过url传递“方法”。 If you really want to do that, you can add custom route, eg: 如果您确实想这样做,则可以添加自定义路线,例如:

resources :scores do
 get :make
end

Then add 'make' action at controller, which creates new record. 然后在控制器上添加“ make”操作,这将创建新记录。

But it's extremely unsafely. 但这是非常不安全的。

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

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