在尝试使用一个字符串时遇到问题但基本上我想要一个只有一个文本框和一个提交按钮的表单,当提交时将文本框的值作为字符串传递回控制器 - 所以我可以在控制器中进行算术运算。不幸的是,我只了解ruby通过论坛传递对象。
视图(论坛所在地):
<h1>Questions#go</h1>
<p>Just a quick question</p>
<%= form_for(@answer) do |f| %>
<div class="field">
<%= f.label :answer %><br />
<%= f.text_field :answer %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
控制器:
class QuestionsController < ApplicationController
def go
end
# POST /go
def submit
@answer = :answer
if @answer = 'Ted'
@message = 'Correct'
else
@message = 'Incorrect'
end
end
end
最后路线:
Quizzer::Application.routes.draw do
resources :quizzes
get "questions/go"
post "questions/go"
match '/go', :to => 'questions#go'
root :to => 'pages#home'
get "pages/home"
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
end
我一直在努力教自己基本的铁轨几天,我正在努力奋斗。基本上我想要实现的是我在Ruby中写的(是的,我正在学习RoR只是为了能够在线实现):
class Quiz
def initialize(name)
puts "Welcome #{name}, I hope you brought your A-game."
@count = 0
@questions = ["What is 1 + 2 equal to?", "What is my name?", "What is the first letter of the alphabet?"]
@answers = ["3", "NICK", "A"]
@numberOfQuestions = @questions.length
self.startGame
end
def startGame
@gameOver = 0
while (@gameOver == 0)
self.playQuiz
self.checkOverallWin
end
end
def checkAnswer (givenAnswer)
if (givenAnswer == @answers[@count])
@count += 1
puts "Well done you answered that correctly."
else
puts "Sorry wrong answer. Here it comes again!"
end
puts ""
end
def checkOverallWin
if @count == @numberOfQuestions
@gameOver = 1
puts "Well done you have finished the quiz!"
else
@gameOver = 0
end
end
def playQuiz
puts "Question number #{@count + 1}:"
puts @questions[@count]
answer = gets()
answer.upcase!
answer.strip!
self.checkAnswer(answer)
end
end
puts "Hello, welcome to this simple game!"
puts "What is your name?"
name = gets()
name.strip!
game = Quiz.new(name)
不幸的是,我已完成的每个指南都处理了对象和OO - 我不需要 - 我只想要一种方法将文本传递给控制器,检查它是否与所需的文本匹配,如果是,请继续执行'下一个问题'和再次显示输入屏幕。
答案 0 :(得分:1)
在def submit
中阅读answer
(您要处理的字符串),您需要从params
数组中获取它:
@answer = params[:answer]
顺便说一下,在Ruby中,一切都是一个对象,所以你无法逃脱它们。)
如果您认为自己没有收到提交的内容,可以puts request.inspect
或puts params.inspect
查看请求或请求参数。
答案 1 :(得分:0)
以下是视图的简单示例:
<%= form_tag(:controller => 'home', :action => 'arithmetic', :method => 'post') %>
<%= text_field_tag(:my_thing) %>
<% end %>
然后是控制器:
class HomeController < ActionController::Base
def arithmetic
my_value = params[:my_thing]
end
end
未经测试,如果我错了,请纠正我!