将参数从视图传递到控制器。然后回到一个视图
Passing a param from a view to controller. Then back to a view
目标是从表单捕获输入,然后在控制器和服务完成后在另一个视图上显示结果。主要问题是控制器和表单视图之间的交互。视图应该将捕获的输入作为参数传递给控制器并继续其愉快的方式。如何正确捕获参数并将其传递给我的控制器?
# controller
class CheckNumberController < ApplicationController
require_relative '../services/number_check_modulo'
def get_cert_number(cert)
@cert_id = cert
end
def show_check_number
checking_policy_number = Get_policy_check_digit.new(@cert_id) # Creates a new instance of the "Get_policy_check_digit" class
# From number_check.rb
@check_number = checking_policy_number.create_check_digit # Runs the "create_check_digit" method from the "Get_policy_check_digit" class
end
end
# service 'app/services/number_check_modulo' required in the controller shown above
class Get_policy_check_digit
def initialize(cert_id)
#instance variable
@cert = cert_id
end
def create_check_digit
cert_id_6 = @cert.to_s
cert_id_6.slice!(0)
cert_id_6.to_i
cert_id_6 % 7
end
end
我目前正在使用表单助手 form_tag。用户提交号码后,页面将重定向到输出显示页面。
# Form view
<H1>Enter Cert Number below</H1>
<%= form_tag('show_check_number', method: 'get') do %>
<%= text_field_tag(:cert) %>
<%= submit_tag("Submit") %>
<% end %>
此页面仅显示服务完成的计算结果 'app/services/number_check_modulo'
# View that shows the result
<h1>Your check number is <%= @check_number %></h1>
我对此很陌生,但我认为我的方向是正确的。
尝试将方法 show_check_number 更改为此
def show_check_number
checking_policy_number = Get_policy_check_digit.new(params[:cert])
@check_number = checking_policy_number.create_check_digit
end
我建议你重构:
# controller
require 'app/services/number_check_modulo'
class CheckNumberController < ApplicationController
def show_check_number
policy_check_digit = PolicyCheckDigit.new(params[:cert])
@check_number = policy_check_digit.create_check_digit
end
end
# app/services/number_check_modulo
class PolicyCheckDigit
def initialize(cert_id)
@cert = cert_id
end
def create_check_digit
cert_id_6 = @cert.to_s
cert_id_6.slice!(0)
cert_id_6.to_i
cert_id_6 % 7
end
end
目标是从表单捕获输入,然后在控制器和服务完成后在另一个视图上显示结果。主要问题是控制器和表单视图之间的交互。视图应该将捕获的输入作为参数传递给控制器并继续其愉快的方式。如何正确捕获参数并将其传递给我的控制器?
# controller
class CheckNumberController < ApplicationController
require_relative '../services/number_check_modulo'
def get_cert_number(cert)
@cert_id = cert
end
def show_check_number
checking_policy_number = Get_policy_check_digit.new(@cert_id) # Creates a new instance of the "Get_policy_check_digit" class
# From number_check.rb
@check_number = checking_policy_number.create_check_digit # Runs the "create_check_digit" method from the "Get_policy_check_digit" class
end
end
# service 'app/services/number_check_modulo' required in the controller shown above
class Get_policy_check_digit
def initialize(cert_id)
#instance variable
@cert = cert_id
end
def create_check_digit
cert_id_6 = @cert.to_s
cert_id_6.slice!(0)
cert_id_6.to_i
cert_id_6 % 7
end
end
我目前正在使用表单助手 form_tag。用户提交号码后,页面将重定向到输出显示页面。
# Form view
<H1>Enter Cert Number below</H1>
<%= form_tag('show_check_number', method: 'get') do %>
<%= text_field_tag(:cert) %>
<%= submit_tag("Submit") %>
<% end %>
此页面仅显示服务完成的计算结果 'app/services/number_check_modulo'
# View that shows the result
<h1>Your check number is <%= @check_number %></h1>
我对此很陌生,但我认为我的方向是正确的。
尝试将方法 show_check_number 更改为此
def show_check_number
checking_policy_number = Get_policy_check_digit.new(params[:cert])
@check_number = checking_policy_number.create_check_digit
end
我建议你重构:
# controller
require 'app/services/number_check_modulo'
class CheckNumberController < ApplicationController
def show_check_number
policy_check_digit = PolicyCheckDigit.new(params[:cert])
@check_number = policy_check_digit.create_check_digit
end
end
# app/services/number_check_modulo
class PolicyCheckDigit
def initialize(cert_id)
@cert = cert_id
end
def create_check_digit
cert_id_6 = @cert.to_s
cert_id_6.slice!(0)
cert_id_6.to_i
cert_id_6 % 7
end
end