我一直在努力尝试使用其他一些StackOverflow帖子解决此permit
错误,但似乎无法通过它。我有一个项目模型&控制器&版本模型&控制器。 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
VERSIONS MODEL:
class Version < ActiveRecord::Base
belongs_to :project
validates :title, presence: true, length: { maximum: 140 }
default_scope -> { order('created_at DESC') }
end
VERSIONS CONTROLLER:
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'
答案 0 :(得分:6)
我能认识到参数中的问题。你有这个:
{"utf8"=>"✓",
"authenticity_token"=>"######AAAA",
"submit"=>"Submit Now! ",
"project_id"=>"51"}
你应该有这个:
{"utf8"=>"✓",
"authenticity_token"=>"######AAAA",
"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?