我创建了一个名为"post"的自定义RESTful操作.它在TransactionsController中显示为名为post的(公共)方法.
resources :transactions do
member :post do
post :post
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个配置如下的表单:
<form action="/transactions/25/post">
...
<input id="transaction_submit" commit="commit" type="submit" value="Post">
</form>
Run Code Online (Sandbox Code Playgroud)
当我点击"发布"按钮时,我的服务器收到:
POST "/transactions/25/post"
Run Code Online (Sandbox Code Playgroud)
我希望这在我的TransactionController中调用"post"方法,但我得到一个路由错误
ActionController::RoutingError (No route matches "/transactions/25/post"):
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢.
詹姆士
使用Rails 3.1和sqlite3进行开发,测试环境.
在迁移中添加了一个新表:
create_table :api_keys do |t|
t.string :api_key
t.integer :user_id
t.timestamps
end
Run Code Online (Sandbox Code Playgroud)
这会生成一个包含以下模式的表:
create_table "api_keys", :force => true do |t|
t.string "api_key"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Run Code Online (Sandbox Code Playgroud)
在ActiveRecord模型中:
before_create :fill_api_key
private
def fill_api_key
self.api_key = SecureRandom.hex(30)
end
Run Code Online (Sandbox Code Playgroud)
ActiveRecord的动态查找器方法find_by_api_key(api_key)不起作用(返回nil).与:相同:
ApiKey.where({:api_key => 'something'}).first
Run Code Online (Sandbox Code Playgroud)
在sqlite3中,我执行以下操作:
insert into api_keys (id, api_key) values (-1, '12345');
Run Code Online (Sandbox Code Playgroud)
如果我现在运行一个选择:
select api_keys.* from api_keys where api_keys.api_key = '12345';
Run Code Online (Sandbox Code Playgroud)
记录将被找到.
如果我运行未经过滤的选择,则会显示从我的应用程序创建的预先存在的数据:
select api_keys.* from api_keys;
Run Code Online (Sandbox Code Playgroud)
如果我尝试通过将一个预先存在的记录中的长十六进制字符串粘贴到我的查询中来查找预先存在的记录:
select api_keys.* from api_keys where api_keys.api_key = 'long hex …Run Code Online (Sandbox Code Playgroud)