Rails:如果在check_box_tag中未选择任何内容,则为默认选择

时间:2012-11-26 16:23:33

标签: ruby-on-rails ruby checkbox controller action

我有一个应用程序,用户可以将项目输入数据库。

他们可以选择为项目选择多种不同的技术。目前,如果用户没有选择至少1项技术,该应用会标记错误。

我想要改变它,以便如果他们不选择技术,它会自动降低为"其他"代替。

这是我的项目控制器操作,new和create:

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> 

&#34;其他&#34;的技术ID在技​​术表中是&#34; 18&#34;。那么有没有办法说如果没有选择技术,那么: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"}

1 个答案:

答案 0 :(得分:3)

首先,应该使用字段的名称来获取参数,就像我的例子一样使用。 您收到了一系列技术ID,对吗?因此,不要使用if来检查nil,而是使用:

 params[:project][:technol_ids] ||= [18]

如果它是nil,它会将此参数转换为数组。

params[:project][:technol_ids].each do |tech_id|
   @project_technol = @project.projecttechnols.build(:technol_id => tech)
end