Unable to create bootstrap-themed scaffold in Ruby on Rails - ruby-on-rails

I'm trying to generate a bootstrap-themed scaffold via the following actions:
Add gem 'twitter-bootstrap-rails' line to the end of the Gemfile and run bundle install
Run rails generate bootstrap:install static as stated in the documentation
Place the DB account details (username and password) to the "default" section of database.yml file and run rake db:create
Run rails g scaffold Purchase company_name:text product_name:text contact_person:text email:text comment:text
Run rake db:migrate
Run rails g bootstrap:themed Purchases
Every command returns 0, so I restarted a web-server and go to the 127.0.0.1:3000/purchases, but it looks like it doesn't use twitter bootstrap at all:
purchases/index.html.erb
<h1>Listing purchases</h1>
<table>
<thead>
<tr>
<th>Company name</th>
<th>Product name</th>
<th>Contact person</th>
<th>Email</th>
<th>Comment</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #purchases.each do |purchase| %>
<tr>
<td><%= purchase.company_name %></td>
<td><%= purchase.product_name %></td>
<td><%= purchase.contact_person %></td>
<td><%= purchase.email %></td>
<td><%= purchase.comment %></td>
<td><%= link_to 'Show', purchase %></td>
<td><%= link_to 'Edit', edit_purchase_path(purchase) %></td>
<td><%= link_to 'Destroy', purchase, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Purchase', new_purchase_path %>
Why? What am I doing wrong? How can I fix it?
I'm using Ruby on Rails 4.1.4 btw.

Try these:
Make sure the <table> tag has the class(es) required by Bootstrap. For example: <table class="table table-striped">.
Also, the scaffold-generated styles in app/assets/stylesheets/scaffolds.scss might be interfering. Remove them and see what happens.
And as #anusha said in a comment, remember to use the singular form when you run the generator. For example: rails g bootstrap:themed Purchase, not Purchases.

Related

users names not showing in users view

Can anyone tell me why the email addresses are not showing in the users list view below?
<p id="notice"><%= notice %></p>
<h1>Users</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
**<td><%= user.email %></td>**
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
I'm not familiar with seeing the two asterisks around the element. I'm thinking that might be messing something up. If you want to bold the user's email you could try something like <td><b><%= user.email %></b></td>.
Are you getting any errors anywhere?
If the asterisks are just there to help us see the line of code, and you say that you see the users (and their emails) in the db, I think it could be one of two other relatively minor things.
Whats your controller look like for this view? Are you passing #users to this view? You could try just dumping #users on the page to make sure that it's available.
Is the field on your User model that holds the email called email?

How Do I Get an Modified Column from a Rails Scaffold to Hold Information as the others?

I generated a scaffold for a To-Do List app, and I left out some columns to add later.
I ran the command to create a migration to add a new column named client and I changed my files so that it shows on the projects index and form, but when i enter something into the client field and submit, it doesn't save the information and remains blank..
Update 1:
Here is whats in my routes:
'
Rails.application.routes.draw do
root :to => 'projects#index'
resources :projects
end
'
Here is my index view:
'
<h1 id="title">Project List</h1>
<table>
<thead>
<tr id="headers">
<th>Title</th>
<th>Client</th>
<th>Description</th>
<th>Hours</th>
<th>Done</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody class="col-md-2" id="listItems">
<% #projects.each do |project| %>
<tr id="table">
<td><%= project.title %></td>
<td><%= project.client %></td>
<td><%= project.description %></td>
<td><%= project.hours %></td>
<td><%= project.done %></td>
<td><%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %></td>
<td><%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %></td>
<td><%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path, id:"new" %>
<footer id="footer">Copyright 2014 Kira Banks</footer>
'
To keep your application secured, Rails has a feature called Strong Parameters and the docs says:
It provides an interface for protecting attributes from end-user
assignment. This makes Action Controller parameters forbidden to be
used in Active Model mass assignment until they have been whitelisted.
So, basically you need to whitelist the new client attribute in the Projects controller by adding it to the list:
class ProjectsController < ApplicationController
# ...
# at the end of the file
private
def project_params
params.require(:project).permit(:title, :description, :hours, :done, :client)
end
end

Why won't this rspec test pass?

I've spent the last hour trying to figure out why this test won't pass.
I'm working in Rails 4.0 with Capybara 2.1.0 and rspec 2.14.1
Here's my test:
require 'spec_helper'
describe "Stats" do
subject { page }
describe "Index Page" do
before { visit stats_path }
it { should have_selector 'h1', text: 'Stats' }
end
end
And my index.html.erb file:
<h1>Stats</h1>
<table>
<thead>
<tr>
<th>Attendance</th>
<th>Salvations</th>
<th>Visitors</th>
<th>Offering</th>
<th>Service Date</th>
<th>Time</th>
<th>Campus</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #stats.each do |stat| %>
<tr>
<td><%= stat.attendance %></td>
<td><%= stat.salvations %></td>
<td><%= stat.visitors %></td>
<td><%= stat.offering %></td>
<td><%= stat.date %></td>
<td><%= stat.time %></td>
<td><%= stat.campus.name %></td>
<td><%= link_to 'Show', stat %></td>
<td><%= link_to 'Edit', edit_stat_path(stat) %></td>
<td><%= link_to 'Destroy', stat, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Stat', new_stat_path %>
And the error I'm getting is:
1) Stats Index Page should have css "h1" with text "Stats"
Failure/Error: it { should have_selector 'h1', text: 'Stats' }
Capybara::ExpectationNotMet:
expected to find css "h1" with text "Stats" but there were no matches
# ./spec/features/stats_spec.rb:7:in `block (3 levels) in '
I'm stumped. Any help would be appreciated.
Incidentally, my index page renders correctly without any errors.
If it works when you visit then page but not when you run your test script then there is certainly a session or authentication matter. Is your index page protected by any authentication or authorization logic?
I cannot give you a solution, but I can try to give you a guide line, if you haven't do it yet, use pp to output to the console the rendering of the page.
For example, if you are testing in controllers directory, use pp response.body. Or if you are testing in views directory, use render followed by pp rendered.
Then from the output in your console you will certainly get a clue according to your specific logic on what's append.

Rails Links with Model Object Path

I have the following table, and I want the name of my "restaurant" to be clickable and link to the page of that restaurant.
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= rest.name %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
How am I going to add something like rest_path to:
Link text
You could try this :
<%= link_to rest.name, rest %>
If you have defined a resource restaurant in your config/routes.rb, it will target the url given by the helper restaurant_path(rest) (ie /restaurants/id_of_restaurant).
As the restaurant is a child of a city you might want to have an url like /cities/id_of_city/restaurants/id_of_restaurant so you could try the following :
<%= link_to rest.name, city_restaurant_path(#city, rest) %>
Be sure to have this in your config/routes.rb in order to generate the corresponding helpers.
resources :cities do
resources :restaurants
end
Then you will be able to see all the available helpers for your routes using the command 'rake routes' in the terminal.
Okay so I did something like this
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= link_to rest.name, rest %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
</table>
but now the link is very ugly when I hover on it? How do I get rid of that?

how do i fix undefined method `user_id' for nil:NilClass?

i am trying to start my server after a db:migrate:reset and suddenly my SQlite3 server will not start. I get the error: ActionView::Template::Error (undefined method 'user_id' for nil:NilClass) when the server begins to render my datum/index page.
Before i did this i had actual prices in my database so the user_id could be detected and everything worked but now since the prices are gone i believe its giving this error.
Controllers - Datum & Price:
def index
#prices = Price.all
end
Views - datum/index & prices/index:
<h1>Prices</h1>
<table>
<tr>
<th>User</th>
<th>Date</th>
<th>Price name</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #prices.each do |price| %>
<tr>
<td><%= price.user_id %></td>
<td><%= price.date %></td>
<td><%= price.price_name %></td>
<td><%= price.price %></td>
<td><%= link_to 'Show', price %></td>
<td><%= link_to 'Edit', edit_price_path(price) %></td>
<td><%= link_to 'Delete', price, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Price', new_price_path %>
I think i am doing this the wrong way as i am new to Rails. My goal was to duplicate my prices/index view so my datum/index is the same so i could then give both unique looks. How do i correct this issue and am i doing this correctly?
I'm guessing that you don't know what rake db:migrate:reset does. There's no description string for it so don't ask rake what it does, you have to look at the source:
# desc 'Resets your database using your migrations for the current environment'
task :reset => ['db:drop', 'db:create', 'db:migrate']
So rake db:migrate:reset destroys your database (including any data you had in it), recreates it, and then applies the migrations to bring everything up to date again. But, all your original data is still gone.
The db:drop part of db:migrate:reset probably explains why you're getting nil all over the place. However, you should be getting an empty array from Price.all if all your data was gone so perhaps you've added something after your reset.
Weird, i think it was giving this error because i was using AptanaStudio 3 with the git terminal. I just restarted everything and it started working now as if the Database needed time to refresh itself. So in conclusion just restart everything and see if it works then.

Resources