I have view Event#show. At this view user may choose how many people will join the event and choose the term. Then he may click "Purchase" which will redirect him to Transaction#new. Here I'd like to pre-fill these two values.
What is best way to do that? Where should I store these variables?
I'm thinking about:
<%= link_to new_transaction_path({ ..... })
...but I have no idea how to pass values to link_to parameters. Here's how i let user choose count of people:
<%= f.select(:seleced_seats, #event.seats, {}, { :class => 'form-control' }) %>
I have view Event#show. At this view user may choose how many people
will join the event and choose the term. Then he may click "Purchase"
which will redirect him to Transaction#new.
So you can make a form from from where user can choose how many people can join and terms:
<%= form_tag("/transaction", method: "get") do %>
<%= label_tag(:people, "How Many People:") %>
<%= text_field_tag(:people) %>
<%= options_for_select([['terms 1', 1], ['terms 2', 2], ...]) %>
<%= submit_tag("Purchase") %>
<% end %>
So you are getting a get or post request like /transaction?people=3
Now from params[:people] you can access information of your next form like the following:
<%= form_tag("/transaction", method: "post") do %>
<%= label_tag(:people, "How Many People:") %>
<%= text_field_tag :people, params[:people], disabled: true %>
...
<%= submit_tag("Submit") %>
<% end %>
Related
How would I go about creating a form that takes what user input as a value and just passes it to the controller without being connected to any model?
Something simple like i.e. calculating tax based on input salary, or other calculation like that, when I show the user a form, let them fill it, and when submitting it would go to the results
<%= form_with url: 'calculator#result' do |form| %>
<%= form.number_field :value, in: 1000.0..20000.0, step: 0.5 %>
<%= form.submit %>
<% end %>
i expected something like this to pass 'value' and redirect to calculator#result when submitting, but the button doesn't really do anything. whereas a form connected to a model seems pretty smart and does it
The form_tag Helper method is usually used for forms that are not linked to a model.
I think this should work:
<%= form_tag("/calculator/result", :method => "get") do %>
<%= label_tag(:value, "Value:") %>
<%= text_field_tag(:value) %>
<%= submit_tag("Submit") %>
<% end %>
I want to display a list of InvestorTypes (as a radio button) but before each type I should be able to add an explanation of that type. Here is what I've got:
<%= simple_form_for(resource, as: resource_name, url: users_user_experience_level_path(resource_name), html: { method: :put }) do |f| %>
<div class="form-inputs">
<% User::USER_EXPERIENCE_LEVEL.each do |level| %>
<b>Investor type <%= level %></b>
<%= t("user.description.#{level}") %>
<%= f.input :experience_level, collection: [level], as: :radio_buttons, label: false %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit, 'Submit' %>
</div>
<% end %>
Which gives me expected view:
Investor type Beginner
Some explanation of what is going on
[checkobox] type Beginner
Investor type Expert
Some clarification of who is an expert and what ever you want to display here
[checkbox] type Expert
Investor type Institutional
Some clarification of who is an institutional client and some legal stuff
[checkbox] type Institutional
But when Submit button is pressed it doesn't pass input value (radio box selection which user chose) into the params:
=> #<ActionController::Parameters {"experience_level"=>""} permitted: true>
[EDIT]
class User < ApplicationRecord
USER_EXPERIENCE_LEVEL = %w[institutional beginner expert].freeze
end
It looks to me like you're using simple form wrong. The "collection" input in Simple Form is expecting to get an entire list of options, not just one option.
Looping in the way you're doing it is creating one group for each experience level, and each group only has one button in it. So it might visually look correct, but it's not functioning the way you intended. Instead you want to create one group of radio buttons for experience level such that each button changes the value of experience level.
Because you're doing this with significant customization around the appearance, it's probably not a good use of Simple Form, and instead you should fall back to the normal Rails form helpers.
You want to pass a block to f.input to get the simple form wrapper and then use the lower level rails helpers:
<%= simple_form_for(resource, as: resource_name, url: users_user_experience_level_path(resource_name), html: { method: :put }) do |f| %>
<div class="form-inputs">
# adds the simple form wrapper
<%= f.input :experience_level do %>
# iterates across the options and yields a input builder to each iteration
<%= f.collection_checkboxes(:experience_level, User::USER_EXPERIENCE_LEVEL, :value_method, :label_method) do |cb| %>
# There are three special methods available:
# object, text and value,
# which are the current item being rendered, its text and value methods, respectively.
<%= t("user.description.#{cb.text}") %>
<%= cb.check_box %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit, 'Submit' %>
</div>
<% end %>
If you don't actually have a model you can use #itself to iterate across a flat array:
<%= f.collection_checkboxes(:experience_level, User::USER_EXPERIENCE_LEVEL, :itself, :itself) do |cb| %>
Or an array of pairs:
<%= f.collection_checkboxes(:experience_level, User::USER_EXPERIENCE_LEVEL.zip(User::USER_EXPERIENCE_LEVEL), :first, :last) do |cb| %>
Or even a struct.
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.
I have a selection box on my page, and when I click the submit button I want to take the selection choice to the server as either a post or get variable (I don't think it matters). How do I link this form:
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
to this button:
<%= button_tag(link_to("Get Rates", store_rates_path))%>
You only need to provide the path to the form_for method, to link it to the rates action of your stores controller:
<%= form_tag(store_rates_path, method: "get") do %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select((1980..2014).to_a)) %>
<%= button_tag "Get Rates" %>
<% end %>
In your rates action you can then retrieve the :year parameter passed as follows:
def rates
#year = params[:year]
end
You also need to define the route in your routes.rb file as follows, if you haven't yet:
get 'stores/rate', to: 'stores#rate', as: 'store_rates'
IMPORTANT
Just note that if the rates belong to a specific store, meaning the url is something like stores/1/rate then the above get must be stores/:id/rate, which also means you need to pass the store.id to the store_rates_path in your form: store_rates_path(#store)
You can use rails submit_tag helper
<%= form_tag(store_rates_path, method: 'get') %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= submit_tag "Get Rates" %>
<% end %>
OR
If you want to use a link or button to submit your form parameters then you can use some js magic to achieve it:
<%= form_tag store_rates_path, id: "store-form", method: 'get' %>
<%= label_tag(:year, "From (year)") %>
<%= select_tag(:year, options_for_select(get_select_options(1980, 2014))) %>
<%= link_to "Get Rates", "#", id: "store-form-btn" %>
<% end %>
$(document).on("click","#store-form-btn",function(e){
e.preventDefault();
$("#store-form").submit();
});
This is what I'm hoping to do. Currently I have a drop down list of characteristics from which user can choose and search does certain element holds that characteristic. He picks a characteristic from drop down menu and clicks search button. Now I am trying to make a list of links for those characteristics so user can immediately click on certain characteristic.
I will have a couple of links separated with |:
characteristic1_link | characteristic2_link | characteristic3_link
Currently I have the following for drop down search which works:
<%= form_for(#element, method: 'get', url: 'query') do |f| %>
<%= f.collection_select :characteristic_id, Characteristic.all, :id, :name, :include_blank => true %>
<%= f.submit "Search" %>
<% end %>
I am trying to do that with links which I generate like this:
<% #characteristics.each do |characteristic| %>
<%= link_to (characteristic.name), '#' %>
<% end %>
How can I pass :characteristic_id parameter with link and somehow make f.submit to trigger when user clicks on link?
EDIT:
This seems to be working:
<% #characteristics.each do |characteristic| %>
<%= link_to (characteristic.name), query_path(:element => {:characteristic_id => characteristic.id}) %>
<% end %>
Opinions about this method? :)
if i understand your question like that clicking and link_to-anchor selects the option with the same name as the anchors label and submits the form then you might want to...
<%= link_to characteristic.name, '#', class: 'trigger_select' %>
and let your JS do the rest. (untested.)
$('.trigger_select').click( function() {
var href = $(this).attr('href');
$('#select_field').val(href);
$('#form').submit();
});
hope i understood you correcly.