3 Replies - 17499 Views - Last Post: 29 March 2010 - 02:59 AM Rate Topic: -----

#1 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Ruby on Rails - POST method

Posted 19 July 2009 - 09:18 AM

Hi! I'm reading WROX "Beginning Ruby on Rails" and I have a little problem with an example ... I'm supossed to send form to controller. Everything works fine with GET method, but there's a problem with POST. When I send form to action, there's an error: ActionController::InvalidAuthenticityToken in LookController#at . I'm new to RoR and I don't get any clue what's wrong :/. I'm using RoR version 2.1.0-6.

Here's code, nothing much:
controller:
class LookController < ApplicationController
	def at
		@data = params[:text1]
	end
end


and view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ruby On Rails</title>
</head>
<body xml:lang="pl">
<div>
Hello <%= @data %>!
</div>
</body>
</html>


and at last, form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ruby On Rails</title>
</head>
<body xml:lang="pl">
<div>
<form action="/look/at" method="post">
<div>
Input your name:<br />
<input type="text" name="text1" /><br />
<input type="submit" value="Send!" /><br />
</div>
</form>
</div>
</body>
</html>



Thanks in advance for any help ;)

This post has been edited by k0b13r: 19 July 2009 - 09:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Ruby on Rails - POST method

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Ruby on Rails - POST method

Posted 19 July 2009 - 09:25 AM

One solution I found was to make your code look like this

class LookController < ApplicationController
	def at
		@data = params[:text1]
	end
	skip_before_filter :verify_authenticity_token
end



Another solution is to add this line somewhere inside your form

<input name="authenticity_token" type="hidden" value="84fc5f10d45977c87c3ac6b88aabc0e73925cad0" />


Was This Post Helpful? 1
  • +
  • -

#3 k0b13r  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 243
  • Joined: 18-July 06

Re: Ruby on Rails - POST method

Posted 19 July 2009 - 09:34 AM

Oh, that was fast. Than's a lot :) ! Anyway, first solution works fine, second isn't working, I still get the same error page.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Gareth*


Reputation:

Re: Ruby on Rails - POST method

Posted 29 March 2010 - 02:59 AM

The second approach doesn't work because the authenticity token is linked to your session ID. To get the correct authenticity token generated and included in your web page, use

<% form_tag "/look/at" do %>
...
<% end %>

instead of

<form ...>
...
</form>
Was This Post Helpful? 0

Page 1 of 1