Admin approve Paperclip images before display - ruby-on-rails

Rails: How can I add admin approve functionality where user uploaded images have to be approved before they are displayed on their profiles?

How about add a column to the user's avatar that says admin_approved or something similar? Since you're using ActiveAdmin, it should pick up the column (unless you've specified otherwise). You then add a check before displaying the image:
<%= user_images.each do |image| %>
<% if image.admin_approved %>
<%= image_tag image.url %>
<% end %>
<% end %>

Related

How do I add an image placeholder to fix this bug

I am a beginner programmer. I'm building my first project which shows a list of my favorite athletes and when clicked the "show" page loads an image of them, their name, and weight.
Overall when adding a new athlete everything works fine. If I decide to add a new athlete and skip adding an image I get this error "Can't resolve image into URL: undefined method `persisted?' for nil:NilClass", it also highlights my code "<%= image_tag(#fighter.image) %>" that's in my show.html.erb
I've tried adding an alt: but I get another error of "wrong number of arguments (given 1, expected 0)".
I've searched for answers and one thread mentioned my controller page and the params section. I did notice that (:image) is preceded by "params.require" which I would assume could be the problem if I'm not adding an element that is required. How do I solve this issue? What would be the best way to add a placeholder, I've added a default.png in my images folder.
Thank you
show.html.erb
fighters_controller.rb
If you're using ActiveStorage for uploading athlete images. You can check if an image was attached to your model with #fighter.image.attached?.
So I would suggest wrapping the image_tag in an IF-clause like so:
<% if #fighter.image.attached? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
If you're using Paperclip then try the following:
<% if #fighter.image.exists? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>
With Carrierwave use:
<% if #fighter.image.present? %>
<%= image_tag(#fighter.image) %>
<% else %>
<%= image_tag("default.png") %>
<% end %>

How to access specific object fields?

I am building a basic bare bones social media app right now.
I have a user class and a status class.
For each status, there is a "creater" (a user object) and a "subject" (a user object that the status is about). I was able to create tags by using the acts_as_taggable_on gem. What ends up happening is when a user goes to create a post, he/she can select another user from a dropdown menu. The chosen user's id attribute is then stored.
Now I am trying to link to the chosen User's profile. This is my code for show statuses on a profile page.
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<br></br>
#link to user who's associated with the tagId
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end%>
this is the line where the above code breaks
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
Can anyone help me out with this?
Not surprised this line is failing:
<%= link_to User.find(status.tag_list).profile_name, user_profile_path(User.find(status.tag_list).profile_name) %>
A couple points:
It's a little cleaner to separate it onto multiple lines
I suspect your problem is because you're passing a profile_name to user_profile_path instead of an id, though I can't be certain without seeing your routes.
Try the following:
<% profile_user = User.find(status.tag_list) %>
<%= link_to profile_user.profile_name, user_profile_path(profile_user.id) %>

Creating a formula in a Rails view?

I would like to create a small formula in my view/home page. Basically it should say if current user signed in (got that part right) AND USER CREATED A REGISTRY = TRUE then this
or that.
Here is the code below:
<% if user_signed_in? and?????? %>
<%= link_to "Show My Registry", current_user.registry %>
<% else %>
<%= link_to "Create a new registry", new_registry_path %>
<% end %>
Thanks for any help.
Assuming Registry is a model that belongs to a User, you could use <% if user_signed_in? && current_user.registry.exists? %>
I assume you all ready have the relationship between Users and Registries established. If so your if so you could create a method in your users controller that looks something like.
def user_registry
User.current.registry
end
Then if your view your if statement should look something like
<% if user_signed_in? and user_registry? %>
<%= link_to "Show My Registry", current_user.registry %>
<% else %>
<%= link_to "Create a new registry", new_registry_path %>
<% end %>
You might want to nest the two decisions. What happens if the user is not signed in? Do you still want the user be able to create a registry without signing in? Other option would be to consider using a before_filter to ensure user is always signed in before she gets to this decision point in your app.

Ruby on Rails: linking to a user-uploaded image in the layout

The following is code to display each of a user's messages, along with the photo associated with each message. The code is located in a partial view from the home_controller.
<% msgs.each do |msg| %>
<%- if msg.photo? -%>
<%= image_tag msg.photo.url(:listsize) %>
<%- end -%>
<%- end -%>
I can display the photo related to a particular message like this. The photo is uploaded by the user via paperclip in rails. The photo is stored in a column on the msg table in the database.
How can I display the latest photo uploaded by the user in the layout itself?
i.e something like image_tag current_user.lastest.msg.photo.url(:listsize) But again it needs to display in the layout, not in the view.
You can, in fact, use:
image_tag current_user.lastest.msg.photo.url(:listsize)
in your layout. Obviously, you'll want to be careful with whether a user is or is not logged in. Some code akin to:
<%= image_tag current_user.lastest.msg.photo.url(:listsize) if current_user.present? %>
will work wonders. Is there any other reason you're afraid to put this in the layout?

How do I get multiple paperclip uploads to display in the view?

I'm very new to programming and have followed this screencast at emersonlackey.com on adding multiple paperclip uploads for a record. It all works great but I can't figure out how to display the uploaded images in the records show page.
They display fine on the edit page using:
<%= f.fields_for :venuephotos do |photo| %>
<% unless photo.object.new_record? %>
<p>
<%= link_to image_tag(photo.object.venuephoto.url(:thumb)), photo.object.venuephoto.url(:original) %>
<%= photo.check_box :_destroy %>
</p>
<% end %>
<% end %>
I've used paperclip in other models but they are the standard each record only has one upload so the images display fine with <%= #model.photo.url %> type call but I cant figure out this other way.
Any help is much apprectiated!
You want to do something like this:
<% for asset in #post.assets %>
<%= link_to image_tag(asset.asset.url(:thumb)), asset.asset.url(:original) %>
<% end %>
as per the code that goes along with the screencast.
https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb

Resources