【问题标题】:rails 5x problem it is possible to add route without crud?rails 5x 问题可以在没有crud的情况下添加路线吗?
【发布时间】: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 EMAIL 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


【解决方案1】:

您可以将faucet 方法作为集合添加到faucets 资源中:

# routes.rb
  resources :faucets do
    collection do
      post :faucet
    end
  end

有了这个,你的按钮就可以工作了,但是你必须将link_to调整为faucet_faucets_path

【讨论】:

  • 哦,好的,谢谢 ;) 我测试了这个!但是如果没有帖子,我也不可能这样做(也许很愚蠢,但我是初学者^^)
  • 是的,你也可以用 get 代替 post。
  • 不,如果可能的话,我想在没有 get 或 post 的情况下创建一条新路线,我看到补丁是可能的
【解决方案2】:

最后我用这个:

resources :faucets do
    collection do
      patch :faucet
    end
  end

【讨论】:

    猜你喜欢
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2016-11-25
    • 2014-10-03
    • 2011-01-14
    • 1970-01-01
    相关资源
    最近更新 更多