app/controllers/guidebooks_controller.rb
# Never trust parameters from the scary internet, only allow the white list through.
Never trust parameters from the scary internet, only allow the white list through.
Never trust parameters from the scary internet only allow the white list through .
def guidebook_params
      split_val = ";"
      organizer = params[:guidebook][:organizer_id] || params[:guidebook][:organizer]
      if organizer.class == String
        new_organizer = Organizer.find_by_name(organizer)
        unless new_organizer.nil?
          new_user = new_organizer.user
          params[:guidebook][:user] = new_user
          params[:guidebook][:organizer] = new_organizer
        end
      end
      if params[:guidebook][:tags] == "" or params[:guidebook][:tags].nil?
        params[:guidebook][:tags] = []
      else
        tags_to_array = params[:guidebook][:tags].split(split_val)
        tags = []
        tags_to_array.each do |t|
          tags.push Tag.find_or_create_by(name: t)
        end
        params[:guidebook][:tags] = tags
      end
      if params[:guidebook][:languages] == "" or params[:guidebook][:languages].nil?
        params[:guidebook][:languages] = []
      else
        langs_to_array = params[:guidebook][:languages].split(split_val)
        langs = []
        langs_to_array.each do |l|
          langs.push Language.find_or_create_by(name: l)
        end
        params[:guidebook][:languages] = langs
      end
      pkg_attr = params[:guidebook][:packages]
      if !pkg_attr.nil?
        post_data = []
        pkg_attr.each do |p|
          included_array = p[1]["included"].split(split_val)
          post_data.push Package.create(name: p[1]["name"], value: p[1]["value"], percent: p[1]["percent"], description: p[1]["description"], included: included_array)
        end
        params[:guidebook][:packages] = post_data
      end
      if params[:guidebook][:included] == "" or params[:guidebook][:included].nil?
        params[:guidebook][:included] = []
      else
        included_to_array = params[:guidebook][:included].split(split_val)
        included = []
        included_to_array.each do |i|
          included.push i
        end
        params[:guidebook][:included] = included
      end
      if params[:guidebook][:nonincluded] == "" or params[:guidebook][:nonincluded].nil?
        params[:guidebook][:nonincluded] = []
      else
        nonincluded_to_array = params[:guidebook][:nonincluded].split(split_val)
        nonincluded = []
        nonincluded_to_array.each do |i|
          nonincluded.push i
        end
        params[:guidebook][:nonincluded] = nonincluded
      end
      current_category = params[:guidebook][:category]
      if current_category == "" or current_category.nil?
        params[:guidebook][:category] = Category.find_or_create_by(name: 'Outras')
      else
        begin
          params[:guidebook][:category] = Category.find(current_category)
        rescue => e
          params[:guidebook][:category] = Category.find_or_create_by(name: current_category)
        end
      end
      params[:guidebook][:currency] = "BRL"
      params.require(:guidebook).permit!
    end
def guidebook_params split_val = ";" organizer = params [ :guidebook ] [ :organizer_id ] || params [ :guidebook ] [ :organizer ] if organizer . class == String new_organizer = Organizer . find_by_name ( organizer ) unless new_organizer . nil? new_user = new_organizer . user params [ :guidebook ] [ :user ] = new_user params [ :guidebook ] [ :organizer ] = new_organizer end end if params [ :guidebook ] [ :tags ] == "" or params [ :guidebook ] [ :tags ] . nil? params [ :guidebook ] [ :tags ] = [ ] else tags_to_array = params [ :guidebook ] [ :tags ] . split ( split_val ) tags = [ ] tags_to_array . each do | t | tags . push Tag . find_or_create_by ( name : t ) end params [ :guidebook ] [ :tags ] = tags end if params [ :guidebook ] [ :languages ] == "" or params [ :guidebook ] [ :languages ] . nil? params [ :guidebook ] [ :languages ] = [ ] else langs_to_array = params [ :guidebook ] [ :languages ] . split ( split_val ) langs = [ ] langs_to_array . each do | l | langs . push Language . find_or_create_by ( name : l ) end params [ :guidebook ] [ :languages ] = langs end pkg_attr = params [ :guidebook ] [ :packages ] if ! pkg_attr . nil? post_data = [ ] pkg_attr . each do | p | included_array = p [ 1 ] [ "included" ] . split ( split_val ) post_data . push Package . create ( name : p [ 1 ] [ "name" ] , value : p [ 1 ] [ "value" ] , percent : p [ 1 ] [ "percent" ] , description : p [ 1 ] [ "description" ] , included : included_array ) end params [ :guidebook ] [ :packages ] = post_data end if params [ :guidebook ] [ :included ] == "" or params [ :guidebook ] [ :included ] . nil? params [ :guidebook ] [ :included ] = [ ] else included_to_array = params [ :guidebook ] [ :included ] . split ( split_val ) included = [ ] included_to_array . each do | i | included . push i end params [ :guidebook ] [ :included ] = included end if params [ :guidebook ] [ :nonincluded ] == "" or params [ :guidebook ] [ :nonincluded ] . nil? params [ :guidebook ] [ :nonincluded ] = [ ] else nonincluded_to_array = params [ :guidebook ] [ :nonincluded ] . split ( split_val ) nonincluded = [ ] nonincluded_to_array . each do | i | nonincluded . push i end params [ :guidebook ] [ :nonincluded ] = nonincluded end current_category = params [ :guidebook ] [ :category ] if current_category == "" or current_category . nil? params [ :guidebook ] [ :category ] = Category . find_or_create_by ( name : 'Outras' ) else begin params [ :guidebook ] [ :category ] = Category . find ( current_category ) rescue => e params [ :guidebook ] [ :category ] = Category . find_or_create_by ( name : current_category ) end end params [ :guidebook ] [ :currency ] = "BRL" params . require ( :guidebook ) . permit! end
Never trust parameters from the scary internet, only allow the white list through.
Never trust parameters from the scary internet only allow the white list through .
# unpermitted params error even with all params provided #params.require(:guidebook).permit(:title, :currency, :picture, :organizer_id, :file, :value, :description, :status, :category_id, user: [:id, :name], organizer: [:id, :name], tags: [:id, :name], languages: [:id, :name], category: [:id, :name], packages_attributes: [:id, :name, :value, :description, :percent, :included], wheres_attributes: [:id, :name, :place_id, :background_id, :lat, :long, :city, :state, :country, :postal_code, :address, :google_id, :url], included: [], nonincluded: []).merge(params[:guidebook])