没有路由匹配[POST]“ / portfollios / new”

时间:2018-07-06 18:30:51

标签: ruby-on-rails-5 ruby-2.4

嘿,我遇到了问题,没有路线匹配,尽管我同时创建了newcreate方法。

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 %>

有人可以帮我解决这个问题吗?我无法弄清楚。

1 个答案:

答案 0 :(得分:0)

我知道了。问题出在文件new.html.erb

我需要更改以下行:-

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

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

它应该是@portfolio_item,而不是复数。