1 Replies - 5076 Views - Last Post: 26 July 2011 - 04:12 PM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Collect array of strings (dynamically generated input boxes)

Posted 24 July 2011 - 05:39 PM

I'm working on this UI element for a site. It's going to have news attacks/ news blasts (I couldn't make up my mind on the name). The UI Element consists of

I. A headline
II. A date
III. An array of strings comprising the content of the news blast.

It looks somewhat like this:
Posted Image

First, I did the scaffold command to get everything going
$  rails g scaffold news_blast headline:string date:date content:string



Then I tweaked the NewsBlast model to look like this:

(app/models/news_blast.rb)
class NewsBlast < ActiveRecord::Base
  attr_accessible :date, :headline, :content
  
  serialize :content   # allows an array of strings be stored in database
end



Now, what do I need to do to the view to get an array of strings into :content instead of just a single string?

Here's the edit form's view.
Posted Image

I'm thinking it would be nice to use a trick like http://forums.digita...032#post1233032 to get more input from the user, but then how would I get that input into rails?

(app/views/news_blasts/_form.html.erb)
<%= form_for(@news_blast) do |f| %>
  <% if @news_blast.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@news_blast.errors.count, "error") %> prohibited this news_blast from being saved:</h2>

      <ul>
      <% @news_blast.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :headline %><br />
    <%= f.text_field :headline %>
  </div>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_field :content %>
  </div>
  <div class="field">
    <%= f.label :date %><br />
    <%= f.date_select :date %> 
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>




(app/controllers/news_blasts_controller.rb)
  # POST /news_blasts
  # POST /news_blasts.xml
  def create
    @news_blast = NewsBlast.new(params[:news_blast])   # the trick must be here, a new form?
    
    respond_to do |format|
      if @news_blast.save
        format.html { redirect_to(@news_blast, :notice => 'News blast was successfully created.') }
        format.xml  { render :xml => @news_blast, :status => :created, :location => @news_blast }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @news_blast.errors, :status => :unprocessable_entity }
      end
    end
  end


This post has been edited by NotarySojac: 24 July 2011 - 05:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Collect array of strings (dynamically generated input boxes)

#2 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Collect array of strings (dynamically generated input boxes)

Posted 26 July 2011 - 04:12 PM

I somewhat solved this and wrote a tuton it, but I don't know how to split the array into different text boxes when it comes time to edit a news blast. Luckily, I don't really need that feature atm, but if anyone knows the solutions, I'd be very glad to be able to finish up that blog entry.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1