2 years ago
#43439
nakakapagpabagabag
NoMethodError in Reviews#new after clicking on a link to create review page
- Error happens after clicking the
Submit a Review
link from restaurants/show.html.erb
Showing ...app/views/reviews/new.html.erb where line #1 raised:
undefined method `reviews_path' for #<ActionView::Base:0x00000000006c20>
1: <%= simple_form_for [@restaurant, @review] do |f| %>
2: <%= f.input :rating %>
3: <%= f.input :content %>
4: <%= f.submit class: "btn btn-primary" %>
5: <% end %>
- Schema
ActiveRecord::Schema.define(version: 2022_01_11_104035) do
create_table "restaurants", force: :cascade do |t|
t.string "name"
t.string "address"
t.string "phone_number"
t.string "category"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "reviews", force: :cascade do |t|
t.integer "rating"
t.text "content"
t.integer "restaurant_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["restaurant_id"], name: "index_reviews_on_restaurant_id"
end
add_foreign_key "reviews", "restaurants"
end
- Routes
Rails.application.routes.draw do
root to: 'restaurants#index'
resources :restaurants, only: [ :index, :new, :create, :show ] do
resources :reviews, only: [ :new, :create ]
end
end
Prefix Verb URI Pattern Controller#Action
root GET / restaurants#index
restaurant_reviews POST /restaurants/:restaurant_id/reviews(.:format) reviews#create
new_restaurant_review GET /restaurants/:restaurant_id/reviews/new(.:format) reviews#new
restaurants GET /restaurants(.:format) restaurants#index
POST /restaurants(.:format) restaurants#create
new_restaurant GET /restaurants/new(.:format) restaurants#new
restaurant GET /restaurants/:id(.:format) restaurants#show
- Controllers
class RestaurantsController < ApplicationController
def index
@restaurants = Restaurant.all
end
def new
@restaurant = Restaurant.new
end
def create
restaurant = Restaurant.new(strong_params)
restaurant.category.downcase!
if restaurant.save
redirect_to restaurants_path
else
render new_restaurant_path
end
end
def show
@restaurant = Restaurant.find(params[:id])
end
private
def strong_params
params.require(:restaurant).permit(:name, :address, :phone_number, :category)
end
end
class ReviewsController < ApplicationController
def new
@review = Review.new
end
def create
@review = Review.new(strong_params)
@review.restaurant = @restaurant
if @review.save
redirect_to restaurant_path(@restaurant)
else
render :new
end
end
private
def strong_params
params.require(:review).permit(:rating, :content)
end
end
- Views
<%# restaurants/show.html.erb %>
<div class="show-resto-container ">
<h1 class="page-headers">
<%= @restaurant.name %>'s Reviews
<%= link_to new_restaurant_review_path(@restaurant), class: "btn btn-primary .btn-lg" do %>
<i class="fas fa-list-ul"></i> Submit a Review
<% end %>
</h1>
<div class="resto-reviews-container">
<ul class="list-group list-group-flush">
<% @restaurant.reviews.each do |review| %>
<li class="list-group-item">
<p><%= review.print_stars %></p>
<p><%= review.content %></p>
</li>
<% end %>
</ul>
</div>
</div>
<%# reviews/new.html.erb %>
<%= simple_form_for [@restaurant, @review] do |f| %>
<%= f.input :rating %>
<%= f.input :content %>
<%= f.submit class: "btn btn-primary" %>
<% end %>
ruby-on-rails
ruby
ruby-on-rails-6.1
0 Answers
Your Answer