Topic: Behind the scaffolding

Hi all,

I'm a rails newbie and I'm trying to figure out how to create, update, delete, list and individually view models using simple forms.  The problem is that most tutorials seem to do this using scaffolding.

I've put together something that seems to work but, in all honesty I don't understand it!  I've attached the code for a view and the corresponding action in the controller.

Here's the view:

<h1>CREATE POST</h1>
<table>
<%= form_tag :action => 'create_post' %>
<tr>
    <td>Title:</td>
    <td><%= text_field ('post', 'title') %></td>
</tr>
<tr>
    <td>Body</td>
    <td><%= text_field ('post', 'post_body') %></td>
</tr>
<tr>
    <td></td>
    <td><%= submit_tag 'Save' %><td>
<tr>

</form>
</table>


Here's the code for the action.  Where does (params[:post]) come from?  I guess the view created a post object?  How did we tell it to do that?  I know we've associated the text fields with fields on the post model (e.g. "text_field ('post', 'post_body')", but where does the object come from?

class ForumController < ApplicationController

  def create_post
    @post = Post.new(params[:post])
    if(params[:post])
      @post.save
      redirect_to :action => 'post_created', :id => @post
    else
      render :action => 'create_post'
    end
  end


  def view_post
    @post = Post.find(params[:id])
  end

end


The other thing is that I don't really understand the process by which the assignment of @post works in the view_post code.  I mean, in the create_post method we've assigned the whole post to :id...  shouldn't it be ":id => @post.id" ?

I know these things will be obvious to most people, but I'm having trouble finding answers to this stuff, and I don't like writing code that I don't understand yikes)

I'd appreciate any help with this

Thanks

S.

Last edited by segfaultsoul (2006-11-19 10:01:53)

Re: Behind the scaffolding

I might be wrong as I'm new also, but I think I can answer some of your questions.

the params[:post] comes from your form where you have all the text_fields ("post", "db_field_name"... it will map the form to the db for you.

ruby includes alot of syntax sugar to allow you to write things they way you want, rails also follows this. you could write :id => @post.id if you wanted but by default all db_tables have an id coloum, so it will do it for you

I would really suggest picking up a book to learn this. ruby for rails is good if you dont know ruby. and it has a rails tutorial in aswell.

Last edited by BasicMind (2006-11-19 10:35:00)

--

Re: Behind the scaffolding

Hey, thanks so much for your reply BasicMind!

Does this mean that as soon as you relate a text field to a model then an instance of that model class will be created under the same name - I mean as soon as I say "text_field ('post', 'title')"  then an instance of the Post class will be created and accessible as params[:post]?

That's pretty cool, but kinda strange for me...

Thanks again for clearing that up for me, I feel more comfortable with this now

S.

Re: Behind the scaffolding

params is just a hash that is available to your controller methods.  Each time a web browser sends a request to your application params is filled with the posted form values (or query string values) from that request.  So:
http://myapp.com/pages/show/1?go=yes&am … y_document
means that your params will look like this (assuming you have the standard routes set up):

params => {:controller => 'pages', :action => 'show', :id => 1, :go => 'yes', :file => 'my_document'}

And when you use a view helper like 'text_field(:page, :name)' it doesn't create any object '@page'.  What it does is look to see if you've already got an object named @page and it uses the values from @page to fill out the form.  So:

<%
@page = Page.new
@page.name = 'my name'
%>
<%= text_field :page, :name %>

will print:
<input type=text name='name' value='my name' />