Dynamically adding pages to rails app - ruby-on-rails

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.

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

Stuck trying to list associated model

I think I am deeply misunderstanding how to write instances.
Miniatures have_many Manufacturers through the Productions table.
On the miniatures show page I am trying to list all the manufacturers for the current miniature and have them link the Manufacturer show page. Like so:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturer.name, manufacturer_path %>
<% end %>
Needless to say it does not work. It gives "undefined method `manufacturer'".
I have tried A LOT of different combinations to no avail. The following version puts all the manufacturers, rolled into one link, once for each manufacturer a miniature has, and links to /manufacturers. A big mess.
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to #miniature.manufacturers.map(&:name).join(', '), manufacturer_path %>
<% end %>
I have been working on other things and hoping I would get the hang of this but I'm pretty sure it's something pretty fundamental about how I set up the instance.
If it's more likely something I need to add to the controller then I can add my controller code here. Any help much appreciated.
Does this work:
<% #miniature.manufacturers.each do |manufacturer| %>
<%= link_to manufacturer.name, manufacturer_path(manufacturer) %>
<% end %>

Rails Way: Take a link on show page of one controller and connect it to new page of another controller

I have on my holder (think of it as a relationships table holding all the questions that belong to a certain holder and the holder specific data like name) show page a list of questions and a link to New Question link.
<% #questions.each do |question| %>
<%= question.question %>
<%= question.answer %>
<%= link_to "Edit Question", edit_question_path(question) %>
<br />
<% end %>
<%= link_to "New Question", new_question_path %>
The goal of this is to set the holder_id when the new question is created. I have the belongs_to and has_many setup in the model if that matters.
I've tried a few different things such as <%= link_to "New Question", new_question_path(#holder) %> but that just sets the format: to the #holder.id. I suppose, I could abuse that to make it work, but that's a very ugly hack.
I've heard people over use nested routes, and not to go more than 2 or 3 deep. So there's got to be a way to do this without using nested routes.
Am I correct in that there is a way to do this without using nested routes? What is it?
Why would nested routes be a good or bad idea for this example?
I suppose you can do something like this.
In your link to new:
<%= link_to "New Question", new_question_path(:holder_id => #holder.id)
This link will make your request send holder_id as a parameter to the new action in your question_controller.rb. So you can use it like this:
def new
holder_id = params[:holder_id]
# Do something with this id
# ...
end

Help me refactor my admin user views vs. non admin user views

I have a list of books that displays edit buttons and a bunch of extra info if the user that's logged in is an admin. Right now I have two separate partials that are rendered depending on what type of user is logged in. I used to have just one partial with a bunch of if user.admin? statements, but it started to get real ugly. Now I am juggling around two files, with little bits of duplicate data in each. Is there any better way to do this?
index.html.erb
<ul>
<% if #current_user.admin? %>
<%= render :partial => "book", :collection => #books %>
<% else %>
<%= render :partial => "non_admin_book", :collection => #books %>
<% end %>
</ul>
_book.html.erb
Title: <%= book.title %> EDIT BUTTON
<!-- Awesome extra info for admins -->
Author: <%= book.author %>
<!-- Awesome extra info for admins -->
_non_adminbook.html.erb
Title: <%= book.title %>
Author: <%= book.author %>
This question is like : should I only use I18n keys all over one partial/view or should I use X views/partials for each language ?
There is no good or bad solution. My opinion is that you should begin by using conditionals like <% if admin? %> blah blah <% end %>...
Then, if your admin view grandly differs from your non admin views, delete the conditionals and make two views : my_view / my_view_admin.
#192 Authorization with CanCan this cancan gem may help you
I really do not like any kind of duplication, but sometimes it is the easiest solution.
In your case, I can tell that
an administrator has the option to edit fields (inline?)
an administrator sees more fields
Generally I use the on_the_spot gem for inline editing, and I then work with a helper like this:
def on_the_spot_edit_if_allowed(object, field, options)
if current_user.admin?
on_the_spot_edit object, field, options
else
object.send(field)
end
end
And in that case my views becomes something like
Title: <%= on_the_spot_edit_if_allowed book, title %>
<%- if current_user.admin? %>
<!-- Awesome extra info for admins -->
<% end %>
Author: <%= book.author %>
<%- if current_user.admin? %>
<!-- Awesome extra info for admins -->
<% end %>
Unless it is otherwise (design/UI constraints) impossible, I would refactor that view to the following:
Title: <%= on_the_spot_edit_if_allowed book, title %>
Author: <%= book.author %>
<%- if current_user.admin? %>
<%= render :partial => 'extra_admin_fields'
<% end %>
Hope this helps.
Keep it like it is.
Your duplications are not that big.
The #current_user.admin? condition will be run only once with your solution.
If you put #current_user.admin? in a shared partial, it will be run for every single member of this collection. Not cool.

Question about checkbox on Ruby on Rails

I'm developing a website for my University and I got a problem.
There is a page where I have the list of all students of the university. The admin can select some students that will be able to go to a selective process and then he have to see them in other separate page.
How can I do that using Ruby On Rails?
Thanks,
Hugo Henley
Hi If you want use checkboxes you should write inside your form something similar to <td><%= check_box_tag "user_ids[]", "#{user.id}", true%><%= user.name%></td> then you'll get array od user ids as an params[:user_ids] and you may show only this users on other page
<h1>in your view </h1>
By checking those ones using checkboxes you can get the id's of those students like this
<%= check_box_tag "user_ids[]", "#{user.id}", true%>
passing those id's into the respective controller action
#users = User.where(:id => params[:user_ids])
Display those object details in to the required webpage using #users.each
<% #users.each do |user| %>
<%= user.name %>
<%end%>

Resources