0

Я проверил другие сообщения simple_form, и они не сильно ударили по моей проблеме. У меня есть вложенное ресурс для обзоры ресторанов в моем routes.rb здесь:simple_form предоставляет ошибку undefined method `reviews_path 'для # <# для вложенного маршрута в Rails 4

Rails.application.routes.draw do 
    devise_for :users 
    resources :restaurants do 
    resources :reviews, except: [:show, :index] 
    end 

Мой контроллер обзор, кажется, установлен правильно здесь:

class ReviewsController < ApplicationController 
    before_action :authenticate_user! 
    before_action :set_restaurant 
    before_action :set_review, only: [:edit, :update, :destroy] 

    # GET /reviews/new 
    def new 
    @review = Review.new 
    end 

    # GET /reviews/1/edit 
    def edit 
    end 

    # POST /reviews 
    # POST /reviews.json 
    def create 
    @review = Review.new(review_params) 
    @review.user_id = current_user.id 
    @review.restaurant_id = @restaurant.id 

    respond_to do |format| 
     if @review.save 
     format.html { redirect_to root_path, notice: 'Review was successfully created.' } 
     format.json { render :show, status: :created, location: @review } 
     else 
     format.html { render :new } 
     format.json { render json: @review.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /reviews/1 
    # PATCH/PUT /reviews/1.json 
    def update 
    respond_to do |format| 
     if @review.update(review_params) 
     format.html { redirect_to @review, notice: 'Review was successfully updated.' } 
     format.json { render :show, status: :ok, location: @review } 
     else 
     format.html { render :edit } 
     format.json { render json: @review.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /reviews/1 
    # DELETE /reviews/1.json 
    def destroy 
    @review.destroy 
    respond_to do |format| 
     format.html { redirect_to reviews_url, notice: 'Review was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_review 
     @review = Review.find(params[:id]) 
    end 
    def set_restaurant 
     @restuarant = Restaurant.find(params[:restaurant_id]) 
    end 
    # Never trust parameters from the scary internet, only allow the white list through. 
    def review_params 
     params.require(:review).permit(:rating, :comment) 
    end 
end 

Мой контроллер ресторан здесь:

class RestaurantsController < ApplicationController 
    before_action :set_restaurant, only: [:show, :edit, :update, :destroy] 

    # GET /restaurants 
    # GET /restaurants.json 
    def index 
    @restaurants = Restaurant.all 
    end 

    # GET /restaurants/1 
    # GET /restaurants/1.json 
    def show 
    end 

    # GET /restaurants/new 
    def new 
    @restaurant = Restaurant.new 
    end 

    # GET /restaurants/1/edit 
    def edit 
    end 

    # POST /restaurants 
    # POST /restaurants.json 
    def create 
    @restaurant = Restaurant.new(restaurant_params) 

    respond_to do |format| 
     if @restaurant.save 
     format.html { redirect_to @restaurant, notice: 'Restaurant was successfully created.' } 
     format.json { render :show, status: :created, location: @restaurant } 
     else 
     format.html { render :new } 
     format.json { render json: @restaurant.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /restaurants/1 
    # PATCH/PUT /restaurants/1.json 
    def update 
    respond_to do |format| 
     if @restaurant.update(restaurant_params) 
     format.html { redirect_to @restaurant, notice: 'Restaurant was successfully updated.' } 
     format.json { render :show, status: :ok, location: @restaurant } 
     else 
     format.html { render :edit } 
     format.json { render json: @restaurant.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /restaurants/1 
    # DELETE /restaurants/1.json 
    def destroy 
    @restaurant.destroy 
    respond_to do |format| 
     format.html { redirect_to restaurants_url, notice: 'Restaurant was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_restaurant 
     @restaurant = Restaurant.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def restaurant_params 
     params.require(:restaurant).permit(:name, :address, :phone, :website, :image) 
    end 
end 

и мой simple_form_for является супер прямым:

<%= simple_form_for [@restaurant, @review] do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :rating %> 
    <%= f.input :comment %> 
    </div> 

    <div class="form-actions"> 
    <%= f.button :submit %> 
    </div> 
<% end %> 

Так что, я не знаю, почему я получаю эту ошибку :(

+0

добавьте полную ошибку в журнал, пожалуйста. – sevenseacat

+0

Можете ли вы показать, как выглядит ваш 'новый' метод в контроллере? Ошибка неопределенного метода 'review_path' происходит с новой страницы, на которой вы показываете форму. Если ваш метод 'create' не имеет redirect_to @review. – kittyminky

+0

Хотя это, вероятно, ваша опечатка в '@ restaurant', которая вызывает проблему, в любом контроллере. – sevenseacat

ответ

1

Вы должны предоставить значение @restaurant, которое так как вы mispelled как @restuarant не будет работать.

+1

Как глупо со мной, спасибо! Я думаю, что, должно быть, я слишком долго смотрел на экран –