【发布时间】:2021-03-16 13:16:51
【问题描述】:
Hello for begin 我无法在我的 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 | ADMIN | WALLET | |
|---|---|---|---|---|---|---|---|---|---|
| 9 | rolf@kassulke-rohan.biz | $2a$12$xpCaMAWCDqDm7QrLR98z... | 2021-03-13 17:32:59 | 2021-03-13 17:32:59 | false | 0 | |||
| 10 | malcolm@smith.net | $2a$12$SBk4Ok4qtdresgA.V7KP... | 2021-03-13 17:33:00 | 2021-03-13 17:33:00 | false | 0 | |||
| 11 | raymond@langosh.info | $2a$12$5mRP2WCS98Pa.2DF0dO.... | 2021-03-13 17:33:01 | 2021-03-13 17:33:01 | false | 0 | |||
| 12 | edwin@padberg.net | $2a$12$hUBW4ZZBUPGN06jOlV37... | 2021-03-13 17:33:02 | 2021-03-13 17:33:02 | false | 0 | |||
| 13 | setsuko@feeney.info | $2a$12$5hIY76sZyjp3GFgxX.6I... | 2021-03-13 17:33:02 | 2021-03-13 17:33:02 | false | 0 | |||
| 8 | ai@wiegand.org | $2a$12$XD.3sehDXE0WCVavfWDY... | 2021-03-13 17:32:58 | 2021-03-13 17:36:50 | false | 0 | |||
| 14 | back-phonix@lie.fr | $2a$12$Iia9pUpGDJPCeuYTUvAk... | 2021-03-13 17:38:10 | 2021-03-13 23:15:25 | true | 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
>>
【问题讨论】:
-
将
resources :faucets更改为resources :faucets, only: [:show,:index] { post 'faucet', on: :collection}应该可以工作 -
谢谢!我这样做;)
标签: ruby-on-rails ruby routes rubygems crud