Home > OS >  Unable to get my edit button to work with form_with helper
Unable to get my edit button to work with form_with helper

Time:09-17

I'm attempting to create an Edit action with form_with in Rails.... My other CRUD actions are working ok, so something to do with my form_with...

<%= form_with(model: [@game, @review], local:true) do |form| %>

this is the tag im attempting to create, yet when I do i get this error

undefined method `review_path' for #<#<Class:0x00007fedfa8befd0>:0x00007fedfa8bd298>
Did you mean?  view_paths

directing me back to the tag, this is my controller

class ReviewsController < ApplicationController
  before_action :set_review, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except:[:index, :show]
  before_action :correct_user, only: [:edit, :update, :destroy]
  def index
    @reviews = Review.all
  end

  def show
    @review = Review.find(params[:id])
  end

  def new
   @game = Game.find(params[:game_id])
   @review = current_user.reviews.new
  end


  def edit
    review = @game.reviews.find(params[:id])
  end


  def create
    @review = current_user.reviews.new(review_params)
    @game = Game.find(params[:game_id])
    @review.game_id = @game.id

    respond_to do |format|
      if @review.save
        format.html { redirect_to game_reviews_path(@review), 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
  



  def update
    @review = current_user.reviews.update(review_params)
    @game = Game.find(params[:game_id])
    @review.game_id = @game.id

    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


  def destroy
    @review.destroy
    respond_to do |format|
      format.html { redirect_to game_reviews_path(@review), notice: 'Review was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def correct_user
    @review = current_user.reviews.find_by(id: params[:id])
    redirect_to reviews_path, notice:" Not Authorized to Edit This Review" if @review.nil?
    end

  private
    def set_review
      @review = Review.find(params[:id])
    end


    def review_params
      params.require(:review).permit(:reviewed_game, :rating, :game_id, :user_id)
    end
  end

my routes...

  devise_for :users
  resources :game_studios
  resources :games do
    resources :reviews
  end
  root to: "games#index"
end

Still pretty new to Rails, any help would be great!

Thanks

CodePudding user response:

def edit
end

private 
def set_review
  @game = Game.find params[:game_id] ####This is the key of fix
  @review = Review.find(params[:id])
end

You are not initializing @game instance in before_action So when you call <%= form_with(model: [@game, @review], local:true) do |form| %> @game is nil , due to which it cannot generate a valid route for this specific form.

So either set this @game instance variable in edit action or in before_action

  • Related