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
Related
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
I am following this stackoverflow post, How can i store and show only the last 5 NEWS seen by the current user?
However, i think I am getting this error (Couldn't find Article with 'id'=python-0), because I'm using friendly_id gem to generate the article url. Is there a workaround for this?
Application controller
before_action :recently_viewed_articles
def recently_viewed_articles
session[:article_id] ||= []
session[:article_id] << params[:id] unless params[:id].nil?
session[:article_id].delete_at(0) if session[:article_id].size >= 5
end
Pages Controller
def home
#recent_articles = session[:article_id]
#feed = current_user.feed
end
_feed.html.erb
<div class = "container">
<div class = "row">
<div class = "col-md-9">
<%= render #feed %>
</div>
<div class = "col-md-3">
<div class="list-group">
<button type="button" class="list-group-item list-group-item-action active">
Recently visited
</button>
<% #recent_articles.each do |recent| %>
<button type="button" class="list-group-item list-group-item-action">
<%= link_to "#{Article.find(recent).title}", article_path(Article.find(recent)) %>
</button>
<% end %>
</div>
</div>
</div>
</div>
As per documentation, I believe you should be using friendly scope.
Article.friendly.find(recent)
https://github.com/norman/friendly_id
Have you set everything up as you should do? If you haven't, then you can use:
Article.friendly.find('python-0')
Otherwise, in your article model:
friendly_id :title, use: [:slugged, :finders]
Then you can call
Article.find('python-0')
I have a resource Invoice and every invoice has an edit path:
http://localhost:3000/invoices/1/edit
On my index page where I will all my invoices I want to have an input field and a submit button to jump right to the edit page for a certain invoice:
<form class="form-inline" action="???" method="get">
<div class="form-group">
<input type="text" class="form-control date" placeholder="Invoice ID" name="id" >
</div>
<button type="submit" class="btn btn-info">Show</button>
</form>
How do I have to define the action so that if I enter for example 1 and click on Show it goes right to my edit method?
I think you're looking for something like this in your html
<%= form_tag find_invoice_url do |f| %>
<%= f.text_field :invoice_number =>
<%= f.submit "Search" =>
<= end =>
then you would need to add a route
post '/invoices/find_invoice', to: "invoices#find_invoice",
as: "find_invoice"
then in your controller something like
def find_invoice
invoice = Invoice.find_by_id(params[:invoice_number])
if invoice
redirect_to invoice_url(invoice.id) #whatever your edit route is
else
redirect_to root_url #anywhere really
end
end
if you are iterating through all the invoices, you wouldn't need an input field.
assuming that you have an invoice obhect, you would just do something like
<%= form_tag edit_invoice_path(invoice), method: "get" do -%>
<%= submit_tag "Show", class="btn btn-info" %>
<% end %>
You may want the button to say edit instead - that's more of a ux "principle of least surprise" issue
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
I have a search method and two models, restaurants and menus.In the menu search method, if I search for "chicken" it shows the closest restaurants near the location that sells meals with word chicken example "chicken sandwich" with the use of association. The problem is, if a restaurant sells multiple chicken meals the restaurant is rendered several times.I want to be able to show restaurants once and when the button is clicked a modal shows the meals with "chicken" in the name using a loop if there are multiple meals with chicken. I tried adding uniq & uniq(&:restaurant_id) in the ActiveRecord and also controller & also trying "DISTINCT" using sql but nothing.
Controller
def search_for_menus
##restaurant= Menu.restaurant.all
#menus = Menu.search_for_menus(params)
end
Menus Model
belongs_to :restaurant
reverse_geocoded_by "restaurants.latitude", "restaurants.longitude"
def self.search_for_menus(params)
menus=Menu.where("dish LIKE ?", "%#{params[:dish]}%") if params[:dish].present?
menus = menus.where("menus.price <=?",params[:max ])
menus = menus.where("menus.price >=?",params[:min ])
menus= menus.joins(:restaurant).near(params[:location],2)
menus
end
end
Search Page HTML
<% if #menus.present? %>
<%#menus.each do |menu| %>
<br>
<br>
<div id ="restaurants">
<div class="box">
<div class="thumbnail box-image">
<%= link_to (image_tag (menu.restaurant.thumbnail.url(:medium))),restaurant_path(menu.restaurant),'data-no-turbolink' => true%>
<div class="caption">
<h3 class="text-center"><%=menu.restaurant.name%></h3>
<p class="text-center">View Food</p>
</div>
</div>
</div>
<div id="modal-<%= menu.id %>" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="col-md-6">
<p><%= menu.dish %></p>
</div>
<div class="col-md-6">
<p><%= menu.price %></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
</div>
<%end%>
</div>
<%else%>
<p>no posts</p>
<%end%>
What you need to start with, I think, is the restaurants for which there exists a matching menu item.
restaurants = Restaurant.near(params[:location],2).
where(Menu.where("menus.restaurant_id = restaurants.id").
where("dish LIKE ?", "%#{params[:dish]}%").
where("menus.price <=?",params[:max ]).
where("menus.price >=?",params[:min ]).exists)
This is guaranteed to return one row per restaurant.
You might want ILIKE for a case insensitive dish search, if your RDBMS supports it.
You can then list the appropriate menu items for the matching restaurants.
After discussing over chat, here's an update, thanks #David for the query.
Menu model:
belongs_to :restaurant
reverse_geocoded_by "restaurants.latitude", "restaurants.longitude"
def self.search_for_restaurants(params)
Restaurant.near(params[:location],2).
where(Menu.where("dish LIKE ?", "%#{params[:dish]}%").
where("menus.price <=?",params[:max ])
where("menus.price >=?",params[:min ]).exists)
end
Put this in the restaurant model (here you are going to fetch all the matching menus for the view):
def search_for_matching_menus dish
menus.where("dish LIKE ?", "%#{dish}%")
end
View (you have to adjust it to your needs, I just want you to understand the idea):
<% if #restaurants.present? %>
<%#restaurants.each do |restaurant| %>
<br></br>
<br></br>
<div id ="restaurants">
<div class="box">
<div class="thumbnail box-image">
<%= link_to (image_tag (restaurant.thumbnail.url(:medium))),restaurant_path(restaurant),'data-no-turbolink' => true%>
<div class="caption">
<h3 class="text-center"><%=restaurant.name%></h3>
<%= restaurant.search_for_matching_menus(#dish).each do |menu| %>
<%= menu.id %>
<% end %>
</div>
</div>
</div>
<%end%>
<%end%>
And finally this has to go on the controller that calls the search, to forward the dish parameter:
#dish = params[:dish]