这是我的路线.rb
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'maincontroller#mainview', as: 'mainview'
get 'additem' => 'maincontroller#additem', as: 'additem'
resources :maincontroller
end
这是我的控制器
class MaincontrollerController < ApplicationController
def item
@post = Post.find(params[:id])
end
def additem
@post = Post.new(post_params)
@post.save
redirect_to @post
end
private
def post_params
params.require(:post).permit(:item_name, :item_desc)
end
end
这是我的 additem.html.erb
<h1>Item</h1>
<%= form_for :post, url: additem_path do |f| %>
<p>
<%= f.label :Item_Name %><br>
<%= f.text_field(:Item_Name, {:class => 'form-control'}) %>
</p>
<p>
<%= f.label :Item_Description %><br>
<%= f.text_area(:item_desc, {:class => 'form-control'}) %>
</p>
<p>
<%= f.submit({:class => 'btn btn-primary'})%>
</p>
<%end%>