1

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

有一个选项,他们可以为他们的项目选择多种不同的技术。目前,如果用户未选择至少 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

I am having trouble getting this to work. I don't think I am putting it in the right bit of my code. I'm still new to rails, so please remember this when trying to help. Thanks very much

EDIT

With no technols_id selected

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"}

With 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"}
4

1 回答 1

3

First of all, the name of the field should be used to get the parameters, use like my example. You are receiving an array of tech ids, right? So instead of using if to check nil, use this:

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

It will turn this parameter into array if it is nil.

params[:project][:technol_ids].each do |tech_id|
   @project_technol = @project.projecttechnols.build(:technol_id => tech)
end
于 2012-11-26T16:46:28.807 回答