NoMethodError in Awards#index - ruby

I am just starting on rails and found a problem which I can't solve. I am creating a simple app with two models (awards and students). :award has_one :student and :student belongs_to :award
I was told that in app/views/awards/ in show.html.erb, replace:
<%= #award.student_id %>
with:
<%= #award.student.given_name %> <%= #award.student.family_name %>
and in index.html.erb, replace:
<%= award.student_id %>
with:
<%= award.student.given_name %> <%= award.student.family_name %>
after this the localhost:3000/students is working fine but localhost:3000/awards is not working and giving following error :-
**NoMethodError in Awards#index**
Showing /home/redblink/rbtest/students/app/views/awards/index.html.erb where line #17 raised:
undefined method `given_name' for nil:NilClass
Extracted source (around line #17):
14: <tr>
15: <td><%= award.name %></td>
16: <td><%= award.year %></td>
17: <td><%= award.student.given_name %> <%= award.student.family_name %></td>
18: <td><%= link_to 'Show', award %></td>
19: <td><%= link_to 'Edit', edit_award_path(award) %></td>
20: <td><%= link_to 'Destroy', award, method: :delete, data: { confirm: 'Are you sure?' } %></td>
please reply asap with the solution.....

The problem is that there is one of the awards record whose corresponding students record is not present so its returning nil and u can call given_name on nil.
So you can handle it as:
Write following:
<%= award.student ? award.student.given_name : '' %> <%= award.student ? award.student.family_name : '' %>
instead of
<%= award.student.given_name %> <%= award.student.family_name %>

Related

approve multiple records at once

how to achieve this Scenario:
admin to select multiple records using a checkbox and click a button to approve selected, which will change the status of the selected items to approved at once. records are like posts.
here is my code:
config/routes:
resources :time_cards do
collection do
get 'management'
put 'approve_multiple'
end
member do
get 'review'
get 'tracking'
patch 'approve_or_reject'
end
end
html:
<%= form_tag({controller: 'time_cards', action: 'approve_multiple'}, method: 'put', id: 'approve_multiple_time_card_form') do %>
<% #time_cards.each do |time_card| %>
<tr>
<% if time_card.status.downcase == 'submitted' %>
<td><%= check_box_tag "time_cards_to_approve[]", time_card.id %></td>
<% else %>
<td> </td>
<% end %>
<td><%= time_card.id %></td>
<td><%= time_card.week_of.to_date.strftime('%B %-d, %Y') %></td>
<td><%= time_card.user.name %></td>
<td><%= time_card.status %></td>
<td><%= output_hours(time_card.time_card_entries) %></td>
<td>
<%= link_to review_time_card_path(time_card) do %>
<% if time_card.status.downcase == 'submitted' %>
<i class="material-icons">edit</i>
<% end %>
<% end %>
</td>
</tr>
<% end %>
<%= submit_tag "Approve Selected", class: 'btn btn-success' %>
<% end %>
controller:
#PUT /time_cards/approve_multiple
def approve_multiple
authorize TimeCard
#time_cards.update_all({status: 'Approved'}, {id: params[:time_cards_to_approve]})
respond_to do |format|
if !#time_cards.errors
format.json { render json: { notice: 'Time cards approved successfully.'} }
format.html { redirect_to redirect_to management_time_cards_path, notice: 'Time cards approved successfully.'}
else
format.json { render json: { error: #time_cards.errors, alert: 'There was a problem approving the time card.'}}
format.html { redirect_to management_time_cards_path, alert: #time_cards.errors}
end
end
end
??? I don't no where it is wrong?
Send these ids to an action like this
Post.where(id: params[:ids]).update_all(approved: true)
params[:ids] is an array of posts' ids
update_all returns the number of the updated records
Ops, I took for granted we were talking about rails but it's never mentioned.

How can I render partial in show.html.erb

I have these two models in my Ruby on Rails application - Artist and Song. They are associated as follows:
class Artist < ActiveRecord::Base
has_many :songs
attr_accessible :artist_name
and
class Song < ActiveRecord::Base
belongs_to :artist
attr_accessible :title, :track_URL, :artist_id
I have this code in views/artist/show.html.erb:
<%= render 'artist_song' %>
<table>
<% #artist.songs.each do |song| %>
<tr>
<td><%= song.title %></td>
</tr>
<% end %>
</table>
The partial Im trying to render(_artist_song.html.erb) in the same view looks like this:
<table>
<% #artist = Artist.all %>
<% #artist.each do |artist| %>
<tr>
<td><%= link_to artist.artist_name, artist %></td>
</tr>
<% end %>
</table>
The way it is suppose to work is when I click on an artist shown trough the partial, the code below the partial has to show me all the songs that belongs to the particular artist.
Both the partial and the code in the table tag are working individually. But when I put them together, it looks like there is a conflict between them and the server is showing me this No Method Error:
NoMethodError in Artists#show
Showing C:/Sites/OML/app/views/artists/show.html.erb where line #9 raised:
undefined method `songs' for #<Array:0x5fe1418>
Extracted source (around line #9):
6:
7:
8: <table>
9: <% #artist.songs.each do |song| %>
10: <tr>
11: <td><%= song.title %></td>
12: </tr>
Rails.root: C:/Sites/OML
Application Trace | Framework Trace | Full Trace
app/views/artists/show.html.erb:9:in `_app_views_artists_show_html_erb__950110288_54062208'
app/controllers/artists_controller.rb:21:in `show'
I couldn`t find a solution. Any help will be appreciated.
Thank you.
You are not using a partial. You need to render the partial inside the artist loop:
show.html.erb
<table>
<% #artists = Artist.all %>
<% #artists.each do |artist| %>
<tr>
<td><%= link_to artist.artist_name, artist %></td>
</tr>
<%= render :partial => 'artist_song', :artist => artist %>
<% end %>
</table>
This way, you are passing the current artist object to inside the partial, so there you can do:
artist_song.html.erb
<% artist.songs.each do |song| %>
<tr>
<td><%= song.title %></td>
</tr>
<% end %>

Rails 3 : Mass update

I want to use mass-update every operation in a single view, with a single update button. Using this following code, Rails thows this error
Showing /home/vincent/git/gestion/app/views/operations/tag.html.erb where line #23 raised:
undefined method `merge' for 1:Fixnum
Extracted source (around line #23):
20: <td>
21: <% #tags.each do |elem| %>
22: <%= f.label elem.tag %>
23: <%= f.check_box "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
24: <% end %>
25: </td>
26: <td><%= f.submit %></td>
Models
class Operation < ActiveRecord::Base
attr_accessible :credit, :date_operation, :debit, :libelle, :tag_ids
has_and_belongs_to_many :tags
accepts_nested_attributes_for :tags, :allow_destroy=>true
end
class Tag < ActiveRecord::Base
attr_accessible :id, :tag
has_and_belongs_to_many :operations
end
Controller
def tag
#operations = Operation.limit(100)
#tags = Tag.all
respond_to do |format|
format.html { "tag" }# tag.html.erb
# format.json { render json: #operations }
end
end
View
<% #operations.each do |operation| %>
<tr>
<td><%= operation.date_operation %></td>
<td><%= operation.libelle %></td>
<td><%= operation.credit %></td>
<td><%= operation.debit %></td>
<%= form_for operation do |f| %>
<td>
<% #tags.each do |elem| %>
<%= f.label elem.tag %>
<%= f.check_box "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
<% end %>
</td>
<td><%= f.submit %></td>
<% end %>
</tr>
<% end %>
Do you have any clue/help about this problem?
Thank you in advance
Edit 1 : adding full stack trace
You need to change the f.check_box for check_box_tag, for instance:
<%= check_box_tag "operation[tag_ids][]", elem.id, operation.tags.include?(elem) %>
The problem in this scenario is that f.check_box is expecting that the value is bounded to the form which in not in this case.
use nested_form gem for this case i think it will work.
for more about nested_form Click here

Ajax : data retrieving in rails3 + formtastic

I have successfully tried ajax saving in my sample formtastic with ajax form.
The new value is added to the database. But the the problem is in retrieving the list from the database as soon as i save via ajax.
How to do it.?
As soon as I add a new record I want my displaying list to be update. Both the option to add new record and list the data from database is in same page
This is my Index page. The controller and all other created via scaffolding
<h1>Listing samples</h1>
<table>
<tr>
<th><%=t :Name%></th>
<th></th>
<th></th>
<th></th>
</tr>
<% #samples.each do |sample| %>
<tr>
<td><%= sample.name %></td>
<td><%= link_to 'Show', sample %></td>
<td><%= link_to 'Edit', edit_sample_path(sample) %></td>
<td><%= link_to 'Destroy', sample, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Sample', new_sample_path %>
<br /><br /><br /><br /><br />
<%= semantic_form_for #sample1,:url => samples_path, :remote => true do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
When you save via ajax, you may have to change the way your controller responds, kinda like this:
def create
# stuff
respond_to do |format|
format.js
end
end
When you do this, rails would expect you to have created a file named create.js.erb, in which you can manipulate the data of your view(append new content to your table, render a whole new partial with your new object list, etc):
$('#your_list').html(
"<%= escape_javascript(render('your_table_partial')) %>"
);
Or
$('#your_list').append(
"<%= escape_javascript(render('your_item_of_the_table_partial')) %>"
);
These are just examples, i don't know your code enough to write the correct code for you, but you sure can use these as a base for your work.
I did the CRUD operation using these guidelines and it worked perfect
http://stjhimy.com/posts/07-creating-a-100-ajax-crud-using-rails-3-and-unobtrusive-javascript

Rails form_tag needs refreshing

Hi I have been stuck on the following issue for days, and I just dont know whats going wrong...
I have the following form_tag code in my partial view, which is displayed with a remote add to cart from a main view:
<tr>
<% form_tag line_item, :method => :put do %>
<td><%= line_item.product.title %></td>
<td class="item_price" ><%= number_to_currency(line_item.total_price) %></td>
<td>
<%= text_field :line_item, :quantity, :size => 1, :value => line_item.quantity %>
<%= submit_tag 'update' %>
</td>
<% end %>
<td>
<%= button_to 'Delete Line Item', line_item, :method => :delete, :remote => true %>
</td>
</tr>
now when i click add to cart, the above is rendered in the id where I have the replace.html function targeted but...
i can't actually update the cart using the update button unless I refresh the page!! I have no idea what the matter is and its driving me mad!
Has anyone got any ideas what I may be doing wrong?
I think form_tag needs :remote => true also.

Resources