I am trying to have a photo upload section in my room listing. when I try to click on the photos I get this error
undefined method 'count' for nil:NilClass rails <% if #photos.count > 0 %> .
I have added a photo_upload.html.erb page and a _room_menu partial but still I get the error.
here's my code:
photos_controller.rb
class PhotosController < ApplicationController
def create
#room = Room.find(params[:room_id])
if params[:images]
params[:images].each do |img|
#room.photos.create(image:img)
end
#photos = #room.photos
redirect_back(fallback_location:request.referer, notice: "Saved...")
end
end
end
**views/rooms/photo_upload.html.erb**
<div class="row">
<div class="col-md-3">
<%= render 'room_menu' %>
</div>
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">
Photos
</div>
<div class="panel-body">
<div class="container">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<!-- PHOTOS UPLOAD GOES HERE -->
<%= form_for #room, url: room_photos_path(#room), method: 'post', html: {multipart: true} do |f| %>
<div class="row">
<div class="form-group">
<span class="btn btn-default btn-file text-babu">
<i class="fa fa-cloud-upload" aria-hidden="true"></i> Select Photos
<%= file_field_tag "images[]", type: :file, multiple: true %>
</span>
</div>
</div>
<div class="text-center">
<%= f.submit "Add Photos", class: "btn btn-form" %>
</div>
<% end %>
</div>
</div>
<div id="photos"><%= render 'photos/photos_list' %></div>
</div>
</div>
</div>
</div>
</div>
views/rooms/_room_menu.html.erb
<ul class="sidebar-list">
<li class="sidebar-item">
<%= link_to "Listing", listing_room_path, class: "sidebar-link active" %>
<span class="pull-right text-babu"><i class="fa fa-check"></i></span>
</li>
<li class="sidebar-item">
<%= link_to "Pricing", pricing_room_path, class: "sidebar-link active" %>
<% if !#room.price.blank? %>
<span class="pull-right text-babu"><i class="fa fa-check"></i></span>
<% end %>
</li>
<li class="sidebar-item">
<%= link_to "Description", description_room_path, class: "sidebar-link active" %>
<% if !#room.listing_nam.blank? %>
<span class="pull-right text-babu"><i class="fa fa-check"></i></span>
<% end %>
</li>
<li class="sidebar-item">
<%= link_to "Photos", photo_upload_room_path, class: "sidebar-link active" %>
<% if !#room.photos.blank? %>
<span id="photo_check" class="pull-right text-babu"><i class="fa fa-check"></i></span>
<% end %>
</li>
<li class="sidebar-item">
<%= link_to "Amenities", amenities_room_path, class: "sidebar-link active" %>
<span class="pull-right text-babu"><i class="fa fa-check"></i></span>
</li>
<li class="sidebar-item">
<%= link_to "Location", location_room_path, class: "sidebar-link active" %>
<% if !#room.address.blank? %>
<span class="pull-right text-babu"><i class="fa fa-check"></i></span>
<% end %>
</li>
</ul>
<hr/>
You can't call .count on a nil. #photos must be instantiated first. You controller doesn't appear to instantiate #photos at all. I can't see where else in your code you're calling #photos but where it's getting called, the instance variable has not yet be defined. Your controller create method only shows it happens if params[:images] is present, if otherwise it will be nil.
Try to instantiate #photos outside the if block.
def create
#room = Room.find(params[:room_id])
#photos = #room.photos
if params[:images]
params[:images].each do |img|
#room.photos.create(image: img)
end
redirect_back(fallback_location:request.referer, notice: "Saved...")
end
end
UPDATE: Ruby 2.3+ you can use safe navigation with & :
def create
#room = Room.find(params[:room_id])
params[:images]&.each{|img| #room.photos.create(image: img)}
redirect_back(fallback_location: request.referer, notice: "Saved...")
end
Or why bother using separate #photos in view when you should be able to just call #room.photos in it's place.
Instead of calling .count as your conditional in the view, use #room.photos.present?
One more suggestion is to prefer to use positive if case. so change
if !#room.photos.blank?
# better to use this below
if #room.photos.present?
Also, one would assume before a create action, you need a new action in your controller where one would expect #photos to be defined if it really needs be isolated from #room.photos. This is standard MVC in rails, but not sure if you've posted all of your code correctly so I'm guessing here.
The people above have already given great explanations as to why you can't call a method on a nil class. In addition, maybe the following code will help in your situation:
<% if !#photos.nil? && #photos.count > 0 %>
<% if #photos.count > 0 %>
If this fails with an error 'undefined method for nil class', then it means #photos is nil, and you therefore can't perform any methods on it. Where are you calling it? You've not included it in your code.
When you click on the link, look at your server logs and it will tell you which controller action you are hitting. Whichever action you're hitting, is where you'll need to define #photos.
If it is photos#create (the controller action you listed above) then it means that #room.photos is nil. That's unlikely, as it would almost certainly return an empty active record relation if anything, so your problem is not defining #photos in the controller and action you're using at that time.
Related
I have been working to follow the best practices in Rails in order to save time in the future.
I am displaying all terms in a view which has more than 0 results.
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end %>
What is the best way to display a different view if the term count is zero? I could do something like a simple if statement in the view doing a count but that seems cluttered. Any suggestions?
How about this:
<% #terms.each do |t| %>
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{t.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, t) %></span>
</div>
</li>
<% end.empty? and begin %>
<li class="media">
<!-- nothing to see -->
</li>
<% end %>
You can also move the code to a partial.
<%= render(:partial => 'terms', :collection => #terms) || 'There are no terms' %>
The partial:
<li class="media">
<div class="media-body">
<div class="media-heading text-semibold"><%= link_to "#{term.name}", authenticated_root_path %></div>
<span class="text-muted"><%= link_to 'Settings', edit_account_term_path(#account, term) %></span>
</div>
</li>
</li>
I have a rails 4 app using pundit gem for authorization. If I do russian-doll fragment caching like the code below, the conditional statement used for authorization will be also cached, which is not good, since edit/delete buttons should only be available for the post.user.
What is the good way to get around this? Should I split the cache into smaller parts or is there a way to exclude some parts of the caching? What's the rails convention in this case?
index.html.erb
<% cache ["posts-index", #posts.map(&:id), #posts.map(&:updated_at).max, #posts.map {|post| post.user.profile.updated_at}.max] do %>
<%= render #posts %>
<% end %>
_post.html.erb
<% cache ['post', post, post.user.profile ] do %>
<div class="row>
<div class="col-md-2">
<%= link_to user_path(post.user) do %>
<%= image_tag post.user.avatar.url(:base_thumb), class: 'post-avatar' %>
<% end %>
</div>
<div class="col-md-8">
<span class="post-user-name"><%= post.user.full_name %></span>
<span class="post-updated"><%= local_time_ago(post.updated_at) %></span>
<div class="post-body">
<%= post.body %>
</div>
<div class="col-md-2" style="text-align:right;">
<!--############### THIS IS THE PART THAT SHOULD NOT BE CACHED #############-->
<% if policy(post).edit? && policy(post).delete? %>
<li class="dropdown">
<ul class = "dropdown-menu dropdown-menu-right">
<li>
<%= link_to "Edit Post", edit_post_path(post), remote: true, type: "button", 'data-toggle' => "modal", 'data-target' => "#updatepost_#{post.id}" %>
</li>
<li>
Delete Post
</li>
</ul>
</li>
<% end %>
<!--########################## UNTIL HERE ############################-->
</div>
</div>
<div class = "row comment-top-row" style="padding-bottom:10px;">
<div class="col-md-12 post-comment-form">
<%= render partial: 'posts/post_comments/post_comment_form', locals: { post: post } %>
</div>
</div>
<div class = "row">
<div class="col-md-12 post-comment-insert-<%= post.id%>">
<%= render partial: 'posts/post_comments/post_comment', collection: post.post_comments.ordered.included, as: :post_comment, locals: {post: post} %>
</div>
</div>
<% if policy(post).edit? %>
<div class="modal fade updatepost" id="updatepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<!-- FORM GETS RENDERED HERE VIA JS -->
</div>
<% end %>
<% if policy(post).delete? %>
<div class="modal fade" id="deletepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
......
</div>
<% end %>
<% end %>
Russian Doll Caching is simple but handy way for caching, there's no complex options or convention to exclude part of fragment from it. Above that, It's more relative to cache strategies. Here are two strategies for this user specific situation:
Rearrange individually and Cache fragments manually, which I don't recommend. Because it's more complex and doesn't leverage the advantages of Russian Doll Caching. Not so maintainable as well. Here is an example:
index.html.erb
<% # pull out cache %>
<%= render #posts %>
_post.html.erb
<% cache post %>
<%= # first part %>
<% end %>
<% # without cache %>
<%= # user specific part %>
<% cache post %>
<%= # third part %>
<% end %>
Preferred way: Add current_user as part of cache_key, which means you will have as many fragment caches as approximately your users and the fragments will automatically invalidate whenever the post or the user has changed their fingerprint. This is more elegant and maintainable. Here is an example:
index.html.erb
<% cache ["posts-index", #posts.map(&:id), #posts.map(&:updated_at).max, #posts.map {|post| post.user.profile.updated_at}.max] do %>
<%= render #posts %>
<% end %>
_post.html.erb
<% cache ['post', post, post.user.profile, current_user ] do %>
<div class="row>
<div class="col-md-2">
<%= link_to user_path(post.user) do %>
<%= image_tag post.user.avatar.url(:base_thumb), class: 'post-avatar' %>
<% end %>
</div>
<div class="col-md-8">
<span class="post-user-name"><%= post.user.full_name %></span>
<span class="post-updated"><%= local_time_ago(post.updated_at) %></span>
<div class="post-body">
<%= post.body %>
</div>
<div class="col-md-2" style="text-align:right;">
<% if policy(post).edit? && policy(post).delete? %>
<li class="dropdown">
<ul class = "dropdown-menu dropdown-menu-right">
<li>
<%= link_to "Edit Post", edit_post_path(post), remote: true, type: "button", 'data-toggle' => "modal", 'data-target' => "#updatepost_#{post.id}" %>
</li>
<li>
Delete Post
</li>
</ul>
</li>
<% end %>
</div>
</div>
<div class = "row comment-top-row" style="padding-bottom:10px;">
<div class="col-md-12 post-comment-form">
<%= render partial: 'posts/post_comments/post_comment_form', locals: { post: post } %>
</div>
</div>
<div class = "row">
<div class="col-md-12 post-comment-insert-<%= post.id%>">
<%= render partial: 'posts/post_comments/post_comment', collection: post.post_comments.ordered.included, as: :post_comment, locals: {post: post} %>
</div>
</div>
<% if policy(post).edit? %>
<div class="modal fade updatepost" id="updatepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<!-- FORM GETS RENDERED HERE VIA JS -->
</div>
<% end %>
<% if policy(post).delete? %>
<div class="modal fade" id="deletepost_<%= post.id %>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
......
</div>
<% end %>
<% end %>
I have a footer in my rails application that shows all recent posts and categories produced by two instances variables in the welcome_controller.rb
But then if i go to another section that has a different controller responsible i get an error as the #posts and #categories instance variables are not there to loop through and list the latest posts and categories.
welcome_controller.rb (code extract)
#categories = Category.all
#posts = Post.order("created_at DESC")
_footer.html
<div class="row footer-black">
<div class="large-3 columns text-left">
<h4>Categories</h4>
<ul>
<% #categories.each do |c| %>
<% unless c.header? %>
<% if c.restriction %>
<% if check_for_free_questions_in_restricted_category(c) %>
<li> <%= link_to c.title, category_random_free_question_path(c),'data-no-turbolink' => true %> </li>
<% else %>
<li> <%= link_to c.title, new_subscription_path,'data-no-turbolink' => true %> </li>
<% end %>
<% else %>
<li> <%= link_to c.title, category_random_question_path(c),'data-no-turbolink' => true %> </li>
<% end %>
<% end %>
<% end %>
</ul>
</div>
<div class="large-5 columns text-left">
<h4>Latest Posts</h4>
<ul>
<% #posts.each do |p| %>
<li> <%= link_to truncate(p.title, length:70), blog_post_path(p) %> </li>
<% end %>
</ul>
</div>
<div class="large-4 columns text-left social-links">
<h4>Social</h4>
<a href="#">
<i class="fa fa-facebook-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-twitter-square fa-2x"></i>
</a>
<a href="#">
<i class="fa fa-google-plus-square fa-2x"></i>
</a>
</div>
</div>
is there a way to show categories and posts in footer without instantiating same variables over and over again?
The short answer is 'no'. Every variable must be instantiated (initialized) before it can be used (in this case, in your view).
Now, there are some more efficient ways of instantiating these variables than writing:
#categories = Category.all
#posts = Post.order("created_at DESC")
in multiple controllers. But, that's a different question. If you're interested in that, then ask a new question or refine this one.
I am a newbie in ruby on rails it's also my first ruby application.
I want to filter results by clicking on checkbox which is coming from
the database and showing on the right side of my webpage, the checkbox
is lying on the left side. In the below I attached my webpage's
screenshot for easy understanding.
Anyone can help me, please, how can I solve this issue?
My Controller file code:resumes_controller.rb
class ResumesController < ApplicationController
before_filter :recruiter!
def resumes
#resume = Jobseeker.paginate(page: params[:page], :per_page => 10).order('jobseeker_id DESC')
end
end
My view file code: resumes.html.erb
<%= form_for :jobseekers,url: resumes_path(#jobseekers), action: :update, method: :post do |f| %>
<div>
<label for="Title">Education</label>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Masters
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Bachelor
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Associates
</div>
<div class="checkbox">
<%= check_box("tag", {checked: true}) %>Diploma
</div>
More
</div>
<% end %>
<!-- item1 -->
<% #resume.each do | res| %>
<div class="job-item bio-card">
<div class="employer-information">
<div class="col-sm-6">
<ul class="list-unstyled">
<li class="employ-name">
<a href="<%= view_profile_path(:jobseeker_id => res.jobseeker_id) %>">
<%= res.first_name %> <%= res.last_name %></a> <span>- <%= res.current_address %></span>
</li>
<li><%= res.institute_name %></li>
</ul>
</div>
<% if res.attach_resume.nil? %>
<div class="col-sm-6 employ-pdf-box">
<div class="employ-pdf">
<i class="fa fa-file-powerpoint-o icon"></i>
<h3>Empty</h3>
</div>
</div>
<% else %>
<div class="col-sm-6 employ-pdf-box">
<a href="<%= res.attach_resume.resume %>"target="_blank">
<div class="employ-pdf">
<i class="fa fa-file-powerpoint-o icon"></i>
<h3><%= res.first_name %> <%= res.last_name %>.pdf</h3>
</div>
</a>
</div>
<% end %>
</div>
</div>
<% end %>
<!-- End item1 -->
I am on Rails 4. I have two resources: subarticles and articles. Subarticles are nested in articles like so:
resources :articles do
resources :subarticles do
member do
put "like" => "subarticles#upvote"
put "dislike" => "subarticles#downvote"
end
end
end
On the articles show page, I display all of the subarticles. However, I would like to place some of the article attributes ( specifically pictures and videos) directly under the first subarticle.
I am having trouble finding any documentation on doing so.
Also note that I have subarticles ordered by number of votes. So if one gets voted higher than the other, it will go to the top. But I want to keep the same pictures and videos right under the first subarticle.
Here is a simplified version of my Article show page:
<div class="row">
<div class="col-md-9">
<div class="well" style="background:white; height:230px">
<%= #article.title.titleize %><br>
<%= social_share_button_tag %>
</div>
<% #subarticles.each do |subarticle| %>
<div class="well" style="background:white">
<%= subarticle.rating %><br>
<%= subarticle.whyrating %><br>
<div class="row" style="margin-top:40px;">
<div class="col-md-7 col-md-offset-1" style="margin-top:30px">
<%= link_to like_article_subarticle_path(#article, subarticle), class: "like", method: :put do %>
<button type="button" class="btn btn-success btn-lg" aria-label="Left Align" style="margin-right:5px">
<span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span> Helpful
</button><span class="badge" style="margin-right:10px"><%= subarticle.get_upvotes.size %></span>
<% end %>
<%= link_to dislike_article_subarticle_path(#article, subarticle), class: "like", method: :put do %>
<button type="button" class="btn btn-danger btn-lg" aria-label="Left Align" style="margin-right:5px">
<span class="glyphicon glyphicon-thumbs-down" aria-hidden="true"></span> Unhelpful
</button><span class="badge"><%= subarticle.get_downvotes.size %></span>
<% end %>
</div>
</div>
</div>
<% end %>
<div class="well" style="background:white; text-align:center">
<h4>Pictures & Videos</h4><br>
<%= image_tag #article.image1(:medium) %><br>
<div class="embed-responsive embed-responsive-4by3">
<iframe class="embed-responsive-item" src="<%= #article.video1 %>"></iframe>
</div>
</div>
</div>
</div>
Because of the #subarticles.each do it will push pictures and videos all the way to the bottom. What is the best way to show just the first subarticle, then pictures & videos, then the rest of the subarticles?
Let me know if I need to show any other code.
<% #subarticles.each_with_index do |subarticle, i| %>
<div class="well" style="background:white">
....... // subarticle contents
</div>
<% if i==0 %>
// put articles content here
<% end %>
<% end %>
In that way after the first subarticle the if condition checks the loop index, as the loop index is currently 0, then it will first show the subarticle first content, then for index 0 it shows the article contents.
You can always use a flag for that:
<% first_one = false %>
<% #subarticles.each do |subarticle| %>
<div class="well" style="background:white">
...
</div>
<% unless first_one %>
// article contents here....
<% first_one = true %>
<% end %>
<% end %>