Get data from form parameters

I am new to rails and ruby. I am trying to make a simple project and have this problem. I have a view with some text fields on it, when I click the "Submit" button, in my controller I need the values ​​from these fields as strings, I try to use this path params[:field1] , but the value is in this format { "field1" => "some_value"}, this is not a string, and I have problems with it. How can I solve it?

UP: view code

 <%= form_tag :action=>:login_user do %> <div class="field"> <h2>Login</h2> <%= text_field "field1", "field1" %> </div> <div class="field"> <h2>Password</h2> <%= password_field "field2", "field2" %> </div> <div class="actions"> <%= submit_tag "Login" %> </div> <% end %> 
+4
source share
3 answers

Try using it like this:

 <%= text_field_tag "field1" %> <%= password_field_tag "field2" %> 
+5
source
 params[:field1] 

is the right way.

Your parameters are a hash:

 params => {"field1"=>"some_value"} 

therefore, to get field1 , you must call params[:field1]

UPD

For your structure (i.e. actaully bad) you should call the parameters like this:

 params[:field1][:field1] params[:field2][:field2] 

better to use text_field_tag and password_field_tag in your case:

 <%= text_field_tag :field1 %> <%= password_field_tag :field2 %> 
+5
source

Using the code you inserted, you will need to access it as params[:field1][:field1] and params[:field2][:field2] . Since Ashish suggested you use text_field_tag. Or in the usual Rails way, use form_for to associate both fields with the same key and use update_attributes or create .

+1
source

Source: https://habr.com/ru/post/1343154/


All Articles