Ruby on Rails: Conditional Links using presence_in - ruby-on-rails

I have two models, User and Profile, with user_id used as a foreign key to link them. I'd like to put a conditional statement in my footer that looks to see if the current user has a profile. If they do they will see a link to the edit page and, if they don't, to the create/new page.
I tried finding a solution online and I think using the presence_in?(object) method might work but, as a newbie, I don't quite get the syntax.
This is what I have so far if someone can help me get to the finish line :)
<% if current_user.id (something something) %>
<li><%= link_to "Edit Profile", edit_profile_path(:id => current_user) %></li>
<% else %>
<li><%= link_to "New Profile", new_profile_path %></li>
<% end %>
If my question is unclear please let me know and I'll provide a link to my Github page

You can simply do <% if current_user.profile.present? %> to check whether user's profile exists or not. You need have has_one association in User model to get this working e.g has_one :profile

Related

ActionController::InvalidAuthenticityToken error in rails

I wanted to add some custom fields to devise authentication, so I followed a tutorial and did some changes. I unpacked the devise gem, added the fields to the views, and devise controller. I added the fields "first_name" and "last_name" to my users table. The changes didn't work. So I didn't want to spend much time on that, so I wanted to move on, I removed all the code I added to the devise gem sourcecode, created a migration to remove first_name, and last_name from users. Everything looked fine, I can move about on the site and everything. But as soon as I try to sign out, I get this error:
In the command line, the error also says "can't verify CSRF token auhtenticity"
This is the code I have in my layout view navbar for the user to sign out:
<% if user_signed_in? %>
<li><%= link_to "Sign out", destroy_user_session_path, method: :delete %></li>
<%else%>
<li>
<%= link_to "sign up" , new_user_registration_path %>
</li>
<li>
<%= link_to "Log in", new_user_session_path%>
</li>
<%end%>
I restarted the server and all that. The user is still signed in. I can't do anything to sign out. Is there a way to fix this?
To destroy a user session in devise, you have to do the following:
<%= link_to "Logout", destroy_user_session_path, :method => :delete %>
the hash ':method' will trigger delete action and sign the user out and destroy current session. Make sure your pointing to your "destroy_user_session_path", or the path you specified. You should be able to see what the name of the path is by using "rails routes" or "rake routes" command depending on what version of rails you're using.
Hope this helps.

Combining Attributes for a Conditional

<% #user.chores.each do |chore| %>
<li><%= chore.name %></li>
<% end %>
<% #user.chorelists.each do |chorelist| %>
<li><%= chorelist.day %></li>
<% end %>
Hola, friends.
My app has three models: User, Chore, and Chorelist (which is a 'joined' resource for User and Chore).
Both of these blocks work, but how can I write, say, a conditional statement that will give me all of that user's chores for a specified day? (The days are saved in the Chorelist model as strings.)
If you need anymore more code from the app, just let me know. And thank you!
It'd be great to know what fields do your models have. Meanwhile, I think that you want something like this:
date = Date.today
#user.chores.joins(:chorelist)
.references(:chorelist)
.where(chorelists: {day: date})
And this should be in your controller, yeah, per commenter's suggestion.

Rails 4 Generating application wide dynamic nav-bar links

I am trying to generate links in my nav-bar based on records.
I want to post a link in the patrols section of my app that correspond to a patrol route that an admin generated?
I have tried to place
#patrol_routes = PatrolRoute.all in the application controller
and then i want something like
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path %> so that it takes me to the show page of the patrol route i want to access?
<% end %>
Is this possible? i have tried to google and research it, but I'm not finding anything, perhaps I'm not hitting the correct key words?
Assuming you have a route (as in config/routes.rb) named patrol_route you should do:
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path(patrol_route) %>
<% end %>

Dynamically adding pages to rails app

This may be a more fundamental aspect of Rails but I am very new.
I have a basic app which is sort of a basic craigslist clone.
Users - I used devise for this, users can sign up/sign in/sign out/edit their profile.
Listings - Users can add multiple listings. Currently I have a few basic fields populating the database (title, content, phonenumber, price, location).
What I want to be able to do is the following:
I want to have a page which lists all of the ads. Currently I can do this by accessing the database and displaying all the contents. Like this:
<h2>LISTINGS</h2>
<% #users.each do |u| %>
<% u.listings.each do |i|%>
<%=u.email %>
<%=i.title %>
<%=i.content %>
<%=i.number %>
<%=i.price %>
<%=i.location %>
<% end %>
<% end %>
What I want to do is have this page only list the titles, each title would link to the appropriate ad. Users could then access their ad with a URL. Ideally the URL would be the title similar to what stackoverflow does "IE in the URL the title is included with "-" instead of spaces" but that is a minor concern at this point.
How is the best way to go about doing this? I may be using incorrect terminology here as I am having trouble finding information.
I think that you should use as below code -
Eg :
<% #users.each do |u| %>
<%= link_to u.name, :controller => 'user', :action => 'show', :id => u.id %>
<% end %>
I hope that you can solve your problem by using this way.

Can Ruby on Rails Automatically generate links to articles?

so my question is after i generate a scaffold and i have the ability to post articles to my site it usually is a link like www.mysite.com/articles/1
my question is...is it possible to have your application automatically generate a link to your www.mysite.com/articles/1 page
because right now i have to manually go into the HTML and add the link
=link_to 'my article', /articles/1
i was just wondering if its possible to make the application automatically generate the link for you?
Yes, it's possible. I don't know based on your question to what articles you would like to link, but if you have this set up using resources in your routes, this should work:
<ul>
<% Article.all do |article| %>
<li><%= link_to article.title, article %></li>
<% end %>
</ul>
Or if you're using HAML (as it appears you are based on the format of your question, but you don't specify:
%ul
- Article.all do |article|
%li= link_to article.title, article
You could modify the query to limit results, paginate them, sort them, etc. as you desire.

Resources