【发布时间】:2012-11-26 16:23:32
【问题描述】:
我有一个应用程序,用户可以在其中将项目输入数据库。
有一个选项,他们可以为他们的项目选择多种不同的技术。目前,如果用户未选择至少 1 项技术,应用会标记错误。
我想改变这一点,如果他们不选择一种技术,它会自动降为“其他”。
这是我的项目控制器操作,新建和创建:
def new
@project = Project.new
@technol = Technol.new(params[:tech])
@all_technols = Technol.order('tech ASC')
@all_technols = Technol.all
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
def create
@project = Project.new(params[:project])
@project.client = params[:new_client] unless params[:new_client].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?
if !params[:technols].nil?
params[:technols][:id].each do |tech|
if !tech.empty?
@project_technol = @project.projecttechnols.build(:technol_id => tech)
end
end
end
这是技术领域的新观点
<ul>
<% @all_technols.each do |technol| %>
<li class="split">
<%= check_box_tag "project[technol_ids][]", technol.id, @project.technols.include?(technol) %>
<%= technol.tech %>
</li>
<% end %>
</ul>
技术表中“其他”的技术 ID 为“18”。那么有没有办法说如果不选择技术,那么:technol_id => ["18"]。
我之前得到了帮助,有人建议我应该添加这个:
def create
...
technol_ids = params[:technol_ids].blank? ? [18] : params[:technol_ids]
technol_ids.each do |id|
...
end
...
end
我无法让它工作。我不认为我把它放在我的代码的正确位置。我对 Rails 还很陌生,所以在尝试提供帮助时请记住这一点。非常感谢
编辑
没有选择 technols_id
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"8Hgc1GXqNhWkzO3Wgkfpf6z+fdImf6QvAv0XLbP0a5g=", "project"=>{"project
_name"=>"Test", "archive"=>"0", "username"=>"test", "status"=>"Active", "exception_pm"=>"", "client"=>"", "business_d
iv"=>"", "project_owner"=>"", "start_date"=>"11-12-2012", "first_name"=>"test", "last_name"=>"test", "fullname"=>
"test test", "entry_date"=>"2012-11-26", "end_date"=>"19-12-2012", "techinfo"=>"Test", "role"=>"", "industry"=>""
, "summary"=>"Test", "lessons_learned"=>"Test", "customer_benefits"=>"Test", "financials"=>"£500,000 - £999,999 "}, "n
ew_exception_pm"=>"Test", "new_client"=>"Test", "new_business_div"=>"Test", "new_project_owner"=>"Test", "new_role"=>"Te
st", "new_industry"=>"Test", "commit"=>"Save New Project"}
有 1 个 technols_id
Started POST "/projects" for 192.168.16.127 at 2012-11-26 16:47:27 +0000
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"8Hgc1GXqNhWkzO3Wgkfpf6z+fdImf6QvAv0XLbP0a5g=", "project"=>{"project
_name"=>"Test", "archive"=>"0", "username"=>"Test", "status"=>"Active", "exception_pm"=>"", "client"=>"", "business_d
iv"=>"", "project_owner"=>"", "start_date"=>"11-12-2012", "first_name"=>"Test", "last_name"=>"McLaughlin", "fullname"=>
"Test Test", "entry_date"=>"2012-11-26", "end_date"=>"20-12-2012", "technol_ids"=>["1"], "techinfo"=>"Test", "rol
e"=>"", "industry"=>"", "summary"=>"Test", "lessons_learned"=>"Test", "customer_benefits"=>"Test", "financials"=>"£250,
000 - £499,999 "}, "new_exception_pm"=>"Test", "new_client"=>"Test", "new_business_div"=>"Test", "new_project_owner"=>"
Test", "new_role"=>"Test", "new_industry"=>"Test", "commit"=>"Save New Project"}
【问题讨论】:
-
当您提交没有 technol_ids 选择的表单时,
params的输出是什么?并且至少选择了一个? -
我已将我的日志添加到问题中
-
尝试使用
params[:project][:technol_ids] ||= [18]创建操作的第一行。如果params[:project][:technol_ids]为nil,它将使用[18]设置technol_ids数组。但正如@Rodrigo Dias 所说,像这样设置默认 id 并不是一个好主意。您可以有一个技术类方法,例如self.default_technology_id返回 id。
标签: ruby-on-rails ruby checkbox controller action