Bootstrap (2.3.1) + SimpleForm (2.1) inline label for radio button group - ruby-on-rails-3

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.

Related

Rails bootstrap-datepicker-rails not working

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.

Pass values of a submitted form to another controller in rails3

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.

link_to current page rails

I was wondering what the syntax is to link to an attribute on the same page
I have a list of dates
<ul>
<% #response.each_pair do |date, movie| %>
<li><%= link_to date_format(date), date, :class => 'scroll_to' %></li>
<% end %>
</ul>
These then have movies underneath each date like so
<% #response.each_pair do |date, movie| %>
<h3 class="resultTitle fontSize13">Available on <%= date_format(date) %></h3>
<% movie.each do |m| %>
<div class="thumbnail clearfix">
<img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>>
<div class="caption pull-right">
<%= link_to m.name, m.title_id, :class => 'resultTitle fontSize11' %>
<p class="bio"><%= m.bio %></p>
<p class="resultTitle">Cast</p>
<p class="bio"><%= m.cast.join(", ") unless m.cast.empty? %></p>
<%= link_to "Remind me", reminders_path(:title_id => m.title_id), :method => :post, :class => 'links button' %>
</div>
</div>
<% end %>
<% end %>
What i would like to achieve is that when a user clicks on a date in the list then it will take them to that date with movies on the same page
My attribute for each date is
"release_date"
Do i need to link to that, maybe a piece of Jquery aswell to scroll down to that date? or would it jump to the date in one step?
Any advice appreciated, I have linked to other pages before but not the same page like this
EDIT
I have tried this but the page just re renders
<li><%= link_to date_format(date), params.merge(:release_date => 'release_date'), :class => 'scroll_to' %></li>
Am i on the right track?
Thanks
All you need is to give your date a unique identifier and link to that in you list. For instance below we give the h3 for each date an id relative to the date object. The browser knows how to handle internal links and will simply jump to the corresponding identifier. Notice how the id of the field you're linking to gets appended to the end of the url when you click on the link.
<ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
<% end %>
</ul>
<%- index = 0 %>
<% #response.each_pair do |date, movie| %>
<%- index += 1 %>
<h3 class="resultTitle fontSize13" id="<%= index %>">Available on <%= date_format(date) %></h3>
<% movie.each do |m| %>
<div class="thumbnail clearfix">
<img class="pull-left" src=<% if m.image_link %> <%= m.image_link %> <% else %> "/assets/noimage.jpg" <% end %>>
<div class="caption pull-right">
<%= link_to m.name, m.title_id, :class => 'resultTitle fontSize11' %>
<p class="bio"><%= m.bio %></p>
<p class="resultTitle">Cast</p>
<p class="bio"><%= m.cast.join(", ") unless m.cast.empty? %></p>
<%= link_to "Remind me", reminders_path(:title_id => m.title_id), :method => :post, :class => 'links button' %>
</div>
</div>
<% end %>
<% end %>
For the added JQuery transition you can replace
<li><%= link_to date_format(date), "##{index}", :class => 'scroll_to' %></li>
with
<li><%= link_to_function date_format(date), "$('html, body').animate({scrollTop:$('##{index}').offset().top }, 'slow');", :class => 'scroll_to'%></li>
The above isn't exactly a DRY approach, but the important thing to abstract out of that is that you're linking to a unique ID elsewhere on the page and not the code itself.

how to update subcategory in Rails

I have Category and Products, Category has many product in it. I'm able to Edit Delete Create the Category, Also able to Create Delete Products in the Each Category, but want to Edit the each Product.
I can access the single Product of particular Category by using link_to , and product controller is receiving the the product of particular Category.
In html of Category where all Product belongs of that category has
<%= link_to 'Edit', edit_category_product_path(product.category, product) %>
Controller of Product, edit function is
#product = Product.where(params[:id])
then my edit html is
<%= form_for #product do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', category_products_path %>
I'm getting this error when try to Edit the product
NoMethodError in Products#edit
Showing C:/Sites/propoolpro6/app/views/products/edit.html.erb where line #3 raised:
undefined method `model_name' for ActiveRecord::Relation:Class
Extracted source (around line #3):
1: <h1>Editing product</h1>
2:
3: <%= form_for #product do |f| %>
4: <% if #product.errors.any? %>
5: <div id="error_explanation">
6: <h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
Rails.root: C:/Sites/propoolpro6
Application Trace | Framework Trace | Full Trace
app/views/products/edit.html.erb:3:in `_app_views_products_edit_html_erb___584392485_32651052'
Request
Parameters:
{"category_id"=>"1",
"id"=>"3"}
Note: i have used this 2, but same error,
<%= form_for(#product) do |f| %>
I think following line create array
#product = Product.where(params[:id])
which could not be used by the form_helper (expecting a Model name generally provided by an ActiveRecord Relation).you can inspect the #product to see the result.
so it is better if you use find instead of where like
#product = Product.find(params[:id])
I hope this answer would help you.
Thanks.

rails 3 form remote

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.