ruby-on-rails-5ruby-2.4

No route matches [POST] "/portfollios/new"


Hey Guys I am getting issue, No Route Matches, though I have created both new as well as create method.

portfollios_controller.rb

class PortfolliosController < ApplicationController
  def index
    @portfolio_items = Portfollio.all
  end

  def new
    @portfolio_item = Portfollio.new
  end

  def create
    @portfolio_item = Portfollio.new(params.require(:portfollio).permit(:title, :subtitle, :body))

    respond_to do |format|
      if @portfolio_item.save
        format.html { redirect_to portfollio_path, notice: 'Your portfolio item is now live.' }
      else
        format.html { render :new }
      end
    end
  end
end

routes.rb

Rails.application.routes.draw do
  resources :portfollios
end

new.html.erb

    <h1>Create a new Portfolio Item</h1>

<%= form_with(model: @portfolio_items, local: true) do |form| %>

  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>

  <div class="field">
    <%= form.label :subtitle %>
    <%= form.text_field :subtitle %>
  </div>

  <div class="field">
    <%= form.label :body %>
    <%= form.text_area :body %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

Can someone help me out with the issue. I am not able to figure it out.


Solution

  • I figured it out. The issue is with with the file new.html.erb

    I needed to change below line:-

    <%= form_with(model: @portfolio_items, local: true) do |form| %>
    

    to

    <%= form_with(model: @portfolio_item, local: true) do |form| %>
    

    It should be @portfolio_item not plural.