I have followed the Devise Wiki to create a very basic admin setup by adding a admin column to my User table in a boolean format.
I have been into my table (through SQlite administrator) and assigned one of my users to be an admin.
I am then have the following code in my view:
<% if user_signed_in? %>
<% if current_user.admin? %>
<%= link_to "Admin Job Post", new_user_job_path(current_user.id) %>
<% else %>
<%= link_to "Post a new job", new_user_job_path(current_user.id) %>
<% end %>
<% else %>
<%= link_to "Post a new job", new_user_session_path %>
<% end %>
The issue I am having is that my app is only ever returning my <%= link_to "Post a new job", new_user_job_path(current_user.id) %> even when logged in with an admin user.
It would be great to get a solution on this because I have tried several variations and can't get it to work.
Thanks in advance for your help!
I'm not sure of the entirety of your code, but I have a similar setup, but I used the following to show a link once a user is logged in as an Admin (I used 'try' due to the fact that it is outside of a 'user_logged_in' check).
<% if current_user.try(:admin?) %>
<li>AdminLink</li>
<% end %>
Are you sure that you made the user an admin?
To make the user admin you should have run the following commands
User.find(#id_of_user_you_want_to_make_admin)
User.admin = true
User.save!
<% if current_user.present? && current_user.has_role?(:admin) %>
<li><%= link_to 'Some Cool Admin Feature', cool_admin_path %></li>
This is what you're looking for if you're using Rails 4 with Devise 2.x. Note the () around :admin aren't necessary, I included them for clarity here as the '?' after the method seems to throw people that are new to ruby.
Another way to give admin privileges is to simply use <% if current_user.try (:email) == "admin#example.com" %> That always works for me. That way you don't have to worry about the whole system.
Related
The problem is, In the menu of my app I want to check if the current user has a book. If they do I will show a link to the edit book path, if not, I will show a link to the create book path.
<% if current_user.book? %>
<% else %>
<% end %>
Yes, you should be able to access current_user from any controller. But always make sure you handle if current_user returns nil.
You can use a try or safe navigation.
<% if current_user.try(:book?) %>
<% else %>
<% end %>
Or
<% if current_user&.book? %>
<% else %>
<% end %>
I'm trying out Rails 5 and have come across something weird. Everywhere that I've googled says that my code is right. Not sure what I'm doing wrong.
My view code:
<% provide(:title, "View all Users") %>
<% #users = User.all %>
<ul>
<% #users.each do |user| %>
<% #user = user %>
<li><%= link_to user.name, users_path(#user) %><%= user.name %></li>
<% end %>
</ul>
Spits out html:
boop boop
Notice that the path is using a dot instead of a forward slash.
Anyone have any idea why?
I believe the issue is the users_path(#user). You have an extra s there, try user_path(#user) or even better try #user
I would write that line as <li><%= link_to user.name, #user %><%= user.name %></li>
Rails is smart enough to understand that linking an object means you want to go to the show page for the object
how can I issue a create command from a link? I have an if/else statement that displays edit or create, but I haven't found the right way to create the record.
I had this, but then I have to refresh the page to get it to function, which I can't have:
<% if FollowUp3Week.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", follow_up3_week_path([FollowUp3Week.where(subject_id: sub.subject_id).first]) %>
<% else %>
<%= FollowUp3Week.create(subject_id: sub.subject_id) %>
<% end %>
And I'm trying this, but no luck so far (where subjects_path is a link to the current page):
<% if Baseline.where(subject_id: sub.subject_id).first != nil %>
<%= link_to "edit", baseline_path([Baseline.where(subject_id: sub.subject_id).first]) %>
<% else %>
<%= link_to "create", subjects_path(Baseline.create(subject_id: sub.subject_id)) %>
<% end %>
Any tips, or references I should read through, would be greatly appreciated.
I really just want the link 'create' to generate the working 'edit' link.
Thank you for your time.
You will only be able to create a record when you call a controller, so what you want to do in your case is make the link_to just render the url to the controller that have the create action, once the user clicks the link that will trigger a request to your controller which will run the create record.
I have a nav bar that appears on every page of my website. There is a link in the navbar like this:
<%= link_to 'Publish' , new_user_comic_title_path(user_id: current_user.id) %>
I am using the DEVISE gem. Each user has many Comic_titles, which is why I have the new_user_comic_title_path. The problem is, when the user is signed out, current_user.id = nil. The error is below:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
I would like (1) the page to render without it throwing this error and (2) if the link is clicked and the user is not signed it, it redirects to the sign in page.
Really appreciate the help!
<% if signed_in? %>
<%= link_to 'Publish' , new_user_comic_title_path(user_id: current_user.id) %>
<% else %>
<%= link_to 'Publish' , sign_in_path %>
<% end %>
or
<%= link_to 'Publish' , signed_in? ? new_user_comic_title_path(user_id: current_user.id) : sign_in_path %>
If you are using devise gem then
<% if user_signed_in? %>
<%= link_to 'Publish' , new_user_comic_title_path(user_id: current_user.id) %>
<% else %>
<%= link_to 'Publish' , new_session_path(:user) %>
<% end %>
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.