I have a simple form as follows
<% provide(:title, 'View Mail') %>
<h1>View Mail</h1>
<div class="row">
<%= form_tag('/mails/new') %>
<%= label_tag "Email Address" %><%= email_field_tag(:email) %><br>
<%= label_tag "Password" %><%= password_field_tag(:password) %><br>
<%= submit_tag "View my Mails" %>
</div>
<% end %>
What I actually want to do is capture the email and password field and forward them to another controller 'mails' so that I can use the value of email and password in that controller and then show the appropriate details.This is just a sample app for me to check something as I am new to rails.
What exactly should be in place of
<%= form_tag('/mails/new') %>
<% provide(:title, 'View Mail') %>
<h1>View Mail</h1>
<div class="row">
<%= form_tag('/mails/new') do %>
<%= label :email %>
<%= text_field :email) %><br>
<%= label :password %>
<%= password_field :password %><br>
<%= submit_tag "View my Mails" %>
<% end %>
</div>
<% end %>
You need to define 'mails/new' path, in config/routes.db to be able to access that path.
Hopefully it answers your problem.
Related
I have a Rails 3.2.19 app using twitter-bootstrap-rails 2.2.6 and bootstrap-datepicker-rails 1.3.0.2.
I am trying to get a text_field in a form to display the datepicker but nothing happens when I click or focus on the field.
When I inspect the element I see the following:
<input data-behaviour="datepicker" id="project_due_date" name="project[due_date]" size="30" type="text">
When checking the console I do not receive any JS/Coffee errors.
Here is my code:
projects.js.coffee
$(document).on "focus", "[data-behaviour~='datepicker']", (e) ->
- $(this).datepicker
- format: "yyyy-mm-dd"
- weekStart: 1
- autoclose: true
projects/_form.html.erb
<%= form_for(#project, :html => { :class => "well"}) do |f| %>
<% if #project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#project.errors.count, "error") %> prohibited this contact from being created:</h2>
<ul>
<% #project.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :project_name %>
<%= f.text_field :project_name, placeholder: "Build Spreadsheet", required: true %>
<%= f.label :project_manager %>
<%= f.collection_select(:user_id, User.all, :id, :full_name)%>
<%= f.label :due_date %>
<%= f.text_field :due_date, 'data-behaviour' => 'datepicker' %>
</br>
<%= f.button :submit, :class => 'btn btn-info' %>
<% end %>
I've tried also calling it based on id and have had no luck. I see the same behavior in Chrome, Firefox, or Safari.
Any help is greatly appreciated.
It looks like my CoffeeScript was wrong. I changed it to the following:
$ ->
$('[data-behaviour~=datepicker]').datepicker(format: "yyyy-mm-dd", autoclose: true)
Now I'm able to select the field and the datepicker comes up.
I'm trying to create inline radio buttons which also has its label inline (at the very left).
As I state in the title, using Bootstrap and SimpleForm.
My code looks like this:
<%= f.input :breakdown_or_size,
:collection => ["Breakdown", "Manual"],
:as => :radio_buttons,
:item_wrapper_class => 'inline',
:label => 'Select One: ' %>
This is what I get:
(source: webpagescreenshot.info)
I looked high and low for simple_form_for help. In the end I couldn't find anything, but at least with form_for you can do this:
<%= form_for(#book) do |f| %>
<% if #book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% #book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions", style='inline'>
<%= f.label 'Select One: ' %>
<br/>
<%= f.radio_button :collection, "Breakdown" %> Breakdown
<%= f.radio_button :collection, "Manual" %> Manual
<br/>
<%= f.submit %>
</div>
Which at least solves the inline rendering.
My microposts have a column called type which is a string. This can either be purchase or sale. I want two seperate input forms, where if you input content into one then it automatically fills in purchase as the type (when creating the micropost) and if you input content into the other it automatically fills in sale. .
Here is my form as it is
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
I recently added type as a column in the micropost data table. That is why there is no input for it (yet). Read above for how I want the type to be automatically filled in
Im thinking one way to do it is to id each form with something. Then when the form is being filled in, I could somehow tell it to automatically fill in the hidden type field based on which form is filled in. IS this possible??
I think you are referring to the hidden_field here. Use hidden_field to pass information to the controller without presenting it to the user. Here is one form for the purchase:
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field :type, 'purchase' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and another for the sale:
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to sell?" %>
<%= f.hidden_field :type, 'sale' %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
right now I have two forms in a row
<section>
<%= render 'shared/micropost_form_purchase' %>
<%= render 'shared/micropost_form_sale' %>
</section>
then for _micropost_form_purchase.html.erb
<%= form_for(#micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "purchase" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
and for _micropost_form_sale.html.erb I have
<%= form_for(#micropost, :html => { :id => "sale" }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field no-indent">
<%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
<%= f.hidden_field_tag :type, :value => "sale" %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
so I want the first micro post to automatically become a purchase micropost (I have a column in the micropost database called type that is a string that I want to depict either sale or purchase) and for the second one I want it to become a sale micropost. I was using hidden_field_tag because I thought you didn't have to define it in the controller, but am I wrong? Is hidden_field more appropriate? how can I use hidden_field_tag?
You can use:
<%= f.hidden_field :type, :value => "sale" %>
or:
<%= hidden_field_tag 'micropost[type]', "sale" %>
but not:
<%= f.hidden_field_tag :type, :value => "sale" %>
Using f.hidden_field will use the value from the variable #micropost, whereas hidden_field_tag will not use that.
It should be f.hidden_field not f.hidden_field_tag as you're using the model's form helpers :)
I am write:
<%= form_for(current_user, :remote => true) do %>
<p>
<%= label_tag t("language") %>:
<%= select_tag "language", options_for_select([["Русский", "rus"], ["English", "eng"]]) %>
</p>
<p><%= submit_tag t "options.save" %></p>
<% end %>
Inspector:
http://deeflow.com/changer/inspect.png
Content:
http://deeflow.com/changer/content.png
But, value in db doesn't updated
<%= form_for(current_user, :remote => true) do |f| %>
<p>
<%= f.label :language, t("language") %>:
<%= f.select :language, options_for_select([["Русский", "rus"], ["English", "eng"]]) %>
</p>
<p><%= f.submit t "options.save" %></p>
<% end %>
Notice the variable |f| and change of label_tag, select_tag and submit_tag to f.label, f.select and f.submit
In rails form_for and corresponding form_buider object(|f|) are used to group values under a common key, which rails can understand. *_tag helpers are generally used to pass unrelated parameters.