I want to pass one variable which is introduced by the user in the front-end. I dont use any model (i dont need it, because im working with JSON data all in memory). I have looked many tutoriasl but almost all of them are focused on filling out a form. My application does not have any form nor tables. Any idea?
Thank you.
{<div class="module1">
<p>Mein Lastprofil berechnen</p>
<div class="boxed">
Jahreshausverbrauch (kWh)
<%= text_field_tag "input", nil, placeholder: "3500" %>
<%= button_to "Senden", root_path, :method => :get %>
</div>
</div>}
I want to save/pass the variable introduced when clickling the button.
By having a text field you implicitly already have a form.
Make it explicit by wrapping the text field and button:
<%= form_tag root_path, method: :get do %>
<%= text_field_tag :input, placeholder: "3500" %>
<%= submit_tag "Senden" %>
<% end %>
Then you can access the value as params[:input] in the controller.
Related
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.
I'm very new to ruby on rails. I'm trying to make a text field to assign one of my variables (end_date), but I keep getting this error:
undefined method `end_date' for #<Quiz:0x007fccd1e0f9c0>
Here's my code:
<%# Main Canvas where cardes places %>
<div class="column large-11" id="main">
<%= form_for #quiz do |q| %>
<%= q.label :quiz_name %>
<%= q.text_field :quiz_name %>
<%= q.label :end_date %>
<%= q.text_field :end_date %>
<%= hidden_field_tag 'selected', 'none' %>
<%= q.hidden_field :classroom_id, value: #classroom_id%>
<%= q.submit "Create Quiz", class: "expanded button" %>
<% end %>
<%= form_tag("/quiz/#{#classroom_id}/copy", method: "get") do %>
<%= label :id, "ID" %>
<%= text_field_tag "id", "" %>
<%= submit_tag "Copy Quiz By ID", class: "expanded button" %>
<% end %>
</div>
Let me break down how these different pieces relate to one-another, which hopefully will make this easier for you to troubleshoot.
<%= form_for #quiz do |q| %>
Here you are invoking form_for to create a form bound to the #quiz object. It yields a form builder object as the argument q.
<%= q.text_field :quiz_name %>
Here you are calling the text_field method on the form builder with the field named quiz_name. This means it will generate a text input, and call the quiz_name method on #quiz to find the current value.
So given that background, it should be clear why you are seeing this error:
<%= q.text_field :end_date %>
You are telling the form builder to call #quiz.end_date for the value of this field, but that method does not exist.
You have not given enough code samples for us to determine why you expect this method to exist. Perhaps this is a field you've added to the quizzes table, but haven't yet run the migration? Is this supposed to be a virtual attribute on Quiz? Or perhaps you just want to send a field that isn't connected to the Quiz model inside this form. (You can do that with a separate set of helpers, in this case text_field_tag, that give you more flexibility in where the data comes from).
I have a Rails 3.2.12 app where I would like to pass a parameter via a form submit button.
The param is :invoice_id. In the form the value is in #invoice.
I tried this:
<%= f.submit "Submit", :class => "btn btn-success", params: {:invoice_id => #invoice} %>
In the controller, I would access it like this:
def addinvtimes
#invoice = params[:invoice_id]
But, it ends up being nil.
Thanks for the help!
UPDATE1
That's not how HTML forms work. If there's data that you want to get submitted along with the rest of your form's data but not be viewable or editable by the user, stuff it into a hidden field, like so:
<%= form_for #order do |f| %>
<%= f.text_field :customer_name %>
<%= f.hidden_field :invoice_id, value: #invoice.id %>
<% end %>
When you do this, the invoice_id will be submitted alongside the rest of the form's data, so in this case you would access it as params[:order][:invoice_id].
My View:
<div class="field">
<%= label :isbn_exist, 'ISBN' %>
<%= text_field :isbn_exist,'ISBN' %>
</div>
<%= link_to 'Add new book', new_book_path(:isbn_exist => #isbn_exist), :class => "btn btn-warning"%>
This ":isbn_exist" variable is not sent to the controller. why? If i put a number instead of #isbn_exist this value is sent. I think the text_field is not saving the value on the :isbn_exist variable...
You have a text_field and a link. What you really want is a form (including this text_field) and a submit button.
Things like this are covered in every rails tutorial you can find.
How I can submit form with link on correct rails 3 format?
Thanks.
<%= form_for #post do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
My code sample.
For people who came here via Google, I have an improvement on Zequez's answer. Instead of the method that he gives, add this method to the application helper instead:
def link_to_submit(*args, &block)
link_to_function (block_given? ? capture(&block) : args[0]), "$(this).closest('form').submit()", args.extract_options!
end
Then, as Zequez stated, for simple links you can just do this in your view:
<%= link_to_submit 'Submit Form' %>
...and for more complicated buttons you can pass HTML options and a block to be used inside the link. If you use Twitter Bootstrap, for example, this lets you add CSS classes, formatting and icons:
<%= link_to_submit( class: 'btn btn-primary' ) do %>
<strong>Submit</strong> the Form <i class="icon-arrow-right"></i>
<% end %>
The JQuery code will work as long as the link is a child of the form (that is, as long as link_to_submit is called from somewhere within the form_for block).
"Correct" is a tricky word in this context ;) . One could ask why you're not just taking a button element and make it look like a link?
Anyways — you can't achieve this with plain HTML (at least not to my knowledge). With a Javascript framework like e.g. jQuery you could simply do something like this:
$('a').click(function(){
$('form').submit();
return false;
});
Rails 2.3.x had a link_to_remote helper which let's you specify a :submit parameter (= DOM element's ID, default is the parent form). So you were be able to write:
link_to_remote 'submit', :url => {…}, :submit => "my_form"
But with Rails 3's push to UJS, this helper is gone.
You can add the following to the application helper:
def link_to_submit(text)
link_to_function text, "$(this).closest('form').submit()"
end
Then inside your view files you can just call
link_to_submit 'Submit Form'
And the link must be child of the form.
With jquery, this one-liner will work fine for a simple form.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"$('form').submit()" %>
Of course you don't really have to use jquery, just finding the dom element for your form will work fine as well.
<%= link_to t("translate.submit"), "#", class: "make it beautiful", :onclick=>"document.getElementById('your_form_id').submit()" %>
This way you don't use any ajax, just plain form submit.
In Rails 3, the link_to_remote helper is gone, but it's replaced with
link_to 'submit me', url_for(#post), {:remote => true, :class => 'submit_me'}
In your case, you likely want your form to do the AJAX, like so:
<%= form_for #post, :remote => true do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %>
<p><%= f.submit %></p>
<% end %>
With a companion link:
link_to 'submit me', '#', :class => 'submit_me'
Then, in an .js file included in the page body:
$('.submit_me').click(function() {
$('form').submit();
return false;
});
The idea is that anything more complicated than turning a link or form into an ajax request should be done with the jQuery callbacks, as listed here:
https://github.com/rails/jquery-ujs/wiki/ajax
And if you want to really get into interactive AJAX requests, go here for a great 2-part article on it.