Ruby on rails Merge two routes - ruby-on-rails

So i have the controller named collection and the model name products.
The collection page is showing all the products.
Here is the code that show's the products
<% #products.each do |x| %>
<div class="col-lg-3">
<div class="product-container animated fadeIn">
<div class="product-image-holder">
<%= image_tag x.image1.url(:fhd) %>
</div>
<div class="product-title-holder">
<span class="product-title"><%= x.title %></span>
</div>
</div>
</div>
<% end %>
Now i want to wrap each product in it's own link ( link to single product )
i managed to do that but the link will be www.localhost.com/product/1 instead of this i want to be www.localhost.com/collection/product/1
Any help ? :)
This are the routes
get 'collection', to:'collection#index'
resources :product

Use a scope
scope '/collection' do
resources :products
end
More info here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

Related

Trying to collect a list of ids from checkboxes to delete from my database

I'm creating an app (personal project) that'll help us schedule and record Nerf tournaments for a group I hang out with. I have tournament and player models. A tournament can have multiple players.
I want to be able to check multiple players and choose to delete them from the tournament. The thing is though is that all of my players are rendered in a nested partial. And my tournament partial has a form already inside of it.
Here is _tournament.html.erb:
<section class="panel" id="<%= dom_id(tournament) %>">
<h1 class="heading">
<%= tournament.name %>
</h1>
<ul>
<%= render tournament.players %>
</ul>
<section class="add_players">
<%= form_for [tournament, Player.new] do |f| %>
<div class="form-group">
<%= f.text_field :name %>
</div>
<div class="form-group">
<%= f.text_field :team %>
</div>
<%= f.submit "Add Player", class="button blue" %>
<% end %>
</section>
<section class="delete_all">
<button class="button red" type="button">Delete Players</button>
</section>
</section>
and here is _player.html.erb:
<li id="<%= dom_id(player) %>">
<div class="player">
<%= check_box_tag "player_ids[]", player.id %>
<span class="name"><%= player.name %></span> |
<span class="team"><%= player.team %></span>
</div>
</li>
I'm familiar with Railscast #52 where he does a simpler example of this, but I can't figure out how to get all of those checked checkboxes in the tournament partial and use those ID's to delete those players from the tournament.
I've tried wrapping the _tournament.html.erb partial inside of a form_tag but it breaks some styling (and HTML doesn't really allow that as a standard.)
Any guidance or help would be great!
What you are doing is creating a form that posts to /tournaments/:tournament_id/players which makes sense if you are assigning the players one by one or creating a single player.
It does not make sense at all if you are assigning multiple players to a tournament. Because you are actually changing the parent resource.
In that case you should be sending a POST request to /tournaments (to create a tournament) or a PATCH request to /tournaments/:id (to modify an existing record).
<%= form_for tournament do |f| %>
<div class="form-group">
<%= f.label :player_ids, "Assign players" %>
<%= f.collection_check_boxes :player_ids, Player.all, :id, :name %>
</div>
<%= f.submit %>
<% end %>
class TournamentsController < ApplicationController
# ...
private
def tournament_params
params.require(:tournament)
.permit(:foo, :bar, player_ids: [])
end
end
If you want to be able to create multiple player records in a single request you should use nested attributes and fields_for.
To unassign all the players you can do a PATCH request to /tournaments/:id
with an empty array as the value for tournaments[:players_ids]. You can do this with javascript by creating a handler that unchecks all the checkboxes and submits the form when the user clicks the button (or confirms it).
Or you could create a custom DELETE /tournaments/:tournament_id/players route.
resources :tournaments do
resources :players
delete :players, on: :collection, action: :delete_all_players
end

Rails 5 Navigational Menu from deep nested routes

I need to create a dropdown sidebar navigational menu from my Rails 5 nested routes. I have 4 models - locations, categories, subcategories and products. We currently have 4 locations, so I created a query in my application_controller to default the path to our corporate location, San Diego.
From there, I created a category query based on the location query. All is good, I can iterate and print the results to the view, with the category as the title of the dropdown. I need to show the sub-categories in the category dropdown they belong to but I keep getting an undefined error.
I know the nested routes that I have are not ideal for most situations, but this site needs absolute defined urls for search engine ranking...just so you know before you comment on them.
application_controller.rb
class ApplicationController < ActionController::Base
...
before_action :load_vendors
def load_vendors
#location = Location.find_by_name('San Diego')
#category = #location.categories
#subcategory = #category.subcategories
end
...
routes.rb
resources :locations do
resources :categories, path: '' do
resources :subcategories, path: '' do
resources :products, path: ''
end
end
end
_sidebar.html.rb
<div id="ss_menu">
<% #category.each do |c| %>
<div class="ss_button"><%= c.name %></div>
<div class="ss_content">
<ul>
<% #subcategory.each do |s| %>
<li>
<a href="">
<%= s.name %>
</a>
</li>
<% end %>
</ul>
</div>
<% end %>
</div>
First of all, you forgot to add erb extension _sidebar.html.erb.
Moreover, this is what the view should look like:
<div id="ss_menu">
<% #categories.each do |category| %>
<div class="ss_button"><%= category.name %></div>
<div class="ss_content">
<ul>
<% category.subcategories.each do |subcategory| %>
<li>
<%= link_to subcategory.name, ... %>
</li>
<% end %>
</ul>
</div>
<% end %>
</div>
What kind association type do you used for categories?

Show only names that belong to a code

So in my app a guest searches for their RSVP code in the code/index.html page then clicks on a continue link that brings them to the rsvp page. I am trying to show only names that belong to the specific code on the RSVP but currently all names are showing up. I have passed the code as a param in the link on the index page but when I get to the rsvp page it is showing up in the url with a period which doesn't seem right ex: /rsvp.1
so I am guessing that this is where I am going wrong but I cant figure out why:
associations
Guest belongs_to :code
Code has_many :guest
code/index.html.erb
(page with the link that takes user to rsvp page and passes the params to rsvp page)
<h2><%= link_to 'Continue', rsvp_path(code) %></h2>
code_controller
def index
#codes = Code.search(params[:search])
end
def rsvp
code_id = params[:code]
#guests = Guest.where("code_id", "#{code_id}")
end
code/rsvp.html.erb
<div class="row">
<div class="box">
<div class="col-lg-12">
<form class="row form-inline">
<% #guests.each do |guest| %>
<%= guest.name %>
<% end %>
<div class="form-group">
<p> Will you be attending the wedding of Kristen Albrecht and Chris Alford September 1, 2017? </p>
<button aria-haspopup="true" class="btn btn-default dropdown-toggle" ngbdropdowntoggle="" type="button" aria-expanded="false">
Yes</button>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">No</button>
</div>
</form>
</div>
</div>
</div>
routes
resources :codes
resources :guests
get '/code' =>'codes#code'
get '/rsvp' =>'codes#rsvp'
To make sure that code is a parameter in the URL, you have to make the following changes:
routes.rb
get '/rsvp/:code' =>'codes#rsvp'
code/index.html.erb
<h2><%= link_to 'Continue', rsvp_path(code: code) %></h2>
This way params[:code] will have a value in the code_controller
Okay I figured it out:
I changed the link on the index page to
<%= link_to "Continue", {:controller => "codes", :action => "rsvp", :code_id => code.id }%>
then I changed the controller code to
def rsvp
#guests = Guest.where(code_id: params[:code_id])
end

Ruby - Is it possible to create two different loops in the same "view" file?

I'm currently learning Ruby on Rails thanks to a website named codecademy, and I'm learning how to display informations from a database's array stocked into a variable
The exercice's correction is as shown below :
<div class="main movie-show">
<div class="container">
<div class="movie">
<!-- Display the movie info here -->
<div class="info">
<%= image_tag #movie.image %>
<h3 class="movie-title"><%= #movie.title %></h3>
<p class="movie-release-year"><%= #movie.release_year %></p>
<p class="movie-plot"><%= #movie.plot %></p>
</div>
</div>
<h2>Cast</h2>
<% #actors.each do |actor| %>
<div class="actor">
<%= image_tag actor.image %>
<h3 class="actor-name"><%= actor.first_name %> <%= actor.last_name %></h3>
<p class="actor-bio"><%= actor.bio %></p>
</div>
<% end %>
</div>
</div>
You can see in the "movie" part that they directly take the variable to display the information needed, while they stock all the "actor" 's part in another |actor| variable
My question is the following, as I didn't find any satisfying answer online, is it possible to use two variables the same way in the same file ? Like using
<% #movies.each do |m| %>
and
<% #actors.each do |a| %>
Will it work anyway ? Will there be an error?
You can use as many number of instance variables as you want in your view provided that they are properly defined in your controller code.
If you have defined both #actors and #movies instance variables in your controller action, then you can access then them in corresponding view. Remember: I wrote, corresponding view.
There is other way as well. For example, if you have defined relation between your Movie model and your Actor model, and the relation states that a movie can have many actors. In that case, you only need to instantiate #movies in your controller, and then you can access actors in the following way:
<% #movies.each do |movie| %>
<% movie.actors.each do |actor| %>
<%# All other relevant code %>
<% end %>
<% end %>
In case, you don't know about relations, you can define them in following way:
class Movie < ActiveRecord::Base
has_many :actors
end
class Actor < ActiveRecord::Base
belongs_to :movie
# actors table should have a column named 'movie_id' for this to work
end

Paginating Tags in Rails 4

I'm using Kaminari to paginate my posts page. A post has many tags and each tag links to a "Show" page that displays all posts with that tag. I'm trying to paginate this tags page but it never quite works.
tags/show
<div class="post-index">
<h1><%= #tag.name %></h1>
<ul>
<% #tag.posts.each do |post| %>
<li>
<div class="post">
<div class="featured-image">
<%= link_to image_tag(post.featured_image.url(:featured)), post_path(post) %>
</div>
<div class="info">
<div class="title">
<h2><%= link_to post.title, post_path(post) %></h2>
</div>
<div class="excerpt">
<p><%= truncate(strip_tags(post.body), length: 360) %></p>
</div>
</div>
</div>
</li>
<% end %>
</ul>
<%= paginate #tags %>
</div>
tag.rb
lass Tag < ActiveRecord::Base
has_many :taggings
has_many :posts, through: :taggings
paginates_per 2
end
tags_controller.rb
class TagsController < ApplicationController
def show
#tag = Tag.find(params[:id]).page(params[:page])
end
end
I've tried all I can think of. The examples Kaminari uses are
tag = Tag.order(:name).page(params[:page])
but this doesn't work and returns a no method "posts" error. I tried paginating the posts model instead but that doesn't work.
If I remove any pagination reference, it displays all the posts correctly on the tag page.
Thanks for any advice
I'm not sure about what do you want to paginate? Tags or posts? In your code, you want to paginate "#tags", but you never define "#tags".
#tag = Tag.find(params[:id])
#posts = #tag.posts.page(params[:page])

Resources