Ruby on Rails - creating and using a custom method

I am new to Rails and really appreciate any help. I created the following method:

  def name_fix
    name = self.split
    mod_name = []
    name.each do |n|
      n.split("")
      if n[0]
        n.upcase
      else
        n.downcase
      end
      mod_name.push(n)
    end
    mod_name.join
  end

I would like to use this method in my controller as such:

def create
  @patient = Patient.new(params[:patient])
  @patient.name = params[:params][:name].name_fix
  if @patient.save
    redirect_to patients_path
  else
    render :new
  end
end

How can i do this? Will this method be inside my model or controller? I used to encounter an undefined method error.

Note. I am sure there is a better way to write my code. I am also grateful for the help in this.

+4
source share
4 answers

Methods called directly are best placed in a controller (or in ApplicationControllerif you think more than one controller can use it).

These are methods such as

# app/controllers/my_controller.rb

def foo(bar)
  # do something here
end

def create
  id = params[:id]
  value = foo(id)
end

, , . - .

# app/models/my_model.rb
def full_name
 first_name + " " + last_name
end

# app/controller/my_controller.rb
def create
  id = params[:id]
  model = MyModel.find(id)
  full_name = model.full_name
end

name_fix ON, params[:params][:name], ( ) a String.

  • String, name_fix. . "monkeypatching" . , .

  • ApplicationController, .

    @patient.name = name_fix (params [: params] [: name])

: , ... . , , , Ruby , . ,

def create
  @patient = Patient.new(params[:patient])

  # 1. Be descriptive with your method names. `name_fix` is vague
  # 2. Why is `:name` nested under another `[:params]` hash?
  @patient.name = capitalize_name(params[:name])

  if @patient.save
    # 1. I think `patient_path` has to be singular
    # 2. It needs a `Patient` object to know how to construct the URL
    #     e.g. `/patients/:id`
    redirect_to patient_path(@patient)
  else
    render :new
  end
end


def capitalize_name(full_name)
  # Example: julio jones
  # 
  # 1. `split` produces an array => ["julio", "jones"]
  # 2. `map` applies a function (`capitalize`) to each element
  #       => ["Julio", "Jones"]
  # 3. `join(" ")` rejoins it => "Julio Jones"
  full_name.split.map(&:capitalize).join(" ")
end
0
#app/models/patient.rb
class Patient < ActiveRecord::Base

   protected

   def name=(value)
      mod_name = []
      value.split.each do |n|
         n.split("")
         type = n[0] ? "up" : "down"
         n.send("#{type}case")
         mod_name.push(n)
      end
      @name = mod_name.join
   end
end

#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
   def create
      @patient = Patient.new patient_params
      @patient.save ? redirect_to(patients_path) : render(:new)
  end

  private

  def patient_params
     params.require(:patient).permit(:name)
  end
end

, setter, . .


, - .

, .

, , / , .

-

- Rails - , . Ruby - , , .

enter image description here

, . , class (IE, ), (IE ).

"class" (Model.method) "" (@model.method) :

#app/models/patient.rb
class Patient < ActiveRecord::Base
   def explode
      #this is an instance method
      puts "Instance Explode"
   end

   def self.explode
      #this is a class method
      puts "Exploded"
   end
end

, :

@patient = Patient.find params[:id]
@patient.explode #-> "Instance explode"

Patient.explode #-> "Exploded"

-

, , , .

, , , .

...

@patient.name = params[:params][:name].name_fix .

, .name_fix , . .name_fix , , , , helper:

#app/helpers/patients_helper.rb
class PatientsHelper
   def name_fix value
      # stuff here
   end
end

#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
   def create
      @patient.name = name_fix params[:patient][:name]
   end
end

.name , name=. , , .

+2

, name_fix , , name :

# app/controllers/patient_controller.rb
private
def name_fix(name)
  name.split.map(&:capitalize).join(" ")
end

@patient.name = name_fix(params[:params][:name])

create.

, :

# app/models/patient.rb
def self.name_fix(name)
  name.split.map(&:capitalize).join(" ")
end

:

@patient.name = Patient.name_fix(params[:params][:name])

name_fix capitalize_name.

0

,

  def create
    @patient = Patient.new(params[:patient])
    @patient.name = params[:params][:name]
    @patient = @patient.name_fix
     if @patient.save
        redirect_to patients_path
     else
       render :new
     end
   end

.

0

Source: https://habr.com/ru/post/1611879/


All Articles