1

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

16
  • If it returns error, show us. And also show your routes because you have namespace Com::A::B::C:: in your controller. Commented Apr 7, 2016 at 1:13
  • what routes are you define? Commented Apr 7, 2016 at 1:27
  • I get following exception: param is missing or the value is empty: project app/controllers/com/a/b/c/projects_controller.rb:59:in project_params' app/controllers/com/a/b/cp/projects_controller.rb:20:in create' Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"", "com_a_b_c_project"=>{"projectName"=>"s" "commit"=>"Create Project"} Commented Apr 7, 2016 at 1:29
  • Routes namespace :com do namespace :a do namespace :b do namespace :c do get 'projects/index' resources :projects do end end end end end Commented Apr 7, 2016 at 1:29
  • 1
    As an aside, you might start out with using snake_case for model attribute names instead of camelcase, and note that this is probably not the only namespace issue you will have. Welcome to rails and good luck Commented Apr 7, 2016 at 2:31

2 Answers 2

2

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 to admin_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]

Sign up to request clarification or add additional context in comments.

3 Comments

This is the proper way:)
This throws me error undefined method com_a_b_c_com_a_b_c_projects_path' for #<#<Class:0x9238530>:0x922d7d8> actionpack (4.2.5.2) lib/action_dispatch/routing/polymorphic_routes.rb:220:in polymorphic_method'
def project_params params.require(:project).permit(:projectName, :briefDesc, :whatSection, :challengers, :status, :valueProposal, :maturityLevel,:tag_ids =>[])#tag_ids: [] end
1

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.

1 Comment

not trying to be a sore sport or anything, but it's probably better to mark my answer correct since it uses the proper style as indicated by the rails docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.