I have simple RoR 3.2 application with Twitter bootstrap 2.1.0 (i implemented it via twitter-bootstrap-rails gem). I want to integrate sidebar with few links (as you can see on twitter bootstrap page on the left side) but i can't get how to implement this (yes, i'm noob). Does anyone have solution how to do that in Rails?
My application layout:
!!!
%html
%head
%title MyApp
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%meta{ :name => "viewport", :content => "width=device-width, initial-scale=1.0" }
%body
%div{ :class => "wrapper" }
= render 'layouts/navbar_template'
%div{ :class => "container-fluid" }
- flash.each do |key, msg|
%div{ :class => "alert alert-#{key}" }
%button{ :type => "button", :class => "close", "data-dismiss" =>"alert" }×
= msg
%div{ :class => "row-fluid" }
%div{:class => "span10"}
=yield
%div{:class => "span2"}
-# I would like to have sidebar here
%ul.nav.nav-list.bs-docs-sidenav.affix
%li.active
%a{:href => "#dropdowns"}
%i.icon-chevron-right
Dropdowns
%li
%a{:href => "#buttonGroups"}
%i.icon-chevron-right
Button groups
%li
%a{:href => "#buttonDropdowns"}
%i.icon-chevron-right
Button dropdowns
%li
%a{:href => "#navs"}
%i.icon-chevron-right
Navs
%li
%a{:href => "#navbar"}
%i.icon-chevron-right
Navbar
%li
%a{:href => "#breadcrumbs"}
%i.icon-chevron-right
Breadcrumbs
%li
%a{:href => "#pagination"}
%i.icon-chevron-right
Pagination
%li
%a{:href => "#labels-badges"}
%i.icon-chevron-right
Labels and badges
%li
%a{:href => "#typography"}
%i.icon-chevron-right
Typography
%li
%a{:href => "#thumbnails"}
%i.icon-chevron-right
Thumbnails
%li
%a{:href => "#alerts"}
%i.icon-chevron-right
Alerts
%li
%a{:href => "#progress"}
%i.icon-chevron-right
Progress bars
%li
%a{:href => "#misc"}
%i.icon-chevron-right
Misc
Related
I'm working from the book 'Agile Web Development with Rails 4' and I'm having some difficulty with my HAML code.
I'm not too sure what I've done wrong, but my form isn't rendering when I go to build a new product. I check the source code and it's not in the HTML either so something is wrong with my code but not sure what. Hopefully someone can help me.
This is my code for Form.html.haml
=if #product.errors.any?
%div{ :id => "error_explanation" }
%h2
=pluralize(#product.errors.count, "error")
prohibited this product from being saved:
%ul
=#product.errors.full_messages.each do |msg|
%li
=msg
%div{ :class => "field" }
=f.label :title
=f.text_field :title
%div{ :class => "field" }
=f.label :description
=f.text_area :description, rows: 6
%div{ :class => "field" }
=f.label :image_url
=f.text_field :image_url
%div{ :class => "field" }
=f.label :price
=f.text_field :price
%div{ :class => "actions" }
=f.submit
And this is my New.html.haml
%h1 New Product
=render 'form'
=link_to 'Back', products_path
Thank you in advance.
According to the answers provided by meagar and theTRON as well as your last comment:
Where are you bringing to light the form object? It seems like nowhere, thus you are getting that error. When you bind a form to a model object through the form_for method, it yields a form builder object (the f variable).
Try something like the following:
<%= form_for #product, url: {action: "create"} do |f| %>
# your code using f variable ...
<% end %>
Let us know if that finally fixed your code.
Partials need to be named with an _ prefix.
Your Form.html.haml must be called _form.html.haml.
In addition to ensuring that your form is named _form.html.haml you'll need to fix some nesting in your HAML. It should look something like this instead:
=if #product.errors.any?
%div{ :id => "error_explanation" }
%h2
=pluralize(#product.errors.count, "error")
prohibited this product from being saved:
%ul
=#product.errors.full_messages.each do |msg|
%li
=msg
%div{ :class => "field" }
=f.label :title
=f.text_field :title
%div{ :class => "field" }
=f.label :description
=f.text_area :description, rows: 6
%div{ :class => "field" }
=f.label :image_url
=f.text_field :image_url
%div{ :class => "field" }
=f.label :price
=f.text_field :price
%div{ :class => "actions" }
=f.submit
Your indentation that you currently have on your form fields placed it in the scope of the if #product.errors.any? block, which means the form would only appear if #product had errors.
I have created a normal
= simple_form_for #nacform, :html => { :multipart => true } do |f|
= f.error_notification
.row
.span12
%h4 Form Details
%hr
.row
.span3
.field
= f.input :Title
.field
= f.input :Description
.field
= f.label :asset, "File"
= f.file_field :asset
.form-actions
= link_to 'Back', nacforms_path, :class => 'btn btn-small btn-primary'
%a#modal_btn.btn.btn-success.btn-small{:href => "#modal"} Continue
and the modal
#modal.modal.hide
.about
.modal-header
%button.close{"aria-hidden" => "true", "data-dismiss" => "modal", :type => "button"} ×
%h2 Forms
= simple_form_for #nacform, :html => { :multipart => true } do |f|
= f.button :submit, "Upload Form", :class => 'btn btn-success'
:javascript
$("#modal_btn").click(function(){
$('#modal').modal();
});
the submit button for the form is on the modal, but when i click on that submit button nothing happens, Am I doing it wrong ?
In order for things to appear using jQuery show method or something derived from it, you generally specify an inline style that's easy to override:
#modal.modal{ :style => 'display:none' }
Having a .hide CSS class may be what's causing it to stay hidden.
I am currently using the ClientSideValidations gem and stuck while rendering a partial using ajax and trying to validate addresses inside that rendered partial with the gem mentioned above. Nothing happens when entering a wrong combination specified in the model.
If browsing to the address-form directly and trying out validations, everything works fine, just like specified.
Any hints or thoughts on how to make the validations gonna work inside the partial?
Thanks!
EDIT: No errors in JS console, just nothing happens when for example entering a too short zipcode (specified in the model with 5 digits). Btw I use haml for the views.
So the code in my view:
= link_to "Shipping", addresses_path, :remote => true
corresponding controller addresses_controller.rb
respond_to do |format|
...
format.js {render :layout => false}
...
end
corresponding index.js.erb
$("#ajax_content").html("<%= escape_javascript(render :partial =>
"partialXY") %>");
and corresponding partial
= form_for #address, :validate => true, :id => "address_form", :remote => true do |f|
- if #address.errors.any?
#error_explanation
%h2
= pluralize(#address.errors.count, "error")
prohibited this user from being saved:
%ul
- #address.errors.full_messages.each do |msg|
%li
=msg
%ul
%li
= f.label :type
= f.select :address_type, [['Billing Address', 'billing'],['Shipping Address',
'shipping']], :class => "address_selection"
%li
= f.label :gender
= f.select :gender, [['Male', 'male'],['Female', 'female']], :class => "text_field"
%li
= f.label :last_name
= f.text_field :last_name, :id => "last_name", :class => "text_field"
%li
= f.label :first_name
= f.text_field :first_name, :class => "text_field"
%li
= f.label :company_name
= f.text_field :company_name, :class => "text_field"
%li
= f.label :street
= f.text_field :street, :class => "text_field"
%li
= f.label :number
= f.text_field :number, :class => "text_field"
%li
= f.label :zipcode
= f.text_field :zipcode, :class => "text_field"
%li
= f.label :place
= f.text_field :place, :class => "text_field"
%li
= f.label :phone_no
= f.text_field :phone_no, :class => "text_field"
%li
= f.label :country
= f.text_field :country, :class => "text_field"
%li
= f.label :email
= f.text_field :email, :class => "text_field"
%li
= f.submit
so like I said nothing happens when validating the rendered partial inputs like zipcode, etc. The funny thing is, that if you look at the following, automatically by rails generated view for editing addresses, the validation works just fine.
rails generated view to edit address
=render 'partialXY'
I have been working on this issue for a long time and have absolutely no clue on how to fix this. I'm sure it has something to do with ajax since using validation when rendering the rails generated partial works just fine.
Thanks a lot! Phil
Ok I fixed it. Turned out despite giving the form an ID, the ID was a different one in the final html code. So I just added a
$('form.form_real_id').validate();
to my .js.erb file!
- #subjects.each do |s|
%tr
%td= s.position
%td= s.name
%td= s.visible ? "Yes" : "No"
%td= s.pages.size
%td= link_to("Show", {:action => "show", :id => s.id}, :class => "action show")
= link_to("Edit", {:action => "edit", :id => s.id}, :class => "action edit")
= link_to("Delete", {:action => "delete", :id => s.id}, :class => "action delete")
error_msg:
Illegal nesting: content can't be both given on the same line as %td and nested within it.
I want those three links--show, edit, and delete--in the same td; how can I do it?
You just need to change this:
%td= link_to("Show", {:action => "show", :id => s.id}, :class => "action show")
= link_to("Edit", {:action => "edit", :id => s.id}, :class => "action edit")
= link_to("Delete", {:action => "delete", :id => s.id}, :class => "action delete")
to this:
%td
= link_to("Show", {:action => "show", :id => s.id}, :class => "action show")
= link_to("Edit", {:action => "edit", :id => s.id}, :class => "action edit")
= link_to("Delete", {:action => "delete", :id => s.id}, :class => "action delete")
You should also indent the tds from the tr.
FYI - I ran into this issue too but the culprit was a trailing space after my <td>, which is content for HAML.
I'm new to all three, and I'm trying to write a simple contact form for a website. The code I have come up with is below, but I know there are some fundamental problems with it (due to my inexperience with sinatra). Any help at getting this working would be appreciated, I can't seem to figure out/find the documentation for this sort of thing.
haml code from the contact page:
%form{:name => "email", :id => "email", :action => "/contact", :method => "post", :enctype => "text/plain"}
%fieldset
%ol
%li
%label{:for => "message[name]"} Name:
%input{:type => "text", :name => "message[name]", :class => "text"}
%li
%label{:for => "message[mail]"} Mail:
%input{:type => "text", :name => "message[mail]", :class => "text"}
%li
%label{:for => "message[body]"} Message:
%textarea{:name => "message[body]"}
%input{:type => "submit", :value => "Send", :class => "button"}
And here is my code in sinatra's app.rb:
require 'rubygems'
require 'sinatra'
require 'haml'
require 'pony'
get '/' do
haml :index
end
get '/contact' do
haml :contact
end
post '/contact' do
name = #{params[:name]}
mail = #{params[:mail]}
body = #{params[:body]}
Pony.mail(:to => '*emailaddress*', :from => mail, :subject => 'art inquiry from' + name, :body => body)
end
I figured it out for any of you wondering:
haml:
%form{ :action => "", :method => "post"}
%fieldset
%ol
%li
%label{:for => "name"} Name:
%input{:type => "text", :name => "name", :class => "text"}
%li
%label{:for => "mail"} email:
%input{:type => "text", :name => "mail", :class => "text"}
%li
%label{:for => "body"} Message:
%textarea{:name => "body"}
%input{:type => "submit", :value => "Send", :class => "button"}
And the app.rb:
post '/contact' do
name = params[:name]
mail = params[:mail]
body = params[:body]
Pony.mail(:to => '*emailaddress*', :from => "#{mail}", :subject => "art inquiry from #{name}", :body => "#{body}")
haml :contact
end
In case anyone can use this, here is what you might need to use your gmail account to send mail.
post '/contact' do
require 'pony'
Pony.mail(
:name => params[:name],
:mail => params[:mail],
:body => params[:body],
:to => 'a_lumbee#gmail.com',
:subject => params[:name] + " has contacted you",
:body => params[:message],
:port => '587',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'lumbee',
:password => 'p#55w0rd',
:authentication => :plain,
:domain => 'localhost.localdomain'
})
redirect '/success'
end
Note the redirect at the end, so you will need a success.haml to indicate to the user that their email was sent successfully.
Uhmm, i tried in irb the following:
foo = #{23}
Of course it wont work! the '#' is for comments in Ruby UNLESS it occurs in a string! Its even commented out in the syntax highlighting.
What you wanted was:
name = "#{params[:name]}"
as you did in your solution (which is not necessary, as it already is a string).
Btw, the reason why the code does not throw an error is the following:
a =
b =
42
will set a and b to 42. You can even do some strange things (as you accidentally did) and set the variables to the return value of a function which takes these variables as parameters:
def foo(a,b)
puts "#{a.nil?} #{b.nil?}" #outputs 'true true'
return 42
end
a =
b =
foo(a,b)
will set a and b to 42.
#{} is interpolation that is used inside "". Just using it outside for a variable assignment won't work.
It would be more likely to be used like this:
number_of_people = 15
Puts "There are #{number_of_people} scheduled tonight"
I've created an example of this in two parts that is available on github. The signup form app is here: signup-form-heroku and an example of the static website that interacts with this is here: static-website-to-s3-example.
The form app is built using Sinatra and is ready to deploy straight onto Heroku. The static site is ready to deploy straight to S3 and use amazon cloudfront.