I've been trying like crazy to work through this permit
error using some of the other StackOverflow postings, but can't seem to get past it. I've got a projects model & controller & a versions model & controller. Projects/##/versions/new
is a form page to create a new version of project id ##. But when I click the submit button to create the version...it throws the following error in the 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
Any and all help would be greatly appreciated...I've been trying to fix this for too long now. My Code is as follows:
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'
PROJECTS MODEL:
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 (new version form):
<% 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'
I can recognize the problem in the params. You have this:
{"utf8"=>"✓",
"authenticity_token"=>"######AAAA",
"submit"=>"Submit Now! ",
"project_id"=>"51"}
You should have this:
{"utf8"=>"✓",
"authenticity_token"=>"######AAAA",
"project_id"=>"51",
"version"=>{"title"=>"Foo Bar"},
"button"=>""}
The reason this is a problem is because you do not have a version title being passed in the params, and you are trying to create a new version with the params. It instead looks for the closest thing which in this case happens to be the string "Submit Now!", but since "submit" is not permitted than strong params tosses it out.
It looks like you are creating your form correctly, it may be an issue with bootstrap_form_for. Can you post what the input output for title looks like in html on your form?
In the meantime I have two suggestions, First thing that may solve the problem, is to change f.submit to f.button. f.button will still create a submit button, but it allows you to name is in the way that you are trying to.
Also in the controller, you don't need to save after you call create. create will actually store it in the database, so you are saving it twice. You should either call new instead of create
@version = @project.versions.new(version_params)
if @version.save
of check if new record
@version = @project.versions.create(version_params)
unless @version.new_record?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With