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

  • SEARCH
  • Home
  • 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 350321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:32:23+00:00 2026-05-12T11:32:23+00:00

I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class

  • 0

I have the following models:

class FieldEntryValue < ActiveRecord::Base
  belongs_to :field_entry
end

and

class FieldEntry < ActiveRecord::Base
  belongs_to :field
  belongs_to :media
  has_many :field_entry_values, :dependent => :destroy
  accepts_nested_attributes_for :field_entry_values, :allow_destroy => true
end

The FieldEntriesController has the following methods:

  def new
    @field_entry = FieldEntry.new
    session[:media_id] = params[:media_id]
    session[:field_id] = params[:field_id]
    @field_entry.field = Field.find(params[:field_id])
    generate_fields(true)
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @field_entry }
    end
  end

  def create
    @field_entry = FieldEntry.new(params[:field_entry])
    @field_entry.media_id = session[:media_id]
    @field_entry.field_id = session[:field_id]
    generate_fields(false)
    respond_to do |format|
      if @field_entry.save
        flash[:notice] = 'FieldEntry was successfully created.'
        format.html { redirect_to(@field_entry) }
        format.xml  { render :xml => @field_entry, :status => :created, :locati$
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @field_entry.errors, :status => :unprocess$
      end
    end
  end

  def generate_fields(do_build)
    @ftype = @field_entry.field.field_type.name
    if @ftype == 'string'
      @field_entry.field_entry_values.build if do_build
      @field_entry.field_entry_values[0].key='name'
    elsif @ftype == 'url'
      2.times{ @field_entry.field_entry_values.build } if do_build
      @field_entry.field_entry_values[0].key='name'
      @field_entry.field_entry_values[1].key='url'
    elsif @ftype == 'imdb'
      1.times{ @field_entry.field_entry_values.build } if do_build
      @field_entry.field_entry_values[0].key='value'
    end
  end

And I have a partial that is loaded by new and edit:

<% form_for(@field_entry) do |f| %>
  <%= f.error_messages %>

  <% f.fields_for :field_entry_values do |fv_f| %>
    <p>
    <%= fv_f.label :key %><br />
    <%= fv_f.text_field :value %>
    </p>
  <% end %>

  <p>
    <%= f.submit 'Save' %>
  </p>
<% end %>

The view should show a variable count fields depending on the type and with a given attribute key as label and value for the data.

For the type string as an example I need a label “Name” (that should be the key) and value

This works mostly except "fv_f.label :key" shows the string key and not the value of the key-field.

  1. How can I show the value of @field_entry.field_entry_values.key as label in the view?

  2. How can I make the Controller better/shorter. As an example, do I really need to set @field_entry.field_entry_values[0].key='name' in the new and the create method to to get it displayed in the view and saved in the database?

thanx for suggestions 😉 I am a Rails newby I hope I did it not too wrong 😉

  • 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-05-12T11:32:23+00:00Added an answer on May 12, 2026 at 11:32 am

    I found a better solution for my Problem 1, in rails 2.3 there is a object attribute for the fields_for loop target to get the object of the field:

    <% form_for(@field_entry) do |f| %>
      <%= f.error_messages %>
        <% f.fields_for :field_entry_values do |fv_f| %>
        <p>
          <%= fv_f.label :value, fv_f.object.key%>
          <br />
          <%= fv_f.text_field :value %>
          <%= fv_f.hidden_field :key %>
        </p>
      <% end %>
      <p>
        <%= submit_tag %>
      </p>
    <% end %>
    

    Point 2 can not be improved much, maybe the generate_fields method could be loaded as a filter. But other than that, I don´t see anything at the moment, at least with this database structure.

    But thanks for the help: so1o, at least the thing with hidden_field was helpfully.

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

Sidebar

Related Questions

I have the following models: class Price < ActiveRecord::Base belongs_to :product end class Metric
I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have the following models class Book < ActiveRecord::Base has_many :chapters end and class
I have the following models: class Label < ActiveRecord::Base has_many :releases end class Release
I have the following two models class Post < ActiveRecord::Base has_many :comments end class
I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible

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.