undefined method `map' for nil:NilClass in Rails with Paperclip - ruby-on-rails

This will be my last question for today (sorry if I'm asking too fast)
I'm getting the error undefined method 'map' for nil:NilClass
It says the problem is on this line: <td><%= image_tag #map.map.url %></td>
The whole index code is below:
<h1>Listing maps</h1>
<table>
<tr>
<th>Carname</th>
<th>Map</th>
<th>Criticalcomponentlocations</th>
<th>Warnings</th>
<th>Additionalinfo</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #maps.each do |map| %>
<tr>
<td><%= map.carname %></td>
<td><%= image_tag #map.map.url %></td>
<td><%= map.criticalcomponentlocations %></td>
<td><%= map.warnings %></td>
<td><%= map.additionalinfo %></td>
<td><%= link_to 'Show', map %></td>
<td><%= link_to 'Edit', edit_map_path(map) %></td>
<td><%= link_to 'Destroy', map, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Map', new_map_path %>
Maps Controller, Index:
def index
#maps = Map.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #maps }
end
end

In your #maps.each loop, you're trying to access a (probably) non-existent #map instance variable when you should be accessing the map variable that is local to your loop.
Try this instead:
<% #maps.each do |map| %>
<tr>
...
<td><%= image_tag map.map.url %></td>
...
</tr>
<% end %>

The variable you wish to use, map, is not an instance variable. It is a local variable, so you should use 'map.url' instead of '#map.map.url'

Your line of code <td><%= image_tag #map.map.url %></td> uses an instance variable #map, but you're inside the scope of the enumerator <% #maps.each do |map| %>. You should use the map.url local variable instead of #map.map.url.

Related

will_paginate (getting undefined method error for boards)

Maybe this is a newbie issue but I just can't figure out was going on
Getting an error when I refresh local host
undefined local variable or method `boards' for #<#:0x3c8eff8>
Extracted source (around line #14):
Here is the code in my index.html.erb for boards
Listing boards
<table>
<thead>
<tr>
<th>Name</th>
<th>About</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<%= will_paginate(boards) %>
<% #boards.each do |board| %>
<tr>
<td><%= board.name %></td>
<td><%= board.about %></td>
<td><%= board.user.full_name %></td>
<td><%= link_to 'Show', board %></td>
<td><%= link_to 'Edit', edit_board_path(board) %></td>
<td><%= link_to 'Destroy', board, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Board', new_board_path %>
****Here is the code for my index method in board controller****
def index
if user_signed_in?
#boards = current_user.boards
else
#boards = Board.paginate(page: params[:page], per_page: 1)
end
end
I have added the will_paginate to my gem file
source 'https://rubygems.org'
gem 'will_paginate'

Simple range search in Rails

I'm new to ROR and I'm trying to implement a simple search which should allow a user to search for hikes that match a certain range of difficulty. The form is properly displayed and I do not get any error messages, the problem is that no search results are dispayed.
I've been trying to solve this for a whole day now, so any help is extremely appreciated!
Index.html.erb
<%= form_tag wanderungens_path, :method => 'get' do %>
<p>
<%= number_field_tag ':search[dauer_von]', #search.dauer_von %>
<%= number_field_tag ':search[dauer_bis]', #search.dauer_bis %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Wanderungens_controller.rb
def index
#search = WanderungenSearch.new(params[:search])
#wanderungens = #search.scope
end
wanderungen_search.rb
class WanderungenSearch
attr_reader :dauer_von, :dauer_bis
def initialize(params)
params ||= {}
#dauer_von = params[:dauer_von]
#dauer_bis = params[:dauer_bis]
end
def scope
Wanderungen.where('zeitdauer BETWEEN ? AND ?', #dauer_von, #dauer_bis)
end
I followed this tutorial: https://www.youtube.com/watch?v=Ri1YNjl1_hA
I'm on Rails 4.0.0 and Ruby 2.0.0
Update: This is the code where #Wanderungens is displayed:
<table>
<thead>
<tr>
<th>Description</th>
<th>User</th>
<th>Schwierigkeitsgrad</th>
<th>Zeitdauer</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #wanderungens.each do |wanderungen| %>
<tr>
<td><%= wanderungen.description %></td>
<td><%= wanderungen.user.email if wanderungen.user %></td>
<td><%= wanderungen.schwierigkeitsgrad.description if wanderungen.schwierigkeitsgrad %></td>
<td><%= wanderungen.zeitdauer if wanderungen.zeitdauer %></td>
<td><%= link_to 'Show', wanderungen %></td>
<td><%= link_to 'Edit', edit_wanderungen_path(wanderungen) %></td>
<td><%= link_to 'Destroy', wanderungen, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
If I search from 1 to 2, the following request is generated:
http://localhost:3000/wanderungens?utf8=%E2%9C%93&%3Asearch[dauer_von]=1&%3Asearch[dauer_bis]=2

Model index.html page displaying duplicated data

I'm getting some strange behavior on my model index page. When I create one model object, it displays correctly on the index page. When I create a second model object, it shows duplicates of both objects on the index page, like so
OBJECT A
OBJECT B
OBJECT A
OBJECT B
I've confirmed that duplicate objects are not being created in my database. Also, when I destroy OBJECT B, it displays OBJECT A correctly only once.
index.html.erb
<table class="table">
<thead>
<tr>
<th>Image</th>
<th>Name</th>
<th>Description</th>
<th>URL</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<%= render #companies %>
</tbody>
</table>
_company.html.erb
<% #companies.each do |company| %>
<tr>
<td><%= image_tag company.image(:medium) %></td>
<td><%= company.name %></td>
<td><%= company.description %></td>
<td><%= company.url %></td>
<td><%= link_to 'Show', company %></td>
<td><%= link_to 'Edit', edit_company_path(company) %></td>
<td><%= link_to 'Destroy', company, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
companies_controller.rb
def index
#companies = Company.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #companies }
end
end
Change your partial to,
<tr>
<td><%= image_tag company.image(:medium) %></td>
<td><%= company.name %></td>
<td><%= company.description %></td>
<td><%= company.url %></td>
<td><%= link_to 'Show', company %></td>
<td><%= link_to 'Edit', edit_company_path(company) %></td>
<td><%= link_to 'Destroy', company, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
You need to drop the each loop in your partial.
The <%= render #companies %> renders the partial for each company but you're also looping through the companies again in each partial.
See 3.4.5 Rendering Collections at http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections for more info
Change the <%= render #companies %> to <%= render "company" %>; your partial is being rendered multiple times, one for each company, and your partial is rendering all the companies. This will only render the partial, which will render all the companies, which is what you want.

Rails: Using Ajax

Problem: Trying to implement "like" function on my blog
PostIndex:
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= link_to post.content, post %></td>
<td><%= post.created_at.strftime("%Y/%m/%d, %I:%M%p") %></td>
<td><%= post.view %></td>
<td><%= post.like %></td>
<td><%= post.hate %></td>
<td><%= link_to 'like', like_post_path(post), :remote => true %></td>
</tr>
<% end %>
PostController:
def like
#post = Post.find(params[:id])
#post.like += 1
#post.save
respond_to do |format|
format.js
end
end
app/views/posts/like.js.erb
$('#post').html("<%=j #post.like %>");
Question:
I believe I'm not correctly pointing at the post I'm looking at in like.js.erb.
In the index file, Simply doing <%= #post.view %> worked. But how do you do it in like.js.erb?
You need some identifier for post's like count in your html when you want to update it after ajax call
PostIndex:
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= link_to post.content, post %></td>
<td><%= post.created_at.strftime("%Y/%m/%d, %I:%M%p") %></td>
<td><%= post.view %></td>
<td id= "post<%= post.id %>_like"><%= post.like %></td> <!-- set id (identifier) here -->
<td><%= post.hate %></td>
<td><%= link_to 'like', like_post_path(post), :remote => true %></td>
</tr>
<% end %>
then in your app/views/posts/like.js.erb,
<% if #post.errors.blank? %>
$("#post<%= #post.id %>_like").html("<%=j #post.like %>");
<% end %>
Complete Way of Using Ajax to Fix a Table Row:
In your show.html (or index.html) file, you probably have a table already.
<tr>
<td>row 1</td>
<td>row 2</td>
<td>row 3</td>
</tr>
Let's pretend the rows represent some kind of variable. Then the table above would be like,
<tr>
<td><%= #variableA %></td>
<td><%= #variableB %></td>
<td><%= #variableC %></td>
</tr>
I want to change variableA without reloading the whole page. So I use Ajax.
My method:
def fixvar
#variableA = 2
end
You have to add one component to use Ajax.
def fixvar
#variableA = 2
end
respond_to do |format|
format.js
end
And wherever you want, you will have a link that will invoke the change via ajax.
<%= link_to 'change variable A', URLof_fixvar(#variableA), :remote => true %>
By using the option :remote => true, you're using an ajax component in your link_to method. You have to set URLof_fixvar in config/routes.rb accordingly. routing
Let's specify where to update in the table.
<tr>
<td id="variable_change_<%= #variableA.id %>><%= #variableA %></td>
<td><%= #variableB %></td>
<td><%= #variableC %></td>
</tr>
We did as above to specify the row that we're trying to update. For example, the row is now defined as variable_change_1. (assuming variableA.id is 1)
Now, you need to go to the related views folder, and create a .js.erb file that relates to the defined method above. For example, I had "like" method defined in my PostController, so I created
app/views/posts/like.js.erb
In here, you only need one line of code:
$("#variable_change_<%= #variable.id %>").html("<%= #variableA %>");
The first part before .html is pointing at the specified row, and the part after is the value that we want to update. Done!

How to return a string for rails view

Use method in helper to hide or show a link accordingly. The method new_cate? works as designed. However the method link_to_edit? causes the following error:
undefined local variable or method `cate' for #<#<Class:0x4ed6a10>:0x4ed2d38>
Extracted source (around line #23):
20: <tr>
21: <td><%= cate.name %></td>
22: <td><%= cate.description %></td>
23: <td><%= link_to_edit? %>></td>
24:
25: </tr>
26: <% end %>
Here is the code:
in index.html.erb
<body>
<h2>Category</h2>
<table>
<tr>
<th>Category</th>
<th>Description</th>
</tr>
<% #categories.each do |cate| %>
<tr>
<td><%= cate.name %></td>
<td><%= cate.description %></td>
<td><%= link_to_edit? %>></td>
</tr>
<% end %>
</table>
<%= new_cate? %>
</body>
in categories_helper.rb
def new_cate?
if session[:eng_dh]
return link_to 'New Category', new_category_path
end
end
def link_to_edit?
if session[:eng_dh]
return link_to 'Edit', edit_category_path(cate)
end
end
The code following "return" seems weird. new_cate? works as designed. But link_to_edit? does not.
Any thoughts? Thanks.
You are referring to a method scope variable (which is not yet declared in that scope).
I guess what you want is
#in controller
def link_to_edit cate
if session[:eng_dh]
return link_to 'Edit', edit_category_path(cate)
end
end
#in view
<% #categories.each do |cate| %>
<tr>
<td><%= cate.name %></td>
<td><%= cate.description %></td>
<td><%= link_to_edit cate %>></td>
</tr>
<% end %>
There is a problem in your link_to_edit function.
In your view pass the variable cate.
<td><%= link_to_edit(cate) %></td>
and in the controller
def link_to_edit(cate)
if session[:eng_dh]
return link_to 'Edit', edit_category_path(cate)
end
end

Resources