ruby-on-rails - rails 5x问题可以添加没有crud的路线吗?
问题描述
您好,我无法在我的 route.rb 中添加一些内容
只是可以使用资源。
我的路线.rb
Rails.application.routes.draw do
root 'pages#home'
devise_for :users
resources :faucets
resources :posts
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
我想链接一个按钮以对我的钱包用户进行修改,原则是通过一个简单的按钮在我的用户的钱包上加钱,不要忘记仅使用资源是可能的。
我的标签用户
ID | 电子邮件 | ENCRYPTED_PASSWORD | RESET_PASSWORD_TOKEN | RESET_PASSWORD_SENT_AT | REMEMBER_CREATED_AT | CREATED_AT | UPDATED_AT | 行政 | 钱包 |
---|---|---|---|---|---|---|---|---|---|
9 | rolf@kassulke-rohan.biz | $2a$12$xpCaMAWCDqDm7QrLR98z... | 2021-03-13 17:32:59 | 2021-03-13 17:32:59 | 错误的 | 0 | |||
10 | malcolm@smith.net | $2a$12$SBk4Ok4qtdresgA.V7KP... | 2021-03-13 17:33:00 | 2021-03-13 17:33:00 | 错误的 | 0 | |||
11 | raymond@langosh.info | $2a$12$5mRP2WCS98Pa.2DF0dO.... | 2021-03-13 17:33:01 | 2021-03-13 17:33:01 | 错误的 | 0 | |||
12 | edwin@padberg.net | $2a$12$hUBW4ZZBUPGN06jOlV37... | 2021-03-13 17:33:02 | 2021-03-13 17:33:02 | 错误的 | 0 | |||
13 | setsuko@feeney.info | $2a$12$5hIY76sZyjp3GFgxX.6I... | 2021-03-13 17:33:02 | 2021-03-13 17:33:02 | 错误的 | 0 | |||
8 | ai@wiegand.org | $2a$12$XD.3sehDXE0WCVavfWDY... | 2021-03-13 17:32:58 | 2021-03-13 17:36:50 | 错误的 | 0 | |||
14 | back-phonix@lie.fr | $2a$12$Iia9pUpGDJPCeuYTUvAk... | 2021-03-13 17:38:10 | 2021-03-13 23:15:25 | 真的 | 1042 |
我的控制器
before_action :authenticate_user!
def index
end
def show
end
def faucet
if current_user.wallet >= 10000
current_user.update(wallet: 10000)
flash.alert = "your wallet is full !"
redirect_to request.referrer
else
@dice = rand(1...50)
x = current_user.wallet + @dice
current_user.update(wallet: x)
redirect_to request.referrer
end
if current_user.wallet == 0
flash.alert = "your wallet is empty !"
redirect_to request.referrer
end
end
end
index.html.erb
<p class="d-flex justify-content-center p-3">Wallet : <%= current_user.wallet %></p>
<%= button_to 'faucet', {:controller => "faucets", :action => "faucet", :method=>:post}%>
我在 application.html.erb 的链接
<li class="nav-item">
<%= link_to "faucet", faucets_path, class:"nav-link" %>
</li>
哦,我的错误
ActionController::UrlGenerationError in Faucets#index
Showing /home/floup/Bureau/_THP/35_projet_final/Nepalis_city/app/views/faucets/index.html.erb where line #4 raised:
No route matches {:action=>"faucet", :controller=>"faucets", :method=>:post}
Extracted source (around line #4):
2
3
4
<p class="d-flex justify-content-center p-3">Wallet : <%= current_user.wallet %></p>
<%= button_to 'faucet', {:controller => "faucets", :action => "faucet", :method=>:post}%>
Rails.root: /home/floup/Bureau/_THP/35_projet_final/Nepalis_city
Application Trace | Framework Trace | Full Trace
app/views/faucets/index.html.erb:4:in `_app_views_faucets_index_html_erb___3525983667023434407_19860'
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
None
x
>>
解决方案
您可以将该faucet
方法作为集合添加到faucets
资源中:
# routes.rb
resources :faucets do
collection do
post :faucet
end
end
有了这个,你的按钮就可以工作了,但你必须调整link_to
到faucet_faucets_path
.
推荐阅读
- twilio - Twilio 语音访问令牌到期时间
- flutter - 我可以在应用程序终止时在后台运行代码吗?
- html - 如何在 CSS Grid 项目周围设置内部边框?
- angular - Gitlab CI/CD - 如何知道进程何时完成
- javascript - 可以同时使用firebase和express吗?
- css - 单击时css更改占位符(无js)
- c++ - 添加 _AFXDLL 宏和 clr 支持后找不到 InterlockedAdd64
- symfony - Symfony 5 无效的防火墙“api”:找不到用户提供程序“app_user_provider”
- javascript - render.js 中的电子属性无法访问
- vue.js - Vue保护路径免受编辑的本地存储