Creating a variable via the form rails - ruby-on-rails

Value the variables written in variables #first_city, #last_city, #date_trip of the controller. Without creating an object in the database. Then pass the values of these variables to another page. It is possible to do that or not?
<%= form_for(#orders) do |f| %>
<p>Выберите маршрут</p>
<%= f.select(:first_city, #city_select, :value => :first_city, prompt: "Откуда") %>
<%= f.select(:last_city, #city_select, prompt: "Куда") %>
<%= f.text_field :date_trip %><br>
<%= f.submit "Дальше", class: "btn" %>
<% end %>

Yes.
However if you state
form_for(#orders)
it will automatically assume you are creating an Order.
However you can do
form_for #order, :url => {:action => "action"}
create your own action and access those values trough
order_params[]
And you can do whatever you want with that data
Hope i helped

Related

How do I retrieve the values of a link in Ruby on Rails

In PHP, to retrieve the value of a link in PHP, all I have to do is use $_GET['value']
Now in Ruby on Rails, how do I do that? As an example, let say I have this link I want to retrieve its id and use it in a form.
This is the link
<%= link_to "Message", new_message_path %># This link will allow the viewer to message the profile owner
and this is the message script
<%= form_for(#message) do |f| %>
<%= render 'shared/error_messages', object: #message %>
<%= f.label :content %>
<%= f.hidden_field :receiver, value:# The ID should be retrieved from the user id of the previous page %>
<%= f.text_area :content, size:"20x15" %>
<%= f.submit "Send message", class: "btn btn-primary" %>
<% end %>
I suspect you might be talking about query params (such as receiver_id on /new?receiver_id=1234). You can do this on Rails as well by passing additional parameters to the helper function used to route, such as:
<%= link_to "Message", new_message_path(receiver_id: #receiver.id) %>
Which would yield something like /messages/new?receiver_id=1 or whatever.
Then you can use the params variable in your controllers in order to access the query params, such as params[:receiver_id], which would yield 1 in this case.

Rails - How to have a form save the data from multiple inputs as a hash

I'm building a page that contains a html table, where the user can choose the name in the table header and the content in the table row.
The user will enter the information to a form, which will be parsed into a hash and saved into the DB.
Here is an example where :key will be a key of the hash (table header text) and :value will be a value in the hash (table column content).
<%= form_for([#page]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :key1 %>
<%= f.text_field :value1 %>
<%= f.text_field :key2 %>
<%= f.text_field :value2 %>
<%= f.text_field :key3 %>
<%= f.text_field :value3 %>
<%= f.text_field :key4 %>
<%= f.text_field :value4 %>
<%= f.submit "Save" %>
<% end %>
I could make a new DB table with a has_many association to the page DB and have the info stored in that table. Then that data can be rebuilt into a hash when I need to render the view.
But if I could save the entire hash into a column in the page DB, then it would save me from having to rebuild the hash every time someone visits the page.
I'm struggling to come up with how to do this in form_for.
The first issue is that it requires key and value columns in the page table. A simple fix would be change all inputs into <%= f.text_field :html_table %> (for html_table column in the page table). But then it won't be able to distinguish between a key and a value in the hash. And I also don't have any way of telling it to put the info together into a hash that is then stored into the page table's html_table column.
Not sure if this is even a possible thing but it doesn't hurt to ask before I go ahead with building the has_many association table and setting it up that way.
I'm rather confused about your question.
Can't you build a hash like this in your controller action?
html_table = {
params[:key1] => params[:value1],
params[:key2] => params[:value2],
params[:key3] => params[:value3],
params[:key4] => params[:value4]
}
Then save it to #page.html_table attribute.
EDIT:
In this case, the form is not about attirbutes of a particular object. So instead of using form_for (Link), you should be using form_tag (Link).
Example:
<%= form_tag "/pages_controller/build_html_table" do %>
<%= text_field_tag "key1" %>
<%= text_field_tag "value1" %>
<%= submit_tag "Save" %>
<% end %>

How to send additional variable with rails form?

I am working with a rails form which takes users text input and sends it to the controller.
It sends two infos, the text and the language of the text (I18n.locale variable). My form looks something like that:
<%= form_for(:text, :url => {:action => 'create'} ) do |f| %>
<%= f.label :content, "#{t :"Write whatever you want"}" %><br />
<%= f.text_area :content, :cols => 80, :rows => 3 %> <br />
<%= f.hidden_field :locale, :value => I18n.locale %>
<%= f.submit "#{t :Post}"%>
<% end %>
I am sending that locale value using a hidden field. But I think this is a bad practice. User can easily modify this form. So is there any way to send that locale value among other form data automatically without any visible/hidden field?
If you want to avoid modifing data by user maybe you can use session like locale_session = "en", and use that session when you are dealing with data in controller
There are two ways of adding variable with rails form
<%= f.hidden_field :locale, :value => I18n.locale %>
AND
<%= form_for(:text, :url => {:action => 'create', :locale => I18n.locale} ) do |f| %>
But both of the above can easily modified by the user.
To avoid this either you have to use sessions or ssl page
Temporary solved this adding another line into controller. Not sure it it safe or not
def create
#text = Microblog.new(params[:text])
#text.update_attributes(params[#text.locale = "#{I18n.locale}" ])

Rails : How to use an object along with a text_area_tag of form_tag?

<%= text_area_tag :body_html, "", :class =>"required mceEditor", :id=>"txaEditableContent" %>
This is my code. I want this text_area_tag to be assigned to an object(object is note in my case) just like we use "form_for object" so that the :body_html goes straight into params[:note] on submit. How can I do this?
You have got to set first an instance of Note in the controller corresponding action:
#note = Note.new
Then, in your view put this form:
<%= form_for #note do |f| %>
<%= f.text_area :body_html, :class => "required mceEditor", :id => "txaEditableContent" %>
<%= f.submit "Submit" %>
<% end %>
Now when you click the submit button you should get the right post request as you needed.

What's the Rails Way to pass data to controller method?

After getting some data from an API, I filtered the results and showed them on a view to et the user select which data he wants to save to the database. The user doesn't have to fill anything once all the data came from the API.
My question is: what's the best way/rails way to pass all this data to the controller?
Right now I'm using a form with hidden fields. It works but I was wondering if that's the best way.
Here's how my 'form_for' looks like:
<%= form_for (#new_photoset) do |f| %>
<%= f.hidden_field :id, :value => not_imported_set.id %>
<%= f.hidden_field :name, :value => not_imported_set.name %>
<%= f.hidden_field :thumb_url, :value => not_imported_set.url) %>
<%= f.submit "Import" %>
<% end %>
I believe you're on the right track, this is way you would normally do this in rails.

Resources