LoadError: Unable to autoload constant Review, Failure/Error: #review = Review.new - ruby

I am getting the following error in my tests in this Ruby project I am doing. I can't seem to figure out why.
Below is my controller.
class ReviewsController < ApplicationController
def new
#restaurant = Restaurant.find(params[:restaurant_id])
#review = Review.new
end
def create
#restaurant = Restaurant.find(params[:restaurant_id])
#restaurant.reviews.create(review_params)
end
private
def review_params
params.require(:review).permit(:thoughts, :rating)
end
end
Below is my view for the Reviews.
<%= form_for [#restaurant, #review] do |f| %>
<%= f.label :thoughts %>
<%= f.text_area :thoughts %>
<%= f.label :rating %>
<%= f.select :rating, (1..5) %>
<%= f.submit 'Leave Review' %>
<% end %>
I can't seem to figure out what is wrong.

The model for the Review is wrong. My model was referring to the wrong class.
class Restaurant < ApplicationRecord
belongs_to :restaurant
end
When it should have been the following.
class Review < ApplicationRecord
belongs_to :restaurant
end

Related

cant upload images rails and error occur

I want to upload an image to my localhost using gem 'carrierwave', i have painting and galleries controller like below code
painting controller
class PaintingsController < ApplicationController
def index
#paintings=Painting.all
end
def new
#painting=Painting.new
end
def show
#painting=Painting.find(params[:id])
end
def create
#byebug
#painting=Painting.new(painting_params)
if #painting.save
flash[:success]="Created image in album"
redirect_to gallery_path(#painting)
else
flash[:error]="Fail!"
render 'new'
end
end
private
def painting_params
params.require(:painting).permit(:name,:gallery_id)
end
end
Gallery controller
class GalleriesController < ApplicationController
def index
#galleries=Gallery.all
end
def new
#gallery=Gallery.new
end
def show
#gallery=Gallery.find(params[:id])
end
def create
#gallery=Gallery.create!(gallery_params)
redirect_to galleries_path
end
private
def gallery_params
params.require(:gallery).permit(:name)
end
end
ok then 2 model files :
gallery.rb
class Gallery < ApplicationRecord
has_many :paintings
end
painting.rb
class Painting < ApplicationRecord
def access_params
params.require(:painting).permit(:gallery_id, :name, :image)
end
belongs_to :gallery, optional: true
mount_uploader :image, ImageUploader
end
It seems everying goes well but then i stuck at the step showing image on show.html.erb in gallery.
show.html.erb
<div id="paintings">
<% #gallery.paintings.each do |painting| %>
<div class="painting">
<%= image_tag painting.image_url.to_s %>
<div class="name"><%= painting.name %></div>
<div class="actions">
<%= link_to "edit", edit_painting_path(painting) %> |
<%= link_to "remove", painting, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
<div class="clear"></div>
</div>
the image isn't showed up althought flash in gallery 's controller reported that i created the image,i inspected the website then i tried print painting s'attributes on show.html.erb
<%= #gallery.name %>
<%= #gallery.paintings.name %>
<%= #gallery.paintings.gallery_id%>
<%= #gallery.paintings.image%>
Only gallery's name and painting's name are printed out. other two methods has an error occur.
undefined method `gallery_id' for #<ActiveRecord::Associations::CollectionProxy []>
I don't know why gallery can only access to painting's name but not others two.I searched for this error but i dont think those situation apply to mine . What is the problem guys?
Your "paintings" is a collection, not a single image, so you need to either iterate on each of them or select the first one:
<%= #gallery.paintings.first.name %>
<%= #gallery.paintings.first.gallery_id %>
<%= #gallery.paintings.first.image %>
I found my error ! in user controller add :image to method painting_params
def painting_params
params.require(:painting).permit(:name,:gallery_id,:image)
end

Adding Categories In Rails 3

I am building an uploader for videos and giving the videos a category through categorizations. Every time I try to upload a video I receive an error saying
NameError in VideosController#create
uninitialized constant Video::Categorization
I want to able to add one category to each video. But no regardless of how I write the association I get the same error.
model
class Video < ActiveRecord::Base
attr_accessible :source, :title, :url, :description,
:category, :category_id, :category_list
belongs_to :user
has_many :category, through: :categorizations
has_many :categorizations
validates :category, presence: true
has_attached_file :source
def source_remote_url=(url_value)
self.source = URI.parse(url_value) unless url_value.blank?
super
end
def self.categorized_with(name)
Category.find_by_name!(name).videos
end
def category_list
["Action", "Anime",
"Arts and Culture", "Beauty", "Business", "Comedy",
"Documentary", "Drama",
"Food", "Gaming", "Health and Fitness", "Horror"]
end
def category_list=(names)
self.category = names.split(",").map do |n|
Category.where(name: n.strip).first_or_create!
end
end
end
Video Controller
class VideosController < ApplicationController
before_filter :authenticate_user!
before_filter :set_video, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#videos = Video.all
end
def show
respond_with(#video)
end
def new
#video = Video.new
respond_with(#video)
end
def edit
end
def create
#video = Video.new(params[:video])
#video.save
respond_with(#video)
end
def update
#video.update_attributes(params[:video])
respond_with(#video)
end
def destroy
#video.destroy
respond_with(#video)
end
private
def set_video
#video = Video.find(params[:id])
end
end
Form
<%= form_for(#video) do |f| %>
<% if #video.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% #video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :source %><br />
<%= f.file_field :source %>
</div>
<div class="field">
<%= f.label "Category", class: 'control-label' %>
<%#= f.select :category, Category.all, :prompt => 'Select One' %>
<%= f.select :category_list, video_category, :prompt => "Select a category..." %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, rows: 4, placeholder: "Description" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
If you declare
has_many :category, through: :categorizations
it should be
has_many :categories, through: :categorizations

Ruby on Rails guide (blog) error in 5.10

I'm learning Ruby on Rails and to begin I'm making this little blog application via http://guides.rubyonrails.org/getting_started.html#showing-articles .
Now I'm at 5.10 where I need to add validation to the form so if the user adds a tittle with a length shorter than 5.
So this is my articles_controller.rb:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def show
#article = Article.find(params[:id])
end
def new
end
def create
#render plain: params[:article].inspect
##article = Article.new(params[:article])
#article = Article.new(article_params)
##article.save
if #article.save
redirect_to #article
else
render 'new'
end
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
And in this view I have an error (new.html.erb):
<%= form_for :article, url: articles_path do |f| %>
<% if #article.errors.any? %>
<% end %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
This is the error I get:
I'm new to Ruby and rails so I hope I can get some help.
You didn't initialize #article instance variable, but you try tu use it in new view. You should have:
def new
#article = Article.new
end

Can not create user with Rails app on heroku, but works perfect on localhost

So I am working through the Michael Hartl tut and this app works perfectly on the localhost but the moment I deploy to heroku it wont create a user when i submit the information. In fact it just sits there as if I just clicked on an empty screen, no error message nor a rediret. I looked at the heroku logs and there are no exceptions that I can see being logged. I tried updating the controller behavior but i get the same result. This is frustrating.
my form looks like this:
<div class="main-form">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages'%>
<%= f.label :name %>
<%= f.text_field :name, class: "active" %><br/>
<%= f.label :email %>
<%= f.text_field :email %><br/>
<%= f.label :password %>
<%= f.password_field :password %><br/>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %><br/>
</div>
<div class="actions">
<%= f.submit "create my account", class: "btn btn-lg btn-default" %>
</div>
<% end %>
my controller looks like:
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
#title = #user.name
end
def create
#title = "welcome"
#user = User.new(user_params)
if #user.password_confirmation.empty? == false
#user.save
redirect_to user_path(#user)
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
i have also tried setting up my create method like:
def create
#user = User.new(user_params)
if #user.save
redirect_to #user
else
render 'new'
end
end
neither of these methods worked? Any advice would be welcome.
If you inspect the page it looks like your submit button is outside of the form definition:

undefined method `bookings_path'

I have a form to fill in booking details, but I'm getting the following error:
NoMethodError in Bookings#new
undefined method `bookings_path'
This happened after change the routes.rb file, to nest the booking resources in the user resource.
My form file code is the following:
<% provide(:title, 'Book Now!') %>
<section id="book-now">
<%= form_for(#booking) do |f| %>
<header>
<h1>Edit booking</h1>
</header>
<%= f.text_field :name, placeholder: "Name" %>
<%= f.text_field :check_in, placeholder: "Check-in" %>
<%= f.text_field :check_out, placeholder: "Check-out" %>
<%= f.submit "Save" %>
<% end %>
</section>
My booking controller code is:
class BookingsController < ApplicationController
def new
#booking = Booking.new
end
def create
#booking = Booking.new(params_bookings)
#booking.user_id ||= current_user.id
if #booking.save
redirect_to user_path(#booking.user_id)
else
# render 'new'
end
end
end
private
def params_bookings
params.require(:booking).permit(:check_in, :check_out, :name, :user_id)
end
end
and my routes.rb file looks like this:
Hightide::Application.routes.draw do
resources :users do
resources :bookings
end
match '/users/:user_id/bookings/new', to: 'bookings#new', via: [:post, :get]
you have this error because your bookings routes are nested to the users, so when you write this
form_for #booking
you essentially call
form_for bookings_path, ...
coz rails gets the type of the object you send to form_for and tries to get vanilla path for it.
to solve the problem you need either create the vanilla bookings resources in your routes, or specify both a user and a booking reference for the form_for call, like so
form_for [current_user, #booking]

Resources