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 247835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:17:33+00:00 2026-05-11T21:17:33+00:00

I have a Model Book with a virtual attribute for create a Editor from

  • 0

I have a Model Book with a virtual attribute for create a Editor from the Book form.
The code looks like:

class Book < ActiveRecord::Base
  has_many :book_under_tags
  has_many :tags, :through => :book_under_tags
  has_one  :editorial
  has_many :written_by
  has_many :authors, :through => :written_by

  def editorial_string
   self.editorial.name unless editorial.nil?
   ""
  end
  def editorial_string=(input)
    self.editorial = Editorial.find_or_create_by_name(input)
  end
end

And the new form:

<% form_for(@book,
            :html => { :multipart => true }) do |f| %>
  <%= f.error_messages %>

...
  <p>
    <%= f.label :editorial_string , "Editorial: " %><br />
    <%= f.text_field :editorial_string, :size => 30  %> <span class="eg">Ej. Sudamericana</span>
  </p>
 ...

With this, when the form data no pass the validations I lost the data submited in the editorial field when the form is redisplayed, and also a new Editor is created. How I can fix this two problems? I am pretty new in ruby and I can’t find a solution.

UPDATE my controller:

  def create
    @book = Book.new(params[:book])
    respond_to do |format|
      if @book.save
        flash[:notice] = 'Book was successfully created.'
        format.html { redirect_to(@book) }
        format.xml  { render :xml => @book, :status => :created, :location => @book }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @book.errors, :status => :unprocessable_entity }
      end
    end
  end
  • 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-11T21:17:33+00:00Added an answer on May 11, 2026 at 9:17 pm

    I believe its cause your Book#editorial_string method will always return “”. Could simplify to the following:

      def editorial_string
       editorial ? editorial.name : ""
      end
    

    Update based on comment:

    Sounds like you want to do nested forms. (See accepts_nested_attributes_for in api docs) Note this is new in Rails 2.3.

    So if you update your Book class

    class Book < ActiveRecord::Base
      accepts_nested_attributes_for  :editorial
      ...
    end
    

    (You could also now remove the editorial_string=, editorial_string methods too)

    And update your forms to something like the following

    ...
    <% f.fields_for :editorial do |editorial_form| %>
      <%= editorial_form.label :name, 'Editorial:' %>
      <%= editorial_form.text_field :name %>
    <% end %>
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like this: public class Book { public string
I have one grails application.In that I have one model class named Book. From
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
Say I have a model like this: class Book(models.Model): user = models.ForeignKey(User) book_isbn =
I have a model like this: class Book(models.Model): authors = models.ManyToManyField(User) # User is
I have HABTM models Client and Book . Client model has a bookshelf_color attribute
Given a model structure like this: class Book(models.Model): user = models.ForeignKey(User) class Readingdate(models.Model): book
I have the following Model class: package com.swaranga.model; public final class Book { private
I have this code book_model_delete = { 'model' : Book, 'post_delete_redirect': /boo/list/, template_name :
Let's say I have two models, Book and Page: class Book(models.Model): pass class Page(models.Model):

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.