HTML / ERB - Standard-Button? [duplicate] - html

This question already has answers here:
How to make enter the submit button in a form
(3 answers)
Closed 7 months ago.
In the picture you can see 2Buttons: screenshot
I want the user to just type in the email and password and accept it by pressing return.
Is there an opportunity to set this up?
I know that there is a option in C#, but I forgot how it's called. (Standard-Button?)
There are 2 input-texts and 2buttons (Login-Form / Register)
eMail - Password - Login-Btn and or-Register-btn.
The user types in his email and password. After that he press "return" to accept and he is logged in.
How is the options called that the login-btn is the ... first button to choose by pressing "return"?***
That's what I am using:
Ruby on Rails 4
HTML
HTML.ERB
Bootstrap 3

You may have an answer, but to help you understand it, you need to remember Rails renders everything as HTML on the browser-side; so every element you want to display has to be some sort of HTML object
If you want to show a button in Rails, there are a number of ways to do it:
button_to
This creates a simple form which points to a URL. The form has a button element inside, making it look like a pure HTML button to the user:
<%= button_to "New", action: "new" %>
# => "<form method="post" action="/controller/new" class="button_to">
# <div><input value="New" type="submit" /></div>
# </form>"
f.submit
For your specific issue, I think you'll benefit from f.submit, which basically adds a submit button to your form:
<%= form_for #var do |f| %>
<%= f.submit "Register" %>
<% end %>
If you'd like to style this, you'll be able to apply classes to the button directly:
<%= f.submit "Text", class: "class" %>

Related

How can I send values from an erb file to a ruby controller?

I'm trying to create a dynamic quiz system as follows:
A controller passes a value to an erb file (which creates an html
form with the first quiz question)
The erb file has an html form where the user can enter an input
The erb file sends the input back to an action in the controller
The action checks the input value and loads the next question. The next
question depends on what the user's input was.
Now I know that I can send a value from an action to its corresponding erb file. Basic stuff. But how can I send a value from the erb file back to the controller (so that it can process the input from the html form)?
For example, this is what the html template (question1.html.erb) for the first question looks like:
<head ><title >Question 1</title ></head >
<body>
<form action="localhost:3000/welcome/processQ1" method = "get">
<p>What is the sum of 5 + 6 ?</p>
<input name='sum' type='text' width='5' id='sum' required />
<input type='submit' value='Submit' />
</form >
</body>
What I want is to send the value of 'sum' back to an action called "processQ1" in the controller. How can this be achieved?
First of all, try always use the Rails way instead html pure tags for create your forms:
<%= form_for :quiz do |f| %>
<label>What is the sum of 5 + 6 ?</label>
<%= f.text_field :answer %>
<%= f.submit "Submit" %>
<% end %>
After submit, in your controller you will get the value of answer in your ProcessQ1 (This is not a good name for a controller because they are written in plural and more generically. So, for your purpose, QuizzesController is an option) in params rails hash variable on your action create.
ex: { quiz: {answer: 5 }} and make anything you want with this data.
Please, read this docs. It is a good way to understand this processes of framework.
http://guides.rubyonrails.org/form_helpers.html
http://guides.rubyonrails.org/getting_started.html
http://guides.rubyonrails.org/getting_started.html#saving-data-in-the-controller

Capybara Choose radio button not working

I've done this maybe 100 times but for the life of me I can't understand why it's telling me it can't find this object. Here is my html:
PS I've verified that I'm on the correct page and I can see the advisor when I do a save_and_open_screenshot
HTML
<div class="panel">
<%= form.label :advisor, class: "panel__label" %>
<%= form.radio_button :advisor_or_client, "advisor", class: "panel__input__radio advisor" %>
</div>
<div class="panel">
<%= form.label :client, class: "panel__label" %>
<%= form.radio_button :advisor_or_client, "advisor", class: "panel__input__radio client" %>
</div>
TEST
choose('advisor')
ERROR
Failure/Error: choose('advisor')
Capybara::ElementNotFound:
Unable to find radio button "advisor"
Screenshot
Based on the info provided how can I select the radio button for my feature spec?
choose locates the radio button by name, id or label text - http://www.rubydoc.info/gems/capybara/Capybara/Node/Actions#choose-instance_method . Rather than that you are specifying the value of the input (I'm assuming the second value should be "client" rather than the "advisor" you have). Since you don't have ids, and name would be ambiguous, you can use the option parameter (matches value) to narrow down to the correct radio. Therefore
choose('trade_request_submission[advisor_or_client]', option: 'advisor')
or potentially
choose(option: 'advisor') # if you don't have any other radios with that value
Another option is to specify the label text but that would require adding the ids to the input elements that match the for attributes on the labels to link them correctly, in which case you could do
choose('Advisor')

Rails checkbox create a submit action

I have a rails form with checkboxes. What I am trying to do is when the user clicks on a check box the app deletes that entry from the database. What I cannot figure out is how to have the app react when the user checks on the check box (similar action to having a submit button clicked). What should I include in my html so when the user click on a check box the app does something?
Here is my code
<% #items.each do |item|%>
<%if item.email == #user.email%>
<%= form_for #item do |f| %>
<%= f.check_box :to_do %>
<%= f.label item.to_do %><br />
<% end %>
<%end%>
<%end%>
I am iterating through items and if there is a match between the item.email and user email print out the item with a check box next to it. I can include more code if needed. Is there an Ajax call I can make?
As RedBassett said, I'm not 100% sure I agree with the strategy of having a checkbox instantly delete an entry. However, if that form you have is already set up to delete things, you can do it like this:
You don't even need an AJAX call. Just regular Jquery. Add a class to the form, as well as to the checkbox
<%= form_for #item do |f|, :class => "form-class" %>
<%= f.check_box :to_do, :class => "checkbox-class" %>
and in your JS file, use Jquery to tell the form to submit when the user clicks the checkbox.
$(".checkbox-class").on("click", function (){
$(".form-class").submit();
});
Note: this will actually submit the form, it will be as if you checked the box and then pressed a 'submit' button, so you will need to handle the form submission in the controller, if you want to stay on the same page, you will need to redirect back to the same page as this action will cause a reload/redirect
Yes. You can write an AJAX call.
<script>
$('#checkbox_id').change(function(){
$.get('controller/action?item_id='+$(this).val(), function(data, status){
/* Call back */
})
})
</script>
Use borbesaur answer, but remember you can just add the remote: true option to your form so Rails can submit the form using ajax and you avoid reloading all the page again. :)

Ruby on Rails pass button value to next page

I am trying to design a webpage that contains both a radio button field and a list of links. When one of the links is clicked, I would like to pass the associated value of the button to a controller. So far I have the following code:
In my views page:
<div id="radio_buttons">
<%= radio_button_tag 'radioGroup', "1" %> 1
<%= radio_button_tag 'radioGroup', "2" %> 2
<%= radio_button_tag 'radioGroup', "4" %> 4
</div>
...
<div class = "text">
<%= link_to "mylink", search_path(:radioGroup => #radioGroup)%>
</div>
In my Controller:
#radioGroup = params[:radioGroup]
When I add the line 'puts #radioGroup' to my controller, it prints nothing. I have been able to pass different values to a new web page upon a link click (i.e those contained in a text field) but for some reason I can't figure out how to do the same thing with buttons. Does anyone know how to accomplish this?
Thanks!
If you want to submit multiple values to another page you should use an HTML form for it. Check this page for more related info: http://guides.rubyonrails.org/form_helpers.html.

Can I have inputs without forms in a Rails view?

I want to have a few check boxes on my page, and a link that accesses the value of these check boxes to pass in a link_to helper. I did not want to use a form because the view essentially has a number of links interspersed, and it doesn't naturally seem to be a form with one logical submit button.
I have something like
<% for p in #some_array %>
<!--other stuff .... -->
<input value=<%= p.id %> id=<%= p.id %> name="selected[]" type=checkbox>
<!--other stuff .... -->
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
But it doesn't seem to recognize the variable selected which stores the inputs. It raises
undefined local variable or method `selected' for #<#<Class:0x000001021b4a38>:0x00000102319a90>
I would say the last line in your code snippet is causing the error:
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
As ruby complains about a selected not being defined.
(Although I can't see why you don't want to use a form, as forcing the selections into something you can pass to the link would require some javascript magic.)