我的创建方法正常运行时遇到问题。
这是我的 new.html.erb 文件
<div class="col-md-8">
<%= form_for @wiki do |f| %>
<%= f.label :title, class: 'form-control' %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter wiki title" %><br><br>
<%= f.label :body, class: 'form-control'%>
<%= f.text_area :body, class: 'form-control', placeholder: "Enter wiki body" %><br><br>
<%= f.check_box :private %> Private Topic<br><br>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>
</div>
Controller
def new
@wiki = Wiki.new
end
def create
@wiki = Wiki.create(params[:wiki])
if @wiki.save
flash[:notice] = "Wiki has been saved"
redirect_to wiki_index_path
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :private)
end
routes.rb
Rails.application.routes.draw do
devise_for :users
resources :wiki
post 'wiki/new'
post 'wiki/create'
resources :users
root 'welcome#index'
end
如果我在 new.html.erb 上的 form_for 中使用 :wiki ,我可以通过该错误,但是当单击实际新页面上的“保存”按钮时,根本不会发生任何事情(因为我没有将信息保存到正确的变量)。但是,一旦我使用 @wiki 变量,我就会收到错误
"undefined method `wikis_path' for #<#<Class:0x007f8f794e5d58>:0x007f8f7bb2ef50>"
感谢任何帮助!
最佳答案
您可以摆脱多余的不稳定的维基路线。 资源:wikis
就足够了:
Rails.application.routes.draw do
devise_for :users
resources :wikis
resources :users
root 'welcome#index'
end
有件事告诉我,改变之后一切都会好起来的。
关于ruby-on-rails - 未定义的方法 `wikis_path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41726686/