1

i am trying to access the data from a form.

<% form_tag(:action => "test") do %>
<p>
    Name:
    <%= text_field_tag("name","web programmer") %>
</p>
<%= submit_tag ("Save data") %>
<% end %>

this form is in views/challenges/show.html I have written this piece of code in challenges_controller

def test
    @name = params[:name]

    end
before_filter :test

now,If i try to access @name variable in views/challenges/show.html heres the code for that part

<% if @name != null %>
<%= @name  %>
<% end  %>

I get the following error "undefined local variable or method `null' "

i have addded the following route to my routes.rb

get 'challenges/test'

Can someone help me out with this issue?

pokiri
  • 87
  • 1
  • 3
  • 11

1 Answers1

0

You need to display your form so instead of using <% %> you need to use <%= %>, checkout this answer for differences between them. By default forms use POST verb and you have defined a route for GET request. Change your form to this:

<%= form_tag({action: :test}, {method: :get}) do %>
  <p>
    Name:
    <%= text_field_tag("name","web programmer") %>
  </p>
  <%= submit_tag ("Save data") %>
<% end %>
Community
  • 1
  • 1
Mandeep
  • 9,093
  • 2
  • 26
  • 36
  • Thanks I changes the form and replaced "null" with 'nil' but now i get the following error "No route matches [POST] "/challenges/test"" – pokiri Jun 23 '15 at 11:22
  • Thanks i tried the updated form and i still get the same error "No route matches [POST] "/challenges/test" The url just changed to "http://localhost:3000/challenges/test?method=get" I am able to display the form by the way – pokiri Jun 23 '15 at 11:38
  • sorry but the same issue continues I just keep on getting the same error "No route matches [POST] "/challenges/test", though i have changed the method to "get" @mandeep – pokiri Jun 23 '15 at 11:47
  • @pokiri try updated answer. I have tested it locally so it will work :) – Mandeep Jun 23 '15 at 11:55
  • thanks i get some error as"Missing template challenges/test, application/test" the error is highlighted at this line:: "find_all(*args).first || raise(MissingTemplate.new(self, *args))" I am using activeAdmin,kaminari gems in my application does this create any issue??? sorry for troubling you @mandeep – pokiri Jun 23 '15 at 12:05
  • @pokiri you don't have template for test action. Either rename views/challenges/show.html.erb to views/challenges/test.html.erb or create a new one or inside test action you can render template of show action by render 'show' – Mandeep Jun 23 '15 at 12:07
  • Thanks a lot it worked i just changed my 'action' attribute in form to 'show' and placed @name = params[:name] in my 'show' method – pokiri Jun 23 '15 at 12:25