Can Ruby on Rails Automatically generate links to articles? - ruby-on-rails

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.

Related

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 %>

Ruby on Rails: Conditional Links using presence_in

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

Rails simplifies a post method

In online bootcamp project, I'm creating a reddit clone, and I'm at the stage where I'm developing support for accessing posts. Here's the method for displaying all of the posts in the index.
def index
#posts = Post.all
end
Here is the code that creates a link for each post, directing the user to the body of the post:
<% #posts.each do |post| %>
<p><%= link_to post.title, post_path(post.id) %></p>
<% end %>
Now the text states that "Rails lets us simplify this one step further, by allowing us to skip the post_path method altogether," and the resulting code in a separate file ends up omitting the post.id:
<% #posts.each do |post| %>
<p><%= link_to post.title, post %></p>
<% end %>
How is that possible? Does Rails just assume the post in question has the same id in the each element? Does making this change negatively affect the readability of the code?
No it doesn't assume that id is the same. Different object to each link_to, so different url/path for each link_to. This is just a simpler or to be precise a "pithier" way of using link_to, thats all. A bit of rails magic to help lazy developers.
link_to post.title, post is same as this
link_to post.title, post_path(post) or link_to post.title, post_path(post.id).
Rails has a concept of Polymorphic Routes which it uses to discern what the path should be, just simply using the object "post" instead of saying "post_path(post)" is enough in a link_to.
To understand this more, look at the implementation of link_to, url_for and polymorphic routes in Rails.

Seeking a better way to define my Rails routes/paths

I have 2 main components to my application, Users and Properties. The URL should be structured like: hostname.com/users/:user_id/properties/:property_id. I believe I've made a configuration error somewhere, because Rails never recognizes "property_path" or any of its variants, and I've had to hard code them in to get the redirects to work.
routes.rb
resources :users do
resources :properties
end
users/show.html.erb - Notice I had to hard code the path, instead of simply linking to "i"
<% #user.properties.each do |i| %>
<li><%= link_to "#{i.address}", "/users/#{#user.id}/properties/#{i.id}" %></li>
<% end %>
How can I better define my routes so that I can link above to just "i", which would represent the "properties_path", and would auto redirect to that show page?
You don't have to hardcode it. You can do:
<% #user.properties.each do |property| %>
<li><%= link_to property.address, [#user, property] %></li>
<% end %>
Yes, it's that simple. For more information, you can go to Rails guides.

Rails: how to exclude one item from an "each" method

I'm using the Enki blogging gem as a type of content management system. It allows you to create posts and pages. It automatically generates two pages (Home and Archives). I've also created two other example pages, Services and Products, and will create many more. Therefore, when I want to list all the pages on the home page, I do this
<% page_links_for_navigation.each do |link| -%>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
Home
Archives
Services
Products
I may want to create more pages in the future, so it's better to loop over all the pages like this rather than hardcode the url for each page.
But how would I change that code if I wanted to exclude one of those pages (i.e. archives). Enki automatically generates that page and doesn't give me an option to delete it. Moreever, I don't want to delete Archives, because I want to use it where I post link to blog posts.
So, in short, how would I exclude one particular page from this code
<% page_links_for_navigation.each do |link| -%>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
The url for Archives is localhost:3000/archives
another way
<% page_links_for_navigation.each do |link| -%>
<% next if link.name == 'Archives' %>
<li><%= link_to(link.name, link.url) %></li>
<% end -%>
<% page_links_for_navigation.each do |link| -%>
<% if link.name != 'Archives' %>
<li><%= link_to(link.name, link.url) %></li>
<% end %>
<% end -%>
or use page_links_for_navigation.reject {|page| page.name == 'Archives'}.each
Edit:
to add more pages do !['Archives', 'Home'].include? link.name or just add the ones you want to include and remove !
read
http://www.humblelittlerubybook.com/
http://www.ruby-doc.org/docs/ProgrammingRuby/

Resources