Reputation: 155
I am trying to save form data by form_for new.erb.html
<%= form_for @project do |f| %>
<h4>
<label for = "projectName">Title(required)</label>
<%= f.text_field :projectName, :maxlength => 50 %>
</h4>
<%= f.submit %>
<% end -%>
ProjectsController
class Com::A::B::C::ProjectsController < ApplicationController
def index
@projects = Project.all
end
def new
@project = Project.new
@allTags = Tag.all
@allBenefits = Benefit.all
end
def create
@project = Project.new(project_params)
if @project.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def project_params
params.require(:project).permit(:projectName)
end
But nothings getting saved on the database
html tags for text_fiels looks like,
<input id="com_a_b_c_project_projectName" type="text" name="com_a_b_c_project[projectName]" size="50" maxlength="50">
I tried adding url=>"com_a_b_c_project_path"
still nothing persists
I am new to rails in ruby, so any help would be nice.
Routes
namespace :com do
namespace :a do
namespace :b do
namespace :c do
get 'projects/index'
resources :projects do
end
end
end
end
end
I get following exception:
param is missing or the value is empty: project
Request
Parameters:
"utf8"=>"✓",
"authenticity_token"=>"",
"com_a_b_c_project"=>{"projectName"=>"s"
"commit"=>"Create Project"
rake rotes
com_a_b_c_projects_index GET /com/a/b/c/projects/index(.:format) com/a/b/c/projects#index com_a_b_c_projects GET /com/a/b/c/projects(.:format) com/a/b/c/projects#index POST /com/a/b/c/projects(.:format) com/a/b/c/projects#create new_com_a_b_c_project GET /com/a/b/c/projects/new(.:format) com/a/b/c/projects#new edit_com_a_b_c_project GET /com/a/b/c/projects/:id/edit(.:format) com/a/b/c/projects#edit com_a_b_c_project GET /com/a/b/c/projects/:id(.:format) com/a/b/c/projects#show PATCH /com/a/b/c/projects/:id(.:format) com/a/b/c/projects#update PUT /com/a/b/c/projects/:id(.:format) com/a/b/c/projects#update DELETE /com/a/b/c/projects/:id(.:format) com/a/b/c/projects#destroy
NOTE I can save data with form_tag But not with form_for... However I need to use form_for otherwise I have to bind object to each field
<%= form_tag :action => 'create', :controller => 'projects' do %>
<h4><label for = "projectName">Title(required)</label> <%= text_field 'project', 'projectName'> </h4>
Cheers
Upvotes: 1
Views: 164
Reputation: 815
In your controller, you are requiring a 'project' param by the line:
params.require(:project).permit(:projectName)
But looking at the request, the param being sent is 'com_a_b_c_project'.
If you change it to:
params.require(:com_a_b_c_project).permit(:projectName)
all should be well.
Upvotes: 1
Reputation: 7956
The Rails docs provide information on using form_for
with namespacing in section 2.3.1:
If you have created namespaced routes,
form_for
has a nifty shorthand for that too. If your application has an admin namespace then
form_for [:admin, @article]
will create a form that submits to the
ArticlesController
inside the admin namespace (submitting toadmin_article_path(@article)
in the case of an update). If you have several levels of namespacing then the syntax is similar:
form_for [:admin, :management, @article]
So in your case it looks like it would be:
form_for [:com, :a, :b, :c, @project]
Upvotes: 2