未定义的方法`wikis_path' [英] undefined method `wikis_path'

查看:50
本文介绍了未定义的方法`wikis_path'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 create 方法无法正常运行时遇到问题.

这是我的 new.html.erb 文件

 

<%= 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 %>私人话题<br><br><%= f.submit "Save", class: 'btn btn-success' %><%结束%>

控制器

 def new@wiki = Wiki.new结尾定义创建@wiki = Wiki.create(params[:wiki])如果@wiki.saveflash[:notice] = "Wiki 已保存"重定向到 wiki_index_path结尾结尾私人的定义 wiki_paramsparams.require(:wiki).permit(:title, :body, :private)结尾

routes.rb

Rails.application.routes.draw 做设计为:用户资源:维基发布维基/新"发布维基/创建"资源:用户根'欢迎#index'结尾

如果我在 new.html.erb 上的 form_for 中使用 :wiki 我可以通过错误但是当单击实际新页面上的保存按钮时,什么也没有发生(因为我没有将信息保存到正确的变量).但是,一旦我使用@wiki 变量,我就会收到错误

"未定义方法`wikis_path' for #<#:0x007f8f7bb2ef50>"

感谢任何帮助!

解决方案

你可以摆脱多余的不稳定 wiki 路由.resources :wikis 就足够了:

Rails.application.routes.draw 做设计为:用户资源:维基资源:用户根'欢迎#index'结尾

有些东西告诉我,改变之后一切都会好起来的.

I am having issues getting my create method to function correctly.

Here is my new.html.erb file

  <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

If I use :wiki in my form_for on new.html.erb I can get passed the error but when clicking on my save button on the actual new page, nothing happens at all (since I'm not saving the information to the correct variable). However, as soon as I use the @wiki variable, I get the error

"undefined method `wikis_path' for #<#<Class:0x007f8f794e5d58>:0x007f8f7bb2ef50>"

Any help is appreciated!

解决方案

You can get rid of redundant unrestful wiki routes. resources :wikis will be enough:

Rails.application.routes.draw do
  devise_for :users
  resources :wikis
  resources :users
  root 'welcome#index'
end

And something tells me that all will be fine after that change.

这篇关于未定义的方法`wikis_path'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆