【问题标题】:Rails - Silmple Form calls a specified action of my user controllerRails - 简单表单调用我的用户控制器的指定操作
【发布时间】:2015-10-17 11:29:24
【问题描述】:

我的问题是,当我尝试从 simple_form_for 表单调用指定方法时,它不起作用。

这是我的代码:

<%= simple_form_for @user, :url => {:action => :register_iban}, :html => { :method => :post } do |f| %> 
                <div class="col-md-8">
                    <%= f.input :first_name, :label => t('user-show.payment.form.first_name'), placeholder: "Prénom" %>
                        <%= f.input :last_name, :label => t('user-show.payment.form.last_name'), placeholder: "Nom" %>
                        <%= f.input :iban, :label => t('user-show.payment.form.iban'), placeholder: "IBAN" %>
                        <%= f.input :bic, :label => t('user-show.payment.form.bic'), placeholder: "BIC" %>
                    </div>

                                    <div class="col-md-8 text-center">
                    <%= f.submit t('user-show.payment.title'), class: 'btn btn-danger' %>
                </div>
            <% end %>

所以,如您所见,我尝试从我的用户控制器调用 register_iban 方法。 但是当我这样做时,我有一个错误:没有路由匹配 {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}

每次我在控制器中创建一个新方法时,我必须在 routes.rb 文件中创建一个路由吗?在这里,我想制作这个网址:/users/5/register_iban(其中“5”是用户 ID)调用我的方法。

对不起,我从 ruby​​ 开始,我很卡住:/

【问题讨论】:

  • 你能用routes.rb代码更新你的问题吗?
  • 是的,您必须为每个控制器操作设置一个路由。有不同的方法可以做到这一点,这里有一篇很棒的文章:guides.rubyonrails.org/routing.html

标签: ruby-on-rails controller action simple-form-for


【解决方案1】:

在您的config/routes.rb 中尝试添加用户资源

resources users do
  member do
    post :register_iban
  end
end

【讨论】:

    【解决方案2】:

    没有路由匹配 {:action=>"register_iban", :controller=>"users", :id=>"5", :locale=>nil}

    此错误表示您正在尝试访问不存在的路由。

    路由定义在config/routes.rb:

    #config/routes.rb
    resources :users do
       post :register_iban
    end
    

    这将允许您通过表单调用users 控制器中的register_iban 方法。

    您还需要确保使用适当的助手调用路由:

    <%= simple_form_for @user, url: user_register_iban(@user) %>
    

    【讨论】:

      猜你喜欢
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 2015-05-08
      相关资源
      最近更新 更多