Forum: Ruby on Rails Database updates doesn't update input fields

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3eda6fcd3204ef285fa52176c28c4d3e?d=identicon&s=25 Guest (Guest)
on 2007-04-25 20:35
Hi there,

I'm a compete Rails n00b and trying to understand how this thing works
so any help is greatly appreciated.

I created a table using migrations, so far so good. My view shows the
list and I can add new records. Now I forgot one column so I added it by
creation a new migration file with an add_column. Raked this migration
and checked the database, it's there.

When I go to my listing web-page the listing is updated BUT, when I
click the New link on this page the input forms still show the old
structure and my newly added column doesn't show up.

I tried to do this without migrations (manually in the database),
restarted Apache, restarted MySQL, migrated to version 0 and back and it
still doesn't show up.

What am I missing here?

Many thanks!
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2007-04-25 20:39
Guest wrote:
> Hi there,
>
> I'm a compete Rails n00b and trying to understand how this thing works
> so any help is greatly appreciated.
>
> I created a table using migrations, so far so good. My view shows the
> list and I can add new records. Now I forgot one column so I added it by
> creation a new migration file with an add_column. Raked this migration
> and checked the database, it's there.
>
> When I go to my listing web-page the listing is updated BUT, when I
> click the New link on this page the input forms still show the old
> structure and my newly added column doesn't show up.

Could show your view code, and your controller code?

Stephan
3eda6fcd3204ef285fa52176c28c4d3e?d=identicon&s=25 Guest (Guest)
on 2007-04-25 20:52
Sure I can.

This is the blog_admin_controller which I didn't change:

--------------
class BlogAdminController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see
http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @blogpost_pages, @blogposts = paginate :blogposts, :per_page => 10
  end

  def show
    @blogpost = Blogpost.find(params[:id])
  end

  def new
    @blogpost = Blogpost.new
  end

  def create
    @blogpost = Blogpost.new(params[:blogpost])
    if @blogpost.save
      flash[:notice] = 'Blogpost was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @blogpost = Blogpost.find(params[:id])
  end

  def update
    @blogpost = Blogpost.find(params[:id])
    if @blogpost.update_attributes(params[:blogpost])
      flash[:notice] = 'Blogpost was successfully updated.'
      redirect_to :action => 'show', :id => @blogpost
    else
      render :action => 'edit'
    end
  end

  def destroy
    Blogpost.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
--------------


And my view code, I suppose you mean the new.rhtml as this is the page
that isn't updated. The edit function also shows the outdated table
structure.


--------------
<h1>New blogpost</h1>

<% form_tag :action => 'create' do %>
  <%= render :partial => 'form' %>
  <%= submit_tag "Create" %>
<% end %>

<%= link_to 'Back', :action => 'list' %>
--------------

Many thanks!
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-04-25 21:12
Guest wrote:
-----------
> <h1>New blogpost</h1>
>
> <% form_tag :action => 'create' do %>
>   <%= render :partial => 'form' %>
>   <%= submit_tag "Create" %>
> <% end %>
>
> <%= link_to 'Back', :action => 'list' %>
> --------------
>
> Many thanks!


Well those scaffold just get you started.  They are not really designed
to continually reflect on your database structure.  After you generate
them you are expected to maintain them yourself.  This is not a
limitation, it's designed to make you move past that featureless ugly
scaffold and into wherever you want to go.

So to add that new field to your forms, you need to add the view code
for it.  As you can see in your new.rhtml file, it render the partial
called "form".  Look in that same folder for a file named "_form.rhtml"
because that is what is being rendered.

Copy one of the existing form field declarations and change the field
name its accessing.
3eda6fcd3204ef285fa52176c28c4d3e?d=identicon&s=25 Guest (Guest)
on 2007-04-25 21:18
Yes!

Thanks, have been looking for this for days and never got to the
_form.rhtml. Doh! I can see it new, off course you don't need the
duplicate code in the edit.rhtml and new.rhtml so this makes sense.

Thanks Alex, this hold me back from going on, thanks to you I found the
motivation again.
This topic is locked and can not be replied to.