How to link_to user's particular page - nested resources - ruby-on-rails

I have the following code:
<%= link_to new_book_path(controller: :books, action: 'new', id: comment) %>
#also tried:
<%= link_to new_book_path(comment.user.id) %>
#outputs: undefined id
<%= link_to new_book_path(comment.user_id) %>
#leads to my (logged-in user) book list, not this user's
<%= link_to new_book_path(comment.user) %>
#same
<%= link_to new_book_path(comment) do %>
#same. comment.post.book.user.id also same.
I was wondering how I can get to this particular user's book list through link_to from this user's comment. I keep going to my own.
My routes are:
resources :books do
resources :posts, shallow: true
end
resources :posts do
resources :comments, shallow: true
end
resources :users do
resources :comments, shallow: true
end

1) in comment:
<%= link_to comment.user.name, books_list_path(user_id: comment.user.id)
books_list_path is path where books are listed
2) in action where books are listed (index or new action):
class BooksController < ApplicationController
..
def index
#books = Book.where(user: params[:user_id])
end
..
end
3) in books/index.html.erb
<% #books.each do |book| %>
<%= book.name %><br>
<% end %>

Related

Unknown action:The action 'rsvp' could not be found for PostsController

So I'm getting the error stated in the title. What code would I have to write in my Postscontroller to fix this? I'm not sure what I would have to do here, would I have to define rsvp in my post controller? If thats the case how would I go about doing that?
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvps.build({post_id: params[:id]})
if rsvp.save
end
end
end
Show.html.erb
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>where:<%=#post.longitude %>, <%=#post.latitude%></p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User_id:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<%= button_to "Rsvp now", rsvp_post_path(#post), class: "btn btn-primary" %>
routes.rb
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :posts
devise_for :users
root 'home#index'
get 'home/ruby_meetup'
resources :posts do
post 'rsvp', on: :member
end
Also I want it so it shows the list of people who already rsvped for the event. How would I go about doing that? I would appreciate some help as I'm still learning rails and this is my first project.
By the following line, you define the route:
resources :posts do
post 'rsvp', on: :member
end
By this line you're requesting this route:
To process the request, you need a controller action rsvp, which actually the error states.
Thus, just define the action (method) and process the request:
class RsvpController < ApplicationController
def create
rsvp = current_user.rsvps.build({post_id: params[:id]})
if rsvp.save
end
end
def rsvp
# process the request here...
end
end

Voting for nested objects using the Acts As Votable gem

I have a Restaurant model that has_many :dishes, through: dish_categories. I found a post that shows how to write the view code necessary to get things going for the Acts As Votable gem. My situation differs being that the dish model is the nested resource that's being voted upon.
I tried translating the provided code but to no avail. At this point should I create a new controller for dishes and place the votable actions there? If so how would I setup my route so I can accomplish this on my restaurant's show page?
Models
class Restaurant < ActiveRecord::Base
has_many :dish_categories, dependent: :destroy
has_many :dishes, through: :dish_categories
end
class DishCategory < ActiveRecord::Base
belongs_to :restaurant
has_many :dishes, dependent: :destroy
delegate :name, to: :dish_category, prefix: "category"
delegate :restaurant, to: :dish_category
end
class Dish < ActiveRecord::Base
belongs_to :dish_category
end
Restaurants Controller
...
def upvote
#restaurant = Restaurant.find(params[:id])
#dish = Dish.find(params[:id])
#dish.liked_by current_user
redirect_to #restaurant
end
def downvote
#restaurant = Restaurant.find(params[:id])
#dish = Dish.find(params[:id])
#dish.disliked_by current_user
redirect_to #restaurant
end
...
Routes
resources :restaurants do
member do
put "upvote", to: "restaurants#upvote"
put "downvote", to: "restaurants#downvote"
end
end
Restaurants - Show View
...
<% #restaurant.dishes.each do |dish| %>
<div>
<h2><%= dish.category_name %></h2>
<b><%= dish.name %></b>
<%= link_to "Upvote", like_restaurant_path(dish), method: :put %>
<%= link_to "Downvote", dislike_restaurant_path(dish), method: :put %>
</div>
<% end %>
A number of things needed to be done to get this to work. The first order of business was moving my controller action to my dishes controller. I also added two more actions: unlike and undislike for toggle functionailty.
NOTE: Logic for authenticating non-registered for users to liking/disliking dishes would still need to be written but this should help get you started.
Dishes Controller
class DishesController < ApplicationController
before_action :load_restaurant_and_dish, only: [:like, :unlike, :dislike, :undislike]
def like
#dish.liked_by current_user
redirect_to #restaurant
end
def unlike
#dish.unliked_by current_user
redirect_to #restaurant
end
def dislike
#dish.disliked_by current_user
redirect_to #restaurant
end
def undislike
#dish.undisliked_by current_user
redirect_to #restaurant
end
private
def load_restaurant_and_dish
#dish = Dish.find(params[:id])
#restaurant = #dish.restaurant
end
end
Next was configuring my routes to correspond with my restaurant and dish models:
Routes
resources :restaurants do
resources :dishes, only: [:like, :unlike, :dislike, :undislike] do
member do
put "like", to: "dishes#like"
put "unlike", to: "dishes#unlike"
put "dislike", to: "dishes#dislike"
put "undislike", to: "dishes#undislike"
end
end
end
I ended up refactoring my show view and created a few partials to reduce clutter now that there's a little bit of logic involved:
Restaurants - Show View
...
<%= render "restaurants/dish_partials/dishes" %>
...
Dishes Partial
<% #dishes.each do |dish| %>
<div>
<h2><%= dish.category_name %></h2>
<span><b><%= dish.name %></b></span>
<%= render "restaurants/dish_partials/like_toggle", dish: dish %>
</div>
<% end %>
Like Toggle Partial
<% if current_user.liked? dish %>
<%= link_to "Unlike", unlike_restaurant_dish_path(#restaurant, dish), method: :put %>
<% else %>
<%= link_to "Like", like_restaurant_dish_path(#restaurant, dish), method: :put %>
<% end %>
<% if current_user.disliked? dish %>
<%= link_to "Undislike", undislike_restaurant_dish_path(#restaurant, dish), method: :put %>
<% else %>
<%= link_to "Dislike", dislike_restaurant_dish_path(#restaurant, dish), method: :put %>
<% end %>

Locale Switcher

I am busy going through PBP - Agile Web Development with Rails and implementing the locale switcher.
However when I try switch between english and spanish I get a error:
No route matches [POST] "/en"
My controller is as follows:
class StoreController < ApplicationController
skip_before_filter :authorize
def index
if params[:set_locale]
redirect_to store_path(locale: params[:set_locale])
else
#products = Product.order(:title)
#cart = current_cart
end
end
end
and an extract of the application.hmtl.erb that is being used;
<div id="banner">
<%= form_tag store_path, class: 'locale' do %>
<%= select_tag 'set_locale', options_for_select(LANGUAGES, I18n.locale.to_s), onchange: 'this.form.submit()' %>
<%= submit_tag 'submit' %>
<%= javascript_tag "$('.locale input').hide()" %>
<% end %>
<%= image_tag("logo.png") %>
<%= #page_title || t('.title') %>
</div>
the routing folder is as follows:
scope'(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
Cant figure out what I did wrong. If I enter /en or /es in the url it works correctly. However choosing it in the drop down that is created I get the error mentioned
Found the issue, the form_tag was expecting a POST so I changed
<%= form_tag store_path, class: 'locale' do %>
to
<%= form_tag store_path, class: 'locale', :method => :get do %>
and it worked
You are missing an slash in the scope as guides states:
# config/routes.rb
scope "/:locale" do
resources :books
end
http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params

Deleting a HABTM object

I have the following code which gives me an ActionView::Template::Error (undefined methodroster_path' for #<#:0x007fe34005c208>):`
While in the background it deletes the association player_roster (Has and belongs to many), but i want to delete it when i press the link.
The roster path is nested within a team, but the issue is regarding roster and players.
<%= form_for [#team, #roster] do |f| %>
<% #players.each do |player| %>
<%= player.gamertag %>
<%= link_to "Delete", player.rosters.delete(#roster) %>
<% end %>
<% end %>
:Update
Player.rb
class Player < ActiveRecord::Base
has_and_belongs_to_many :rosters
belongs_to :country
mount_uploader :picture, PictureUploader
end
Roster.rb
class Roster < ActiveRecord::Base
has_and_belongs_to_many :players
has_many :placements
belongs_to :team, touch: true
end
The way you are doing it now will call your delete when the page loads. You can't link to arbitrary Ruby code, you need to link to a route and controller action which will perform your logic.
<%= form_for [#team, #roster] do |f| %>
<% #players.each do |player| %>
<%= player.gamertag %>
<%= link_to "Delete", player_roster_path(player, #roster), method: :delete %>
<% end %>
<% end %>
This link will route to players/:id/rosters/:id with the DELETE HTTP action, which Rails will route to the destroy method.
class RostersController < ApplicationController
def destroy
#player = Player.find(params[:player_id])
#roster = Roster.find(params[:id])
#player.rosters.destroy(#roster)
# redirect/render
end
end
You also will need to setup player_roster_path as a route in config/routes.rb
resources :players do
resources :rosters, only: [:destroy] # you may have other routes here as well
end

User friendships Nesting logic in Rails 3

I am trying to finish my forum application. I want arrange user Friendships. I am using devise as authentication system. I want some suggestions.
Shall i nest the resources in User and Friendships. This is the way Railscasts used:
devise_for :users
resources :users, only: [:index, :show]
resources :friendships, only: [:create, :destroy]
This is how i used:
devise_for :users
resources :users, only: [:index, :show] do
resources :friendships, only: [:create, :destroy]
end
My real problem is that . I want to use friendships in a way that , signed_in user can check the users list , and add a friend if he is not in the friendslist. Now i can add a friend multiple times. And also ,user can add himself as a friend.
How can i fix this links with if/else statements:
showing a user profile works:
<section>
<h1>
<%= #user.username %>
</h1>
<%= link_to "Arkadaşlarıma Ekle", friendships_path(:friend_id => #user), :method => :post,class: "btn btn-large btn-primary" %>
</section>
and herei I cant find the way to show the friend's profile. :
<% for friendship in #friendships %>
<%= link_to friendship.friend.username, '#' %>
(<%= link_to 'Sil', friendship, :method => :delete %>)
<% end %>
..
rake routes:
I can understand that , I have to use correct if/elses but i am lost in nesting my resources and routing. . Thanks for explanations..
These are my edits:
In my users_controller:
def user_is_friend?(other_user)
current_user.friendships.find_by_friend_id(other_user.id)
end
<% unless user_is_friend?(#user) %>
<%= button_to "Add friend", friendships_path(:friend_id => user), :method => :post %>
<% end %>
Is it correct?
I would recommend you, in your user index view, when you are looping with a variable user (for #users do |user|) to add first a condition:
<% unless current_user == user %>
--> this will allow you to process all users except the current_user (i.e. yourself)
Then, you can define this:
<% user_is_friend = current_user.friendships.find_by_friend_id(user.id) %>
This will be TRUE if the user is already in your friend's list
And then, a last piece:
<% if !user_is_friend %>
<%= button_to "Add friend", friendships_path(:friend_id => user), :method => :post %>
<% end %>

Resources