I am following the Rails Tutorial by Michael Hart and I am already on Chapter 7. But I wanna do something different right now, which the tutorial doesn't teach. I wanna run a script file inside my webpage. How I can do that? I saw other posts here saying to use Sinatra, but since I am following this tutorial I don't think it is such a good idea to use it because it can make everything different from the tutorial.
Here is the simple script I wanna run on my webpage:
#Somando idades
def soma_vetor_excluindo(index,vet)
soma = 0
for i in 0..9
if(i!=index)
soma = soma + vet[i].to_i
end
end
return soma
end
def soma_vetor(vet)
soma = 0
for i in 0..9
soma = soma + vet[i].to_i
end
return soma
end
def maior_vetor(vet)
maior = 0
for i in 0..9
if(maior < vet[i])
maior = vet[i]
end
end
return maior
end
idades = (0..9).collect{rand(99)+1}
soma_idades = (0..9).collect{0} soma = 0
print "#{idades} \n"
for i in 0..9
soma_idades[i] = soma_vetor_excluindo(i,idades)
end
print "#{soma_idades} \n"
div = soma_vetor(soma_idades) / 9
resp = div - maior_vetor(soma_idades)
puts "#{resp}"
The simplest way to do it would be to make the method soma_vetor_excluindo, soma_vetor, maior_vetor, etc, controller methods, so when you send data through a form or ajax, the action would trigger, calculate the values and return you a result.
Knowing this, you can have a controller, let's say MathController.rb, and inside it, the soma_vetor_excluindo method:
class MathController < ApplicationController
def soma_vetor_excluindo
end
def soma_vetor
end
def maior_vetor
end
end
To access this, you probably need a route, so on your routes.rb add something like this:
get 'math/soma_vetor_excluindo/:index/:vet', to 'math#soma_vetor_excluindo'
get 'math/soma_vetor/:vet', to 'math#soma_vetor'
get 'math/maior_vetor/:vet', to 'math#maior_vetor'
This means that when your browser hit localhost/math/soma_vetor_excluindo/1/2 or the other urls, it would send a get request to the controller calling the soma_vetor_excluindo method and putting in the parameters, params[:index] and params[:vet], so theoretically the script would run.
The thing is, you can adapt your controller to do something like this with very little work.
I believe the simplest solution is to load a page per script. First you add a path for your script into the routes.rb with something like:
get 'scripts/your_script', to 'scripts#your_script
And in the controller (app/scripts_controller.rb) you should add your code like this:
class ScriptsController < ApplicationController
#Somando idades
def soma_vetor_excluindo(index,vet)
soma = 0
for i in 0..9
if(i!=index)
soma = soma + vet[i].to_i
end
end
return soma
end
def soma_vetor(vet)
soma = 0
for i in 0..9
soma = soma + vet[i].to_i
end
return soma
end
def maior_vetor(vet)
maior = 0
for i in 0..9
if(maior < vet[i])
maior = vet[i]
end
end
return maior
end
def your_script
idades = (0..9).collect{rand(99)+1}
soma_idades = (0..9).collect{0}
soma = 0
answer = "#{idades} \n"
for i in 0..9
soma_idades[i] = soma_vetor_excluindo(i,idades)
end
answer << "#{soma_idades} \n"
div = soma_vetor(soma_idades) / 9
resp = div - maior_vetor(soma_idades)
answer << "#{resp}"
render(text: answer)
end
end
when you access the page scripts/your_script, it should render a plain text presentation of your script result.
Although this is not the most elegant solution, it should solve your problem.
Related
Supposing I have a Survey rails model that has_many: questions and a need to be able to resequence the questions...
Where would I put the renumber_questions method (Survey model, Question model, or other class, and why?) and how would this ugly method below best be written following the rules?
def renumber_questions
last_page = 0
new_page = 0
new_seq = 0
questions.unscope(:order).order(page: :asc, seq: :asc).each do |question|
if last_page != question.page
new_page = new_page + 1
end
last_page = question.page
new_seq = new_seq + 1
question.page = new_page
question.seq = new_seq
question.save
end
end
Put it on another class, because of Single Responsibility (mentioned in the link you gave).
Despite the large number of private methods we wrote, keeping classes short proved easy. It forced us to consider what the single responsibility of our class was, and what should be extracted.
class RenumberQuestions
def initialize(questions)
#questions = questions.unscope(:order).order(page: :asc, seq: :asc)
#last_page = 0
#new_page = 0
#new_seq = 0
end
def call
#questions.each do |question|
counting_var(question)
mutate_(question)
end
end
def counting_var(question)
#new_page += 1 if #last_page != question.page
#last_page = question.page
#new_seq += 1
end
def mutate_(question)
question.page = #new_page
question.seq = #new_seq
question.save
end
end
Sorry for the bad naming. I don't know what you do, so I just naming it as I understand. And you can make this more clean.
I am learning Rails and I stuck with this custom validation. I tried many different approaches to pass the form field called (:cpf) to validate without success. I also tried using ActiveModel::EachValidator, but I get more confused. The validation is a check digit using modulo 11. So this is what I ended up.
controllers/drivers_controller.rb
def new
#driver = Driver.new
end
def create
#driver = Driver.new(driver_params)
if #driver.save
flash[:success] = "Motorista cadastrado com sucesso!"
redirect_to #driver
else
render 'new'
end
end
models/driver.rb
class Driver < ApplicationRecord
validate :checkcpf
private
def checkcpf
return false if cpf.nil?
nulos = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000}
valor = cpf.scan /[0-9]/
if valor.length == 11
unless nulos.member?(valor.join)
valor = valor.collect{|x| x.to_i}
soma = 10*valor[0]+9*valor[1]+8*valor[2]+7*valor[3]+6*valor[4]+5*valor[5]+4*valor[6]+3*valor[7]+2*valor[8]
soma = soma - (11 * (soma/11))
resultado1 = (soma == 0 or soma == 1) ? 0 : 11 - soma
if resultado1 == valor[9]
soma = valor[0]*11+valor[1]*10+valor[2]*9+valor[3]*8+valor[4]*7+valor[5]*6+valor[6]*5+valor[7]*4+valor[8]*3+valor[9]*2
soma = soma - (11 * (soma/11))
resultado2 = (soma == 0 or soma == 1) ? 0 : 11 - soma
return true if resultado2 == valor[10] # CPF VALIDO
end
end
end
return false # CPF INVALIDO
errors.add(:cpf, "CPF INVÁLIDO")
end
end
I think I am missing how to pass my cpf field to validate. Either I got a error message or miss the validation.
I will try to use ActiveModel::EachValidator later when I get better understanding from Rails. This is how I usually do, try to get things to work the way I can understand then refactor the Rails way.
I appreciate any help! Thank you!
First of all, no need to return either true or false from this method, just return - you want to either return from the method because the field is valid or add an error. I didn't quite follow the logic of your validation, but regardless of the result you always return false # CPF INVALIDO and the line errors.add(:cpf, "CPF INVÁLIDO") is never reached. Bear in mind - whenever you return from a method, you stop the execution, so you have to be sure that there is at least one execution path, that can reach the line, which adds the actual error.
I have a rails 4 app. In the controller, I iterate through each assignment entry in the database to check if a requirement is associated with more than one assignment. However, I can't figure out how to add a counter, i.e. hit = 0, hit = 1, etc. to the controller.
EDIT: The relationship between assignment and requirement is HABTM.
My code is below:
def check_requirements
#assignments = Assignment.all
#assignment = Assignment.find(params[:id])
#requirement = Requirement.find(params[:requirement_id])
#assignments.each do |assignment|
if assignment.include(requirement)
#here's where the counter should go
end
end
if counter is greater than zero or one, do nothing
else #assignment.delete(requirement)
end
You can use Enumerable#each_with_index
def check_requirements
#assignments = Assignment.all
#assignment = Assignment.find(params[:id])
#requirement = Requirement.find(params[:requirement_id])
#assignments.each_with_index do |assignment,hit|
if assignment.include(requirement)
p "hit : #{hit}"
end
end
if counter is greater than zero or one, do nothing
else #assignment.delete(requirement)
end
def check_requirements
#assignments = Assignment.all
#assignment = Assignment.find(params[:id])
#requirement = Requirement.find(params[:requirement_id])
counter = 0
#assignments.each do |assignment|
if assignment.include(requirement)
counter+=1
end
end
if counter is greater than zero or one, do nothing
else #assignment.delete(requirement)
end
Hi I create a controller Game to display a Q/A game
And I am blocked with <<, here is the code
def play
lvlup(lvl)
if lvl == 1
set_questions
else
get_questions
end
#answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
#answer ||= []
#answers << question.answer
#answers = #answers.shuffle
render 'play'
end
I create an array and I put the related answer in the global answers I want to display 4 Max.
Why does the undefined is here?
Here is the total code
class GamesController < ApplicationController
attr_accessor :lvl
def welcome
end
def congrat
end
def play
lvlup(lvl)
if lvl == 1
set_questions
else
get_questions
end
#answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
#answer ||= []
#answers << question.answer
#answers = #answers.shuffle
render 'play'
end
def loose
#question = Question.find(params[:question])
flash.now[:alert] = "Miss..."
render 'loose'
end
def check
#lvl = params[:lvl].to_i
answer_id = params[:id].to_i
question = Question.find(params[:question])
if #lvl == lvlmax
render action: 'congrat' and return
elsif answer_id == question.answer_id
flash.now[:notice] = "Well done !"
play
else answer_id != question.answer_id
loose
end
end
private
def lvlup(value)
#lvl = 1 + value.to_i
end
def lvlmax
#lvlmax = Question.all.count
end
def set_questions
#questionsids = []
Question.all.shuffle.each do |d|
#questionsids << d.id
end
cookies[:questions] = #questionsids
end
def get_questions
#questions = cookies[:questions].split('&')
end
def questions
#questions = cookies[:questions]
end
def question
#question = Question.find(questions[lvl])
end
end
Thank you for your help.
You are trying to append to the #answers result - this is an ActiveRecord relation, you cannot append data to that array.
Add .to_a in the end of your line where you set #answers to allow you to append to the array.
#answers = Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()").to_a
mtrolle's answer might be correct, but I have my doubts as to why ActiveRecord::Relation was not returned as Array by default. (Also as mentioned by BroiStatse in his comment.)
I too noticed the same problem with my local setup however it was attributed to another issue all together. I am sharing this here in case you too happen to use MySQL.
Answer.where.not(id: question.answer_id).limit(2).order("RANDOM()")
returns an ActiveRecord::Relation object. And it translates to following SQL:
SELECT `answers`.* FROM `answers` WHERE (id != ID) ORDER BY RANDOM() LIMIT 2
When I try running the same in MySQL, I get:
ERROR 1305 (42000): FUNCTION database.RANDOM does not exist
Apparently MySQL does not have RANDOM() function, instead it uses RAND().
Converting ActiveRecord query accordingly returned correct Array to me:
Answer.where.not(id: question.answer_id).limit(2).order("RAND()")
I'm trying to generate stats for a character created by a form. The user inputs the name, race, class, alignment, and whether or not the stats will be generated randomly, or prioritized (values being assigned from highest to lowest). The form works flawlessly, as I can see the output in a view.
What I am now trying to do is call a method from a class in /lib in the model that will generate the stats; however, I keep getting the following error (I can't post pictures):
NoMethodError in CharactersController#create
undefined method `[]' for nil:NilClass
Extracted source (around line #14):
12 before_save do
13 generate_stats
14 self.strength = #character_stats[:strength]
15 self.dexterity = #character_stats[:dexterity]
16 self.constitution = #character_stats[:constitution]
17 self.intelligence = #character_stats[:intelligence]
Here is a copy of some of my code:
In controllers\characters_controller.rb
class CharactersController < ApplicationController
def create
#character = Character.new(character_info_params)
#character.name = params[:character][:name].capitalize
#character.alignment = "#{params[:character][:alignment_lr]} #{params[:character][:alignment_ud]}"
if #character.save
redirect_to #character
else
render 'new'
end
end
private
def character_info_params
params.require(:character).permit(:name, :race, :class_, :alignment)
end
end
In models\character.rb
class Character < ActiveRecord::Base
require 'random_stats_generator'
attr_accessor :rand_stat_gen
def generate_stats
if #rand_stat_gen == true
#character_stats_inst = RandomStatGenerator.new
#character_stats = #character_stats_inst.generate
end
end
before_save do
generate_stats
self.strength = #character_stats[:strength]
self.dexterity = #character_stats[:dexterity]
self.constitution = #character_stats[:constitution]
self.intelligence = #character_stats[:intelligence]
self.wisdom = #character_stats[:wisdom]
self.charisma = #character_stats[:charisma]
end
#validation passed this point
end
In initializers\stat_builders.rb
require "./lib/random_stat_generator.rb"
In lib/random_stat_generator.rb
class RandomStatGenerator
def initialize
#strength = :strength
#dexterity = :dexterity
#constitution = :constitution
#intelligence = :intelligence
#wisdom = :wisdom
#charisma = :charisma
#character_stats = HashWithIndifferentAccess.new()
end
def self.generate
roll_stats
end
def roll(stat)
#roll_value_1 = (1 + (rand(6)))
#roll_value_2 = (1 + (rand(6)))
#roll_value_3 = (1 + (rand(6)))
#roll_value_4 = (1 + (rand(6)))
#roll_array = [#roll_value_1,#roll_value_2,#roll_value_3,#roll_value_4]
#roll_array = #roll_array.sort_by {|x| x }
#roll_array = #roll_array.reverse
stat = #roll_array[0] + #roll_array[1] + #roll_array[2]
end
def roll_stats
#strength = roll(#strength)
#dexterity = roll(#dexterity)
#constitution = roll(#constitution)
#intelligence = roll(#intelligence)
#wisdom = roll(#wisdom)
#charisma = roll(#charisma)
#character_stats[:strength] = #strength
#character_stats[:dexterity] = #dexterity
#character_stats[:constitution] = #constitution
#character_stats[:intelligence] = #intelligence
#character_stats[:wisdom] = #wisdom
#character_stats[:charisma] = #charisma
return #character_stats
end
end
To me, it looks like the method isn't returning anything, or isn't being called at all.
I've tried a lot of solutions that I've come across online, none of them working. There may be some things that don't really make sense that are left over from these solutions. I'm only just starting with rails, so I'm still trying to get used to everything.
Thanks a lot for your help.
Ruby has really powerful functions for manipulating both hashes and arrays.
Typing out duplicate assignments like:
self.strength = #character_stats[:strength]
self.dexterity = #character_stats[:dexterity]
self.constitution = #character_stats[:constitution]
Is pretty dull. So instead we can simply rewrite the methods to pass hashes around.
class RandomStatGenerator
# This is just a constant containing all the stats we want to generate.
STATS = [:strength, :dexterity, :constitution, :intelligence, :wisdom, :charisma]
# Create a hash with random roll values for each stat
def self.roll_stats
# This is kind of scary looking but actually just creates an
# hash from an array of keys
Hash[STATS.map {|k| [k, self.roll ] } ]
end
private
def self.roll
# Create an array with 4 elements (nil)
ary = Array.new(4)
# We then replace the nil value with a random value 1-6
ary = ary.map do
(1 + (rand(6)))
end
# sort it and drop the lowest roll. return the sum of all rolls.
ary.sort.drop(1).sum
# a ruby ninja writes it like this
Array.new(4).map { 1 + rand(6) }.sort.drop(1).sum
end
end
Output:
irb(main):032:0> RandomStatGenerator.roll_stats
=> {:strength=>14, :dexterity=>14, :constitution=>14, :intelligence=>13, :wisdom=>10, :charisma=>9}
But if you don't intend to actually create instances of a class, than you should use a module instead.
Rails models can either be created with a hash or you can replace its values with a hash:
Character.new(RandomStatGenerator.roll_stats)
#character.assign_attributes(RandomStatGenerator.roll_stats)
So we can use this in Character#generate_stats:
def generate_stats
assign_attributes(RandomStatGenerator.roll_stats)
end
You should use ActiveModel callbacks with extreme prejudice. It is often quite a challenge to regulate where in your application and when in the model lifetime. Since before_save runs after validations means that any validations like validates_presence_of :constitution will fail.
In your case it might be better to simply do it in the controller or use:
before_validation :generate_stats, if: -> { new_record? && #rand_stat_gen }
I would like to suggest the following organisation fo your library
# Use a module at top level
module RandomStatGenerator
STATS = [:strength, :dexterity, :constitution, :intelligence, :wisdom, :charisma]
# Use a class Stats if you need to but I don't see why...
class Stats
def initialize
RandomStatGenerator::STATS.each do |stat|
# Below line will do #stat = :stat
instance_variable_set("##{stat.to_s}", stat)
#character_stats = HashWithIndifferentAccess.new()
end
def roll_stats
#character_stats = RandomStatGenerator.roll_stats
end
end
module_function
# below lines will be considered as module functions
# => call RandomStatGenerator.function_name
def roll
roll_value_1 = (1 + (rand(6)))
roll_value_2 = (1 + (rand(6)))
roll_value_3 = (1 + (rand(6)))
roll_value_4 = (1 + (rand(6)))
roll_array = [roll_value_1,roll_value_2,roll_value_3,roll_value_4]
roll_array = roll_array.sort_by {|x| x }
roll_array = roll_array.reverse
roll_array[0] + roll_array[1] + roll_array[2]
end
def roll_stats
character_stats = {}
STATS.each do |stat|
character_stats[stat] = RandomStatGenerator.roll
end
return character_stats
end
end
Then in your character.rb
def generate_stats
#character_stats = RandomStatGenerator.roll_stats
end