嘿伙计们,我遇到了问题,没有路由匹配,尽管我已经创建了new
和create
方法。
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
路线.rb
Rails.application.routes.draw do
resources :portfollios
end
新.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 %>
有人可以帮助我解决这个问题吗?我无法弄清楚。
我想通了。问题出在文件new.html.erb
我需要更改以下行:-
<%= form_with(model: @portfolio_items, local: true) do |form| %>
自
<%= form_with(model: @portfolio_item, local: true) do |form| %>
它应该是@portfolio_item而不是复数。