Link to Admin Dashboard in View - html

I already have Active Admin all set up and now I was trying to add a link to the dashboard in my view.
I have the current_admin_user method in my application_controller:
def current_admin_user
return nil if user_signed_in? && !current_user.admin?
current_user
end
And my view is:
<% if current_admin_user %>
<li><%= link_to "Admin", admin_path %></li>
<% end %>
However I'm getting the error:
undefined local variable or method `current_admin_user'
Anyone knows how to solve this?

If you want to use controller methods in views, you should add this line in your application_controller:
helper_method :current_admin_user

Put the function in your application_helpers.rb file in the /helpers folder instead and it should work
def current_admin_user
return nil if user_signed_in? && !current_user.admin?
current_user
end
An alternative is to use if user_signed_in? && current_user.admin? in your view
<% if user_signed_in? && current_user.admin? %>
<li><%= link_to "Admin", admins_path %></li>
<% end %>

This should work:
<% if user_signed_in? %>
<% if current_admin_user? %>
<li><%= link_to "Admin", admin_path %></li>
<% end %>
<% end %>
If it fails try:
<% if user_signed_in? %>
<% if current_user.admin? %>
<li><%= link_to "Admin", admin_path %></li>
<% end %>
<% end %>

Related

ActionView::SyntaxErrorInTemplate in AccountsController#profile

Unexpected end when end exists, But I don't see where the issue is.
My terminal keeps giving me the error: ActionView::SyntaxErrorInTemplate (Encountered a syntax error while rendering template:
my profile.html.erb
<% if #users.image.present? %>
<%= image_tag #users.image %>
<% end %>
<strong><h1><%= #users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if #user == current_user %>
<%= link_to"Edit Profile", edit_user_registration_path(#user) %>
<% if user_signed_in? && !#user? %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= #users.posts.count %> Posts </div>
<p><%= #users.full_name %></p>
<p><%= #users.description %></p>
<p><%= link_to #users.website if #users.website.present? %></p>
<%= #posts.each do |post|%>
<%= image_tag post.image %>
<% end %>
<% if user_signed_in? && #user == current_user %>
<%= link_to"Edit Profile", edit_user_registration_path(#user) %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
now it's ok :D
Seems like issue is here
<p><%= link_to #users.website if #users.website.present? %></p>
Change it to
<p><%= link_to 'User Website', #users.website if #users.website.present? %></p>
I have seen two mistakes, please correct them. first there is nothing #user? that is biggest error, another might not be problem but there is no space <%= link_to"Edit Profile" Please try following code I have changed things and reformat it properly.
<% if #users.image.present? %>
<%= image_tag #users.image %>
<% end %>
<strong><h1><%= #users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if #user == current_user %>
<%= link_to "Edit Profile", edit_user_registration_path(#user) %>
<% if user_signed_in? && !#user %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= #users.posts.count %> Posts </div>
<p><%= #users.full_name %></p>
<p><%= #users.description %></p>
<p><%= link_to #users.website if #users.website.present? %></p>
<%= #posts.each do |post|%>
<%= image_tag post.image %>
<% end %>

Reduce code redundancy

I have a rails application which has some following code
<ul class="sortable grid row">
<% #videouploads.each do |video_upload| %>
<% if video_upload.category == 'モノナビ' && video_upload.priority == 1 %>
<%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %>
<% end %>
<% end %>
</ul>
I have to write this code 10 times for video_upload.priority == 1 where priority changes from 1 to 10. How do I avoid code duplication or do I have what is the best solution ?
loop over the priorities
<% priorities = (1..10).to_a %>
<% priorities.each do |priority| %>
<ul class="sortable grid row">
<% #videouploads.each do |video_upload| %>
<% if video_upload.category == 'モノナビ' && video_upload.priority == priority %>
<%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %>
<% end %>
<% end %>
</ul>
<% end %>
<% (1..10).each do |inc| %>
<ul class="sortable grid row">
<% #videouploads.each do |video_upload| %>
<% if video_upload.category == 'モノナビ' && video_upload.priority == inc %>
<%= render partial: 'adminrow', :locals => {:video_upload => video_upload } %>
<% end %>
<% end %>
</ul>
<% end %>
How about this ?

Separating posts in a newsfeed using CSS/BootStrap

I'm currently trying to render a newsfeed, similar to that of FB on a Rails application I'm working on. Unfortunately, I'm not the greatest when it comes to CSS and I'm having some issues trying to display different posts. This issue occurs whether I'm using BootStrap or plain CSS. I do believe it's something to do with the loop that is created by <% #posts.each do |post| %> Currently, whenever a new post is made, it wraps inside the previous post; thus the more posts that are made, the thicker the border gets.
Image:
<% if #posts.any? %>
<% #posts.each do |post| %>
<div class="well">
<%= post.user.first_name %> <%= post.user.last_name %><br>
<% if !post.image.exists? %>
<h2> <%= post.text %> </h2>
<% else %>
<h2> <%= link_to post.text, post_path(post) %> </h2>
<%= link_to post_path(post) do %>
<p><%= image_tag post.image.url(:medium) %></p>
<% end %>
<% end %>
<% if #user %>
<% if current_user.voted_up_on?(post) %>
<%= link_to "Like", dislike_post_path(post), method: :put %>
<% else %>
<%= link_to "Like", like_post_path(post), method: :put %>
<% end %>
<% end %>
<%= "Likes: #{post.get_upvotes.size}" %>
<% if post.user == current_user %>
<%= link_to "Edit", edit_post_path(post) %>
<%= link_to "Delete", post_path(post), method: :delete %>
<% end %>
<div id='comments_div' class="comments-index">
<%= render post.comments %>
</div>
<% if current_user %>
<%= form_for [post, post.comments.new ], remote: true do |f| %>
<%= f.text_area :text, placeholder: 'Add a comment' %>
<%= f.submit 'Comment' %>
<% end %>
<% else%>
<p>You need to <%= link_to "sign in", new_user_session_path %> to comment</p>
<% end %>
<% end %>
<% else %>
No posts have been added!
<% end %>
</div>
Any help would be greatly appreciated! Thanks.
Edit: OK, please take a look at the new image -- hopefully that will make the issue slightly more obvious. Additionally, I've removed all the dead tags and replaced them with just one: BootStrap's 'well' class. So, there you have it. All the information you need is within the code above.
from your description it sounds as though an html element is not being properly closed. Run the page source through an html validator and that could show you the problem.
If you don't want to take a structured problem solving approach, try adding another </div> to the end of your posts-index container.
Your issue is very simple, just that its not clear due to poor indendation.
A simple way to explain what you did is:
<-- if (start) -->
<-- do (start) -->
<-- post (start) -->
(post is not ending here, hence it breaks the layout)
<-- do (end) -->
<-- if (end) -->
<-- post (end) -->
Mistake in the above should be simple to understand so if you move your last </div>(of the well class) just before the second last <% end %>(of the <% #posts.each do |post| %> loop) it should fix the issue. So the last few lines should be
<% else%>
<p>You need to <%= link_to "sign in", new_user_session_path %> to comment</p>
<% end %>
</div>
<% end %>
<% else %>
No posts have been added!
<% end %>
Sounds to me like it could be a misplaced
<% end %>
or a missing
</div>
that is causing this behavior.
Proper indentation will point to where to close off actions or divs

IF/ELSE Conditions on Ruby hash

I want to set up a Ruby partial that takes a 'type' then, depending on that type, spits out a <li> with defined elements (such as an icon, a particular label, etc)
Here's my working .erb chunk
<ul>
<% if locals.has_key? :sermon_links %>
<% sermon_links.each do |link| %>
<% if locals.has_key? :type == "download" %>
<li>
<i class="material-icons">music_note</i>
Download this sermon (~5mb)
</li>
<% else %>
<% if locals.has_key? :type == "passage" %>
<li>
<i class="material-icons">link</i>
<%= sermon_passage %> on Bible Gateway
</li>
<% end %>
<% end %>
<% end %>
</ul>
And then I'd call it in the HTML file like this:
:sermon_links => [
{ :type => "download", :hyperlink => "https://www.biblegateway.com/resources/matthew-henry/John" }
]
I'm 99% sure the problem is how I'm setting the 'IF type equals THIS' (<% if locals.has_key? :type == "download" %>) I'm a newbie to Ruby on Rails so any help in this area would be really appreciated :) thanks!
I think you can rewrite as follows:
<ul>
<% if locals.has_key? :sermon_links %>
<% sermon_links.each do |link| %>
<% if link[:type].present? && link[:type] == "download" %>
<li>
<i class="material-icons">music_note</i>
Download this sermon (~5mb)
</li>
<% else %>
<% if link[:type].present? && link[:type] == "passage" %>
<li>
<i class="material-icons">link</i>
<%= sermon_passage %> on Bible Gateway
</li>
<% end %>
<% end %>
<% end %>
<% end %>
</ul>

Undefined method "errors"... But there are ERRORS

I am doing a basic validation for a bank entry and am trying to display the errors on submit. Here is my validation and html.
controller:
class PagesController < ApplicationController
def index
#guestbook_entries = GuestbookEntry.all
#guestbook_entry = GuestbookEntry.new
render "welcome"
end
end
class GuestbookEntry < ActiveRecord::Base
validates :body, presence: :true
end
This is my html:
<% form_for #guestbook_entry do |f| %>
<% if f.errors.any? %>
<% f.errors.full_messages.each do |m| %>
<li><%= m %></li>
<% end %>
<% end %>
<%= f.label :body, "Guestbook Entry:" %>
<%= f.text_area :body %>
<%= f.submit "Submit" %>
<% end %>
Any ideas?