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.
Related
I want this function, if the status of progress so that it changes on the done exactly at those IDs where the status is for the conditions, at the moment I get an error and secondly, if it worked without errors, then it would change everything, but I need if progress is finished and if you don’t, then on progress
I selected three records through the form using a checkbox ...
in the function, I want that if the status of the record from database is "done", then it should change to "progress", if the status is "progress" in the record, then it should be changed to "done". Now I click on "submit_tag" and I get an error and in any case this code will not work the way I want it, I want everything to be conditional.
I am completely in ruby, help please, maybe the problem is in the syntax
no implicit conversion from nil to integer
def update_me
#iteam = Iteam.find(params[:id])
if #iteam[params[:status]] == 'progress'
Iteam.where(params[:id]).update_all(status: 'DONE')
else
Iteam.where(params[:id]).update_all(status: 'progress')
end
end
index view
<%= form_tag update_me_iteams_path, :method =>'put' do %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th>Status</th>
<th></th>
</tr>
<% #iteams.each do |iteam| %>
<tr>
<td><%= iteam.id %></td>
<td><%= iteam.title %></td>
<td><%= iteam.text %></td>
<td><%= iteam.status %></td>
<td><%= link_to 'Show', iteam_path(iteam) %></td>
<td>
<%= check_box_tag "id[]", iteam.id %>
</td>
</tr>
<% end %>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
Please Use following to find with id:
Iteam.where(id: params[:id])
or you can use the following
Iteam.find(params[:id])
I have an html table that renders a partial as follows
<tbody id="horse_logs">
<%= render #horse_logs %>
</tbody>
This calls _horse_log.html.erb and iterates through each element in #horse_logs
<tr id="<%= dom_id(horse_log) %>">
<td><%= horse_log.id %></td>
<td><%= horse_log.name %></td>
<td><%= horse_log.boarder.first_name%></td> <!-- This is the association I want to access -->
<td><%= horse_log.arrival_date %></td>
<td><%= horse_log.monthly_boarding_fee %></td>
<td><%= link_to '<i class="link blue large id card icon"></i>'.html_safe, horse_log_path(horse_log)%></td>
<td><%= link_to '<i class="link green large edit icon"></i>'.html_safe, edit_horse_log_path(horse_log), remote: true %></td>
<td><%= link_to '<i class="link red large remove icon"></i>'.html_safe, horse_log_path(horse_log), method: :delete, :data => {:confirm => 'Are you sure?'}, remote: true %></td>
</tr>
The issue is that accessing the boarder association inside the partial gives the following error
undefined method `first_name' for nil:NilClass
So basically the way rails implements this, they seem to be stripping the association data. The association is set up in my models correctly, and I can access the data in all other ways EXCEPT inside the partial.
How can I force rails to include the entire association.
here is my controller method if this helps
def index
#horse_logs = HorseLog.all.paginate(:page => params[:page], :per_page => 9).order("created_at DESC")
#horse_log = HorseLog.new
end
There may be a better way to do this but I would try.
<tbody id="horse_logs">
<% #horse_log.each do |horse_log| %>
<%= render 'horse_logs', horse_log: horse_log %>
<% end %>
</tbody>
If you get the same error, then you will need to make sure that your association is set up correctly.
Does every horse_log have a boarder? If not, you will get this error every time you run this code. you will need to check that hourse_log.boarder isn't nil.
For a quick fix, try this. It will prevent the error and return nil gracefully
<td><%= horse_log.try(&:boarder).try(&:first_name) %></td>
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.
I'm a total newbie to rails, and want to stop making the same tedious reports in excel and create a marketing analytics dashboard.
I have a User model, with created_at, marketing_source, purchases, and revenue.
What I want to do in sql is
select marketing_source, weekofyear(created_at), count(id), sum(revenue)
from User
where weekofyear(created_at) between # and #
group by marketing_source, weekofyear(created_at)
and then print this as a table to my page.
I'm just not exactly sure where and how to do the transformation. Do I put this via
sql.execute
in the controller code? Should I create a rake task to make a csv and then use javascript to read the csv and print out the table?
Any direction would be extremely appreciated.
Welcome to rails
If you are new to rails check out railscasts.com and http://guides.rubyonrails.org/
In rails you don't have to write sql statements like this.
I am assuming you have done the database migrations and have all the necessary columns in you database
in your users_controller, I think this would do what you want.
def index
#users = User.where
(["created_at >= ? AND created_at <= ?", yesterday.beginning_of_day, yesterday.end_of_day]).
group("marketing_source")
end
Then in your corresponding view
/views/users/index.html.erb
<table>
<tr>
<th>Name</th>
<th>revenue</th>
<th>Marketing source</th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%=h user.name %></td>
<td><%=h user.revenue %></td>
<td><%=h user.marketing_source %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
Right now, if I go to the index action of a model that I have, I don't show the basic table of data that rails generates for me if there are no existing records in the database. I do something like:
<% if #my_records.count > 0 %>
<table>
<tr>
<th>...</th>
<th>...</th>
<th>...</th>
<th>etc</th>
</tr>
<% #my_records.each do |my_record| %>
<tr>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
<td><%=h my_record.etc %></td>
</tr>
<% end %>
</table>
<% end %>
This works locally. However, when I push my app to heroku, this causes a 500 error and the log says:
ActionView::TemplateError (undefined method 'count' for []:Array) on line ...
So I change it to .length and it works fine. Can anyone tell me why that is? Someone has told me that these were redundant and rails got rid of .count but my understanding was that .length is an Array function that tells you how many items are in the Array and .count was an ActiveRecord method for determining how many items in the array were actual records in the database.
Can anyone shed light on this for me?
That's ruby issue, not rails. Locally you probably have 1.8.7, and heroku has 1.8.6. The Enumerable#count method was introduced in 1.8.7: compare http://ruby-doc.org/core-1.8.6/classes/Enumerable.html and http://ruby-doc.org/core-1.8.7/classes/Enumerable.html.