加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-4 – 未定义的方法`permit’表示“立即提交! “

发布时间:2020-12-17 03:23:47 所属栏目:百科 来源:网络整理
导读:我一直在努力尝试使用其他一些StackOverflow帖子解决这个许可错误,但似乎无法超越它.我有一个项目模型控制器版本模型控制器. Projects / ## / versions / new是一个表单页面,用于创建项目标识##的新版本.但是当我单击提交按钮来创建版本时…它会在VersionsCo
我一直在努力尝试使用其他一些StackOverflow帖子解决这个许可错误,但似乎无法超越它.我有一个项目模型&控制器&版本模型&控制器. Projects / ## / versions / new是一个表单页面,用于创建项目标识##的新版本.但是当我单击提交按钮来创建版本时…它会在VersionsController中抛出以下错误:

undefined method `permit' for "Submit Now! ":String

Extracted source (around line #36):
34
35
36
37
38


    def version_params
      params.require(:version).permit(:title)
    end
end

任何和所有的帮助将不胜感激…我一直在努力解决这个问题太久了.我的守则如下:

的routes.rb

resources :users
  resources :sessions,only: [:new,:create,:destroy]
  resources :projects,only: [:create,:new,:show,:edit,:update,:destroy]

  resources :projects do
    resources :versions
  end

  # get "static_pages/home"
  # get "static_pages/help"
  # get "static_pages/about"
  #The original routes above map to...
  root  'static_pages#home'
  match '/signup',to: 'users#new',via: 'get'
  match '/signin',to: 'sessions#new',via: 'get'
  match '/signout',to: 'sessions#destroy',via: 'delete'
  match '/help',to: 'static_pages#help',via: 'get'
  match '/about',to: 'static_pages#about',via: 'get'
  match '/contact',to: 'static_pages#contact',via: 'get'

项目模型:

class Project < ActiveRecord::Base
  has_many :users
  has_many :versions,dependent: :destroy
  validates :title,presence: true,length: { maximum: 100 }
  validates :background,presence: true
  validates :user_id,presence: true

  default_scope -> { order('created_at DESC') }
end

版本模型:

class Version < ActiveRecord::Base
  belongs_to :project
  validates :title,length: { maximum: 140 }

  default_scope -> { order('created_at DESC') }
end

版本控制器:

class VersionsController < ApplicationController
  def new
    @version = Version.new
  end

  def show
    @project = Project.find(params[:project_id])
    @version = Version.find(params[:id])
  end

  def index
    @versions = Version.paginate(page: params[:page])
  end

  def create
    @project = Project.find(params[:project_id])
    @version = @project.versions.create(version_params)
    if @version.save
      flash[:success] = "You've successfully added a version to this branch..."
      redirect_to project_path(@project)
    else
      render 'new'
    end
  end

  def edit

  end

  def update

  end

  def destroy

  end

  private

    def version_params
      params.require(:version).permit(:title)
    end
end

NEW.HTML.ERB(新版本表格):

<% provide(:title,'New Version') %>
<h1>Add a version to this project</h1>

<div class="row-fluid">
  <div class="col-md-5 no-pad offset3">
    <%= bootstrap_form_for @version,:url => project_versions_path do |f| %>

      <%= render 'shared/error_messages',object: f.object %>

      <%= f.text_field :title %>

      <br clear="all">

      <%= f.submit "Submit Now! ",class: "btn btn-lg btn-primary" %>
    <% end %>
  </div>
</div>

PARAMS:

{"utf8"=>"?","authenticity_token"=>"######AAAA","submit"=>"Submit Now! ","project_id"=>"51"}

Processing by VersionsController#create as HTML
  Parameters: {"utf8"=>"?","authenticity_token"=>"################=","version"=>"Submit Now! ","project_id"=>"51"}
  Project Load (0.3ms)  SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? ORDER BY created_at DESC LIMIT 1  [["id","51"]]
Completed 500 Internal Server Error in 3ms

NoMethodError (undefined method `permit' for "Submit Now! ":String):
  app/controllers/versions_controller.rb:41:in `version_params'
  app/controllers/versions_controller.rb:17:in `create'

解决方法

我能认识到参数中的问题.你有这个:

{"utf8"=>"?","project_id"=>"51"}

你应该这样:

{"utf8"=>"?","project_id"=>"51","version"=>{"title"=>"Foo Bar"},"button"=>""}

这是一个问题的原因是因为您没有在参数中传递版本标题,并且您正在尝试使用参数创建新版本.它反而寻找最接近的东西,在这种情况下碰巧是字符串“立即提交!”,但由于“提交”是不允许的,因此强大的参数将其抛出.

看起来您正在正确创建表单,这可能是bootstrap_form_for的问题.你可以在表单的html上发布标题的输入输出吗?

与此同时,我有两个建议,
可以解决问题的第一件事是将f.submit改为f.button.
f.button仍然会创建一个提交按钮,但它允许你按照你想要的方式命名.

同样在控制器中,调用create后无需保存. create实际上会将它存储在数据库中,因此您将其保存两次.你应该调用new而不是create

@version = @project.versions.new(version_params)
if @version.save

检查新记录

@version = @project.versions.create(version_params)
unless @version.new_record?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读