Forum: Ruby on Rails a problem http POST params

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Learning (Guest)
on 2009-05-12 19:19
(Received via mailing list)
this is my HTML:
<form method = "post" action = "form/index">
<input type = "text" name = "username" />
</form>

and the form_controller:
class FormController < ApplicationController
  def index
      @username=params[:username]
  end
end

what's wrong about this? it works with GET method.
but the API shows that params() Returns both GET and POST \parameters
in a single hash.
there's the error message below

 ActionController::InvalidAuthenticityToken in FormController#index

ActionController::InvalidAuthenticityToken

RAILS_ROOT: /home/Learning/workspace/myapp
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
request_forgery_protection.rb:79:in `verify_authenticity_token'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:178:in `send'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:178:in `evaluate_method'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:166:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:225:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:629:in `run_before_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:615:in `call_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:610:in `perform_action_without_benchmark'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
rescue.rb:160:in `perform_action_without_flash'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
flash.rb:141:in `perform_action'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in `send'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in `process_without_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:606:in `process'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:391:in `process'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:386:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
routing/route_set.rb:433:in `call'

/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
request_forgery_protection.rb:79:in `verify_authenticity_token'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:178:in `send'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:178:in `evaluate_method'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:166:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:225:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:629:in `run_before_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:615:in `call_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:610:in `perform_action_without_benchmark'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
rescue.rb:160:in `perform_action_without_flash'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
flash.rb:141:in `perform_action'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in `send'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in `process_without_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:606:in `process'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:391:in `process'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:386:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
routing/route_set.rb:433:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:88:in `dispatch'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:111:in `_call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:82:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:29:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:29:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/query_cache.rb:34:in `cache'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:9:in `cache'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:28:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/connection_pool.rb:361:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/head.rb:9:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/methodoverride.rb:24:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
params_parser.rb:15:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
rewindable_input.rb:25:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
session/cookie_store.rb:93:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
reloader.rb:9:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
failsafe.rb:11:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:106:in `call'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/static.rb:31:in
`call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:46:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:40:in `each'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:40:in `call'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/log_tailer.rb:
17:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/content_length.rb:13:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/handler/webrick.rb:46:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/handler/webrick.rb:13:in `run'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
script/server:3

/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
request_forgery_protection.rb:79:in `verify_authenticity_token'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:178:in `send'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:178:in `evaluate_method'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
callbacks.rb:166:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:225:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:629:in `run_before_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:615:in `call_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:610:in `perform_action_without_benchmark'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
benchmarking.rb:68:in `perform_action_without_rescue'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
rescue.rb:160:in `perform_action_without_flash'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
flash.rb:141:in `perform_action'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in `send'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:523:in `process_without_filters'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
filters.rb:606:in `process'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:391:in `process'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
base.rb:386:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
routing/route_set.rb:433:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:88:in `dispatch'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:111:in `_call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:82:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:29:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:29:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/query_cache.rb:34:in `cache'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:9:in `cache'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:28:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/connection_pool.rb:361:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/head.rb:9:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/methodoverride.rb:24:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
params_parser.rb:15:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
rewindable_input.rb:25:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
session/cookie_store.rb:93:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
reloader.rb:9:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
failsafe.rb:11:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/lock.rb:11:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
dispatcher.rb:106:in `call'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/static.rb:31:in
`call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:46:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:40:in `each'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/urlmap.rb:40:in `call'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/log_tailer.rb:
17:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/content_length.rb:13:in `call'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/handler/webrick.rb:46:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
vendor/rack-1.0/rack/handler/webrick.rb:13:in `run'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
/usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
script/server:3
-e:2:in `load'
-e:2

Request

Parameters:

{"username"=>"123"}

Show session dump

---

Response

Headers:

{"Content-Type"=>"",
 "Cache-Control"=>"no-cache"}
Tom Z Meinlschmidt (Guest)
on 2009-05-12 20:04
(Received via mailing list)
because of security ...

you need to specify 'removal' from token protection in your controller,
eg
class FormController << ApplicationController
   protect_from_forgery :only => [:create, :update, :destroy]

   def index
   ...
   end
end


Learning wrote:
> end
> RAILS_ROOT: /home/Learning/workspace/myapp
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> core_ext/benchmark.rb:17:in `ms'
> base.rb:523:in `send'
>
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
> base.rb:523:in `process_without_filters'
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> query_cache.rb:9:in `cache'
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> vendor/rack-1.0/rack/lock.rb:11:in `synchronize'
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> /usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
> `gem_original_require'
> /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/
> benchmarking.rb:68:in `perform_action_without_rescue'
> flash.rb:141:in `perform_action'
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> query_cache.rb:29:in `call'
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> failsafe.rb:11:in `call'
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> vendor/rack-1.0/rack/handler/webrick.rb:46:in `service'
> /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/
> Request
>
> Headers:
>
> {"Content-Type"=>"",
>  "Cache-Control"=>"no-cache"}
>

--
===============================================================================
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
  - experienced RoR/PHP freelancer, available for hire

   www.meinlschmidt.com  www.maxwellrender.cz  www.lightgems.cz
===============================================================================
Yongning L. (Guest)
on 2009-05-13 09:12
(Received via mailing list)
Does RoR has an easy way to tell different about GET and POST params
just
like $_GET and $_POST in PHP?
Matthew MacLeod (Guest)
on 2009-05-13 13:06
(Received via mailing list)
On May 13, 6:12 am, Yongning L. <removed_email_address@domain.invalid> wrote:
> Does RoR has an easy way to tell different about GET and POST params just
> like $_GET and $_POST in PHP?

You can use the request object:

case request.method
when :get
  #Do stuff for GET method
  return
when :post
  #Do stuff for POST method
  return
end
This topic is locked and can not be replied to.