【问题标题】:rails, how to pass my collection_select value to a controllerrails,如何将我的 collection_select 值传递给控制器
【发布时间】:2017-07-02 16:15:18
【问题描述】:

我需要以一种或另一种方式将表单的选定值传递给我的控制器。我不确定我应该在路由和控制器中放入什么来获取值。

show.html.erb

<h1>Report#show</h1>

<%= form_tag new_report_path(:cname) do %>
  <label for="lookup">Lookup</label>
  <%= collection_select(:cname, 2, Company.all, :id, :name) %>
  <%= submit_tag "Submit" %>
<% end %>

控制器 report_controller.rb

def show
end

def add 
  # how do i get the :cname here?
end

路线

  root 'report#show'
  get '/report/index' => 'report#show'

我测试了什么? 我测试了我的代码,它给了我这个 url。

当前结果:http://localhost:3000/report/new.cname

预期结果:http://localhost:3000/report/:cname

********更新******

我测试了widjajayd 的解决方案。 它把这个错误返回给我.. 我还在这里提供了控制器名称

这也是我的 rake 路线

               add_reports POST       /reports/add(.:format)                    reports#add
                   reports GET        /reports(.:format)                        reports#index
                           POST       /reports(.:format)                        reports#create
                new_report GET        /reports/new(.:format)                    reports#new
               edit_report GET        /reports/:id/edit(.:format)               reports#edit
                    report GET        /reports/:id(.:format)                    reports#show
                           PATCH      /reports/:id(.:format)                    reports#update
                           PUT        /reports/:id(.:format)                    reports#update
                           DELETE     /reports/:id(.:format)                    reports#destroy
             company_index GET        /company(.:format)                        company#index
                           POST       /company(.:format)                        company#create
               new_company GET        /company/new(.:format)                    company#new
              edit_company GET        /company/:id/edit(.:format)               company#edit
                   company GET        /company/:id(.:format)                    company#show
                           PATCH      /company/:id(.:format)                    company#update
                           PUT        /company/:id(.:format)                    company#update
                           DELETE     /company/:id(.:format)                    company#destroy
                      root GET        /                                         report#show

【问题讨论】:

标签: ruby-on-rails forms routes


【解决方案1】:

这里是对上面代码的一些更正

你的路线.rb

resources :reports do
  collection {
    post :add
  }
end

你的 show.html.erb, 这是你报错后修改的版本,其实只是去掉了“(...)”见form_tag

<%= form_tag add_reports_path, :method => 'post' do  %>
  <label for="lookup">Lookup</label>
  <%= collection_select(:cname, 2, Company.all, :id, :name) %>
  <%= submit_tag "Submit" %>
<% end %>

你的控制器(我在路由中创建了集合)

def add 
  your_cname = params[:cname]
end

您可以检查自定义路线以获取和传递数据 this link

【讨论】:

  • 您好,感谢您的帮助!我试过了,它返回了一个错误。将在上面发布。
  • 好吧抱歉问题,我更正了我的答案,我已经在我的系统中测试了上面的答案,我可以得到你要求的 params[:cname]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多