Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:05:21+00:00 2026-06-10T19:05:21+00:00

I’m using namespaced models to assign different types of services to a platform :

  • 0

I’m using namespaced models to assign different types of services to a platform :

Relation : Platform has_many :services & Service belongs_to :platform

Inhéritance : Service::Service1 < Service, Service::Service2 < Service, etc.

Each service type has a different set of fields (I’m using Mongoid so fields are declared into the model and “submodel” type is stored in a _type field).

In ServicesController::new, I instanciate new services like this :

def new
  klass    = "service/#{params[:type]}"
  @service = klass.camelize.constantize.new 
  render :form
end

params[:type] is given by a route parameter (something like /:platform_id/services/new/:type)

In my form view, I can now display fields depending on the service type (it’s Haml + simple_form) :

= simple_form_for @service do |f|

[...]

  - case @service.class

  - when Service::Service1
    = f.input :field1

[...]

Everything works fine until now : Generated HTML inputs looks like this :

<input class="string required" id="service_service1_field1" name="service_service1[field1]" size="50" type="text">

With service_service1[field1] as field name, my params hash will contain different keys depending on the sub-service and, in my controller’s create action, I’ll have to write a case/when for each sub-service, while service[field1] would have “DRYed” my code (I’d just have to add a hidden field with the exact model to instantiate).

Is there a way to force Rails to use the root class as field name when using namespaced models ?

Edit : Same question for submit button i18n : Rails looks for helpers.submit.service_service1.create where I’d like it to look for helpers.submit.service.create as the text is the same for all services.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:05:22+00:00Added an answer on June 10, 2026 at 7:05 pm

    Have you tried the :as option? I have used this for sub classes but not for name spaces. You may have to do some additional gymnastics with the :url option to get your form to post to the correct location.

    If you have an object that needs to be represented as a different parameter, 
    like a Person that acts as a Client:
    
    <%= form_for(@person, :as => :client) do |f| %>
      ...
    <% end %>
    

    Taken from the Rails docs

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.