1

I have a controller named Welcome with view called index.

In my index view i have created a small form as such.

<%= form_for :location do |f| %>

    <%= f.label :Longitude %><br>

    <%= f.text_field :integer %>
    <br>
    <br>
    <%= f.label :Latitude %><br>
    <%= f.text_field :integer %>


  <p>
    <%= f.submit %>
  </p>
<% end %>

In this form the user can enter some integer value for longitude and latitude. Once the user enters value for longitude and latitude. They click submit. Upon submit i would like to store these values in my controller. So i am using the following method where i have two instance variables taking values from the form.

def index
    @long = params[:longitude]
    @lat = params[:latitude]
  end

In my routes.rb I have

get 'welcome/index'
  post 'welcome/index'

Please tell me where i went wrong. Also if someone can suggest a better way of doing this also i would appreciate it i am new to rails and i want to learn the correct way of doing things so i don't create bad habits early on.

2 Answers 2

2

The reason it's not working is because your fields are both named :integer, and since they share the same name, the browser will only send one value.

So, with your code, if you filled in the first field with 'a' and the second with 'b', your params would contain something like this:

{ location: { integer: "aaa" } }

Which obviously isn't what you want! If your HTML looked more like this (I've stripped the layout stuff to make things clearer):

<%= form_for :location do |f| %>
  <%= f.label :longitude %>
  <%= f.text_field :longitude %>

  <%= f.label :latitude %>
  <%= f.text_field :latitude %>

  <%= f.submit %>
<% end %>

Then you could access the params in your controller params[:location][:longitude] and params[:location][:latitude]

A good idea to see the difference between the effect of your form vs this form would be to inspect the html. Take a look at the input name attributes, and label for attributes and see how they match up with the params Rails receives. Also, when you post the form, be sure to look in your server log to see the params! :)

Sign up to request clarification or add additional context in comments.

6 Comments

Ah you are right i made a mistake there okay that makes a lot of sense but after making suggested changes i went to my controller and did this @long = params[:location][:longitude] , I got an error saying undefined method `[]' for nil:NilClass
@Learner check your html to see what the field names are, and then look in your rails server log - it will list the parameters of the post request.
it seems to indicate that " NoMethodError (undefined method []' for nil:NilClass): app/controllers/welcome_controller.rb:4:in index'"
which means something is undefined?
can you please look at my new question
|
2

After reading your question, I think you want to see how controllers, views and models work. For learning purpose you can generate scaffold and study the generated code.

For example, generate a model GeoLocation, related controller and views by this:

rails g scaffold GeoLocation longitude:string latitude:string

Now fire up rails server and browse http://localhost:3000/geo_locations/new and save your long, lat. I wrote this answer to give you some guidance.

You can follow these excellent books:

  • The book of Ruby
  • The Rails 4 Way

1 Comment

Hi, Thanks for your comment, I have studied how to generate models and create associations between tables and also other topics such as naming scopes. So that part is clear to me. I am getting very confused when it comes to dealing with view and controller as in passing information between the two. I will try looking at the scaffold as you suggested now. i would also appreciate if you can provide input for the above code i posted. Thanks so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.