Rails: Image Uploading Site. Home page - ruby-on-rails

class PhootosController < ApplicationController
before_action :logged_in_user
def index
#phootos = Phooto.all.sample(1)
end
def new
end
def show
#phooto = Phooto.find(params[:id])
end
def create
#phooto = current_user.phootos.build(phooto_params)
if #phooto.save
flash[:success] = "Photo created!"
redirect_to uploads_url
else
redirect_to root_url
end
end
def favorite
#phooto = Phooto.find params[:id]
if request.put?
current_user.favorites << #phooto
redirect_to :back, notice: 'You successfully favorited #{#phooto.name}'
elsif request.delete?
current_user.favorites.delete(#phooto)
redirect_to :back, notice: 'You successfully unfavorited #{#phooto.name}'
else
redirect_to :back, notice: 'Nothing happened.'
end
end
def feed
#favorites = current_user.favorites.all
end
def uploaded
#phootos = current_user.phootos.all
end
private
def phooto_params
params.require(:phooto).permit(:picture)
end
end
show.html.erb
<p><strong>Picture:</strong></p>
<%= image_tag(#phooto.picture) %>
<%= link_to("< Previous", #phooto.previous) if #phooto.previous %>
<%= link_to("Next >", #phooto.next) if #phooto.next %>
<% if current_user %>
<%= link_to "favorite", favorite_phooto_path(#phooto), method: :put %>
<%= link_to "unfavorite", favorite_phooto_path(#phooto), method: :delete %>
<% end %>
www.example.com/photos/1
Professional websites have the homepage www.example.com load with a photo already displayed. Then when you click next/previous, you are routed to www.example.com/photos/#{photo_id}
How do I get my website to do this? I also want it setup so that each day a different random photo is displayed in the homepage.

If you want to show a random photo on the homepage, you need the following:
#config/routes.rb
resources :photos, only: [:index, :show]
#app/controllers/photos_controller.rb
class PhotosController < ApplicationController
def index
random = ActiveRecord::Base.connection.adapter_name == 'MySQL' ? "RAND()" : "RANDOM()"
#phooto = Photo.order(random).first
end
def show
#phooto = Photo.find params[:id]
end
end
Refs:
https://stackoverflow.com/a/2536743/1143732 (adapter)
https://stackoverflow.com/a/25577054/1143732 (random)
--
This will allow you to populate the #phooto variable in both the index and show actions;
#app/views/photos/index.html.erb
<%= render #phooto %>
#app/views/photos/show.html.erb
<%= render #phooto %>
#app/views/photos/_photo.html.erb
<p><strong>Picture:</strong></p>
<%= image_tag(photo.picture) %>
<%= link_to("< Previous", photo.previous) if #phooto.previous %>
<%= link_to("Next >", photo.next) if #phooto.next %>
<% if current_user %>
<%= link_to "favorite", favorite_phooto_path(phooto), method: :put %>
<%= link_to "unfavorite", favorite_phooto_path(phooto), method: :delete %>
<% end %>
You'll be able to work out the rest.
In regards rendering a new random image each day, you'll have to store the "random" value each time the day changes.
This would be best done with a cron job, invoking a rake task:
#lib/tasks/new_random.rb
namespace :random do
task :generate => :environment do
# save random for the day (maybe in a model or redis)
end
end

Related

Issue with a nested form_for's parameter

I'm creating a website on which people can read mangas, thanks to three scaffolds : one for the manga itself, one for its chapters and a last one for the chapter's pages. In my routes.rb file, I nested the pages inside the chapter resources, which I nested inside the manga's, so my routes look like the following:
resources :mangas do
resources :chapters do
resources :pejis #Rails doesn't like the "scan" word
end
end
I can create a manga without troubles, but for an unknown reason, I can't manage to make the form appear:
views/chapters/_form.html.erb
<%= form_for(chapter) do |f| %>
<div class="field">
<%= f.label :titre %>
<%= f.text_field :titre %>
</div>
<div class="field">
<%= f.label :apercu %>
<%= f.file_field :apercu %>
</div>
<div class="field">
<!-- Allows me to upload the chapter's pages in the same time -->
<label for="images[]">Multi Upload</label>
<%= file_field_tag 'images[]', type: :file, multiple: true %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The form_for parameter is as it was when created by the scaffold command. However, obviously, it doesn't work since I nested the resources inside the manga scaffold. I tried a few things, until new_manga_chapter_path allowed me to see the form. However, when submitting it, I'm getting the following error:
No route matches [POST] "/mangas/1/chapters/new". I'm not even sure if this is normal or strange.
I'm pretty sure I shouldn't put "new_manga_chapter_path" as parameter for the form_for but I have no idea what to put instead.
Just in case, here is the chapter controller:
class ChaptersController < ApplicationController
before_action :set_chapter, only: [:show, :edit, :update, :destroy]
def index
#chapters = Chapter.all
end
def show
end
def new
#chapter = Chapter.new
end
def edit
end
def create
#chapter = Chapter.new(chapter_params)
if #chapter.save
(params[:images] || []).each_with_index do |image, index|
#chapter.pejis.create(image: image, scan_number: index + 1)
end
redirect_to #chapter, notice: 'Chapter was successfully created.'
else
render :new
end
end
def update
if #chapter.update(chapter_params)
(params[:images] || []).each_with_index do |image, index|
#chapter.pejis.create(image: image, scan_number: index + 1)
end
redirect_to #chapter, notice: 'Chapter was successfully updated.'
else
render :edit
end
end
def destroy
#chapter.destroy
redirect_to chapters_url, notice: 'Chapter was successfully destroyed.'
end
private
def set_chapter
#chapter = Chapter.friendly.find(params[:id])
end
def chapter_params
params.require(:chapter).permit(:titre, :apercu)
end
end
Don't hesitate if you want to see something else
Thank you in advance
Try this:
# don't replace the [] by ()
<%= form_for [#manga, #chapter] do |f| %>
Or
<%= form_for manga_chapter_path(#manga, #chapter) %>
And in your controller:
def new
#manga = Manga.find(params[:manga_id])
#chapter = Chapter.new
end
To help you after your comments:
In your create method:
def create
#manga = Manga.find(params[:manga_id])
#chapter = Chapter.new(chapter_params)
if #chapter.save
(params[:images] || []).each_with_index do |image, index|
#chapter.pejis.create(image: image, scan_number: index + 1)
end
redirect_to manga_chapter_path(#manga, #chapter), notice: 'Chapter was successfully created.'
else
render :new
end
end

Rails: link_to delete from different controller

Right now I have a like button that allows you to like foods. When you try to unlike the food, I get this error:
The action 'destroy' could not be found for UsersController
I'm not sure why it is looking for the destroy action in the users controller. My only guess is because the button is on the user show page, so I assume it defaults to that controller, but how would I access the delete method from my votes controller?
Shared like form
<% unless current_user.votes.empty? || current_user.votes.pluck(:food_id).include?(food.id) %>
<%= form_for #vote do |f| %>
<%= f.hidden_field 'food_id', food.id %>
<%= f.hidden_field 'user_id', food.user.id %>
<%= f.submit "Vote", :class => "like_button" %>
<% end %>
<% else %>
<% vote = food.votes.where(user_id: current_user.id).first %>
<div class="unlike_button">
<%= button_to "Unlike", vote, method: :delete %>
</div>
<% end %>
class VotesController < ApplicationController
def index
#votes = Vote.all
end
def new
#vote = Vote.new
end
def create
#vote = Vote.new(vote_params)
if #vote.save
puts #vote
flash[:notice] = "Thanks for voting!"
redirect_back(fallback_location: root_path)
else
puts "No"
flash[:notice] = "Something went wrong"
redirect_back(fallback_location: root_path)
end
end
def destroy
#vote = Vote.find(params[:id])
if #vote.destroy!
flash[:notice] = "Unvoted!"
redirect_back(fallback_location: root_path)
end
end
private
def vote_params
params.require(:vote).permit(:food_id, :user_id)
end
end
class Vote < ApplicationRecord
belongs_to :user
belongs_to :food
end

How to make a simple check like unless current_user on favorites work?

I am building an app where a user can add a room to favorites. It works but it also duplicates the relationship multiple times. For example, someone can favorite the same room multiple times. Therefore I wanted to implement a check in the controller unfortunately I am getting this error:
How can I make this work?
rooms_controller.rb
before_action :set_room, only: [:show, :favorite]
def favorite
type = params[:type]
if type == "favorite"
current_user.favorites << #room unless current_user.rooms.exists?(room)
redirect_to wishlist_path, notice: 'You favorited #{#room.listing_name}'
elsif type == "unfavorite"
current_user.favorites.delete(#room)
redirect_to wishlist_path, notice: 'Unfavorited #{#room.listing_name}'
else
# Type missing, nothing happens
redirect_to wishlist_path, notice: 'Nothing happened.'
end
end
private
def set_room
#room = Room.find(params[:id])
end
end
routes.rb
resources :rooms do
put :favorite, on: :member
end
show.html.erb
<% if current_user %>
<%= link_to "favorite", favorite_room_path(#room, type: "favorite"), method: :put %>
<%= link_to "unfavorite", favorite_room_path(#room, type: "unfavorite"), method: :put %>
<% end %>
If your User model has many favorites, just fix typo:
current_user.favorites << #room unless current_user.favorites.exists?(#room)

Rails - Updating and deleting content?

EDIT: I managed to delete! i had to define teh instance variable #movies = Movie.find(params[:id]) to the delete method in the controller.
I still can't update though. I get "param is missing or the value is empty: movie"
I forgot to add my contrller! sorry!
I'm trying to replicate one of my in class exercises into another app, for practice.
The idea is to be able to add new movies into a database, and get the option to update their info, and delete them as well. I can add new content, but I can't update them or delete them.
Appreciate any inputs I can get.
Routes.rb
Rails.application.routes.draw do
root "movies#index"
get "/movies", to: "movies#index"
get "/movies/new", to: "movies#new", as: "new_movie"
get '/movies/:id', to: "movies#movie_page", as: "movie_page"
post "/movies", to: "movies#add"
get "/movies/:id/edit", to: "movies#edit", as: "movie_edit"
put "/movies/:id", to: "movies#update"
patch "/movies/:id", to: "movies#update", as: "update_movie"
delete "/movies/:id", to: "movies#delete", as: "delete_movie"
end
Controller
class MoviesController < ApplicationController
def index
#movies = Movie.all
end
def movie_page
#movies = Movie.find(params[:id])
end
def new
#movies = Movie.new
end
def add
#movies = Movie.create(movie_params)
redirect_to movies_path
end
def edit
#movies = Movie.find(params[:id])
end
def update
#movies.update(movie_params)
redirect_to #movies, notice: "Shirt was updated."
end
def delete
#movies = Movie.find(params[:id])
#movies.destroy
# flash[:notice] = "Shirt was deleted."
redirect_to root_path, notice: "Shirt was deleted."
end
def movie_params
params.require(:movie).permit(:title, :description, :year_released)
end
# def set_movie
# #movies = Movie.find(params[:id])
# end
end
Form partial
<%= form_for #movies do |m| %>
<p>
<%= m.label :title %><br>
<%= m.text_field :title %>
</p>
<p>
<%= m.label :description %><br>
<%= m.text_field :description %>
</p>
<p>
<%= m.label :year_released %><br>
<%= m.text_field :year_released %>
</p>
<p>
<%= m.submit %>
</p>
<% end %>
Movie page html (individual movies, labeled by IDs)**I can't update or Delete, no route matches Delete.
When I press Update - I get param is missing or the value is empty: movie
<h1><%= #movies.title %></h1>
<h2>Released on : <%= #movies.year_released %> </h2>
<p> <%= #movies.description %> </p>
<%= link_to "Update", movie_edit_path(#movies) %>
<%= link_to "Delete", movies_path, method: :delete %
Edit page *I cant access this link. the form is the problem
<h1>Edit <%= #movies.title %> Info </h1>
<%= render "form" %>
<%= link_to "Cancel Edit", movie_edit_path(#movies) %>
Many thanks guys
def update
#movie = Move.find(params[:id])
#movie.update(movie_params)
redirect_to movie_path(#movie)
end
on your routes. all you need is resources :movies
you are getting param is empty because you have to pass in the id of the movie to update.
The major issue is that you do not load the variable #movies from the DB before you use it.
def update
#movies.update(movie_params)
redirect_to #movies, notice: "Shirt was updated."
end
def update
#movies.find(params[:id])
#movie.update(movie_params)
redirect_to #movies, notice: "Shirt was updated."
end
Aside from that you have tons of duplication and quite a few idiosyncrasies.
Rails uses these naming conventions for actions:
index
show (not movie_page)
new
create (not add)
edit
update
destroy (not delete)
You should follow them unless you have a damn good reason not to.
class MoviesController < ApplicationController
# cuts the duplication
before_filter :set_movie, except: [:new, :index]
def index
#movies = Movie.all
end
# GET /movies/:id
def show
end
# GET /movies/new
def new
#movie = Movie.new
end
# POST /movies
def create
#movie = Movie.create(movie_params)
redirect_to movies_path
end
# GET /movies/edit
def edit
end
# PUT|PATCH /movies/:id
def update
#movie.update(movie_params)
redirect_to #movie, notice: "Shirt was updated."
end
# DELETE /movies/:id
def destroy
#movie.destroy
redirect_to action: :index
end
private
def movie_params
params.require(:movie).permit(:title, :description, :year_released)
end
def set_movie
# Use the singular form when naming a variable with a single record
# failure to do so may result in tarring and feathering
#movie = Movie.find(params[:id])
end
end

Different notification partials for different models?

notifications/index has <%= render partial: "notifications/notification", collection: #notifications %>, which contains:
<%= link_to "", notifications_habit_path(notification.id), method: :delete, class: "glyphicon glyphicon-remove" %>
<%= link_to Comment.find_by(notification.comment_id).user.name, user_path(Comment.find_by(notification.comment_id).user.id) %>
commented on <%= link_to "your habit", habit_path(notification) %>
which shows:
This is problematic because it should say 3x ".com commented on your habit" and 2x ".com commented on your value".
We need to create two separate partials notifications/_habits & notifications/_values.
My confusion is how to make the code know when to direct to the habit partial or the value partial based on whether it's a habit or value.
notifications_controller
def index
#habits = current_user.habits
#valuations = current_user.valuations #aka values
#notifications = current_user.notifications
#notifications.each do |notification|
notification.update_attribute(:read, true)
end
The notifications are based on if a user comments on one of your habits or values:
comment.rb
class Comment < ActiveRecord::Base
after_save :create_notification
has_many :notifications
belongs_to :commentable, polymorphic: true
belongs_to :user
validates :user, presence: true
private
def create_notification
Notification.create(
user_id: self.user_id,
comment_id: self.id,
read: false
)
end
end
I followed this tutorial but it is based on using just one model: http://evanamccullough.com/2014/11/ruby-on-rails-simple-notifications-system-tutorial/
UPDATE FOR VALADAN
class CommentsController < ApplicationController
before_action :load_commentable
before_action :set_comment, only: [:show, :edit, :update, :destroy, :like]
before_action :logged_in_user, only: [:create, :destroy]
def index
#comments = #commentable.comments
end
def new
#comment = #commentable.comments.new
end
def create
#comment = #commentable.comments.new(comment_params)
if #comment.save
redirect_to #commentable, notice: "comment created."
else
render :new
end
end
def edit
#comment = current_user.comments.find(params[:id])
end
def update
#comment = current_user.comments.find(params[:id])
if #comment.update_attributes(comment_params)
redirect_to #commentable, notice: "Comment was updated."
else
render :edit
end
end
def destroy
#comment = current_user.comments.find(params[:id])
#comment.destroy
redirect_to #commentable, notice: "comment destroyed."
end
def like
#comment = Comment.find(params[:id])
#comment_like = current_user.comment_likes.build(comment: #comment)
if #comment_like.save
#comment.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Two many likes'
end
redirect_to(:back)
end
private
def set_comment
#comment = Comment.find(params[:id])
end
def load_commentable
resource, id = request.path.split('/')[1, 2]
#commentable = resource.singularize.classify.constantize.find(id)
end
def comment_params
params[:comment][:user_id] = current_user.id
params.require(:comment).permit(:content, :commentable, :user_id, :like)
end
end
Your notification is associated with comment, and comment can have commentable of type Habit or Value (you havent show those two model, so lets call them Habit and Value models).
So you can check if notification is for Habit or Value by checking commentable type like this:
Comment.find_by(notification.comment_id).commentable.class == Habit
or check if its value notification:
Comment.find_by(notification.comment_id).commentable.class == Value
Similar way is checking polymorphic type on the comment, like:
Comment.find_by(notification.comment_id).commentable_type == 'Habit'
So on the end, you dont actualy need two partials just IF and two different link_to, one for value and one for habit.
<%= link_to "", notifications_habit_path(notification.id), method: :delete, class: "glyphicon glyphicon-remove" %>
<%= link_to Comment.find_by(notification.comment_id).user.name, user_path(Comment.find_by(notification.comment_id).user.id) %> commented on
<% if Comment.find_by(notification.comment_id).commentable.class == Habit %>
<%= link_to "your habit", habit_path(notification) %>
<% else %>
<%= link_to "your value", value_path(notification) %>
<% end %>
I needed
<% if notification.habit_id %>
<%= link_to "your habit", habit_path(notification) %>
<% elsif notification.valuation_id %>
<%= link_to "your value", valuation_path(notification) %>
<% elsif notification.quantified_id %>
<%= link_to "your stat", quantified_path(notification) %>
<% elsif notification.goal_id %>
<%= link_to "your goal", goal_path(notification) %>
<% end %>
and in the comment model:
def create_notification
Notification.create(
habit_id: self.habit_id,
valuation_id: self.valuation_id,
quantified_id: self.quantified_id,
goal_id: self.goal_id,
user_id: self.user_id,
comment_id: self.id,
read: false
)
end

Resources