SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Forms fields pre-populating w/ first record's values

    I have a strange situation. I want to have a create_user form w/ all the fields blank

    When I call the create_user method, the corresponding create_user.html.erb form is displaying the first
    record's data from the user table in the form fields

    Here's a quick view of my form:

    Code:
    <% form_tag('create_user') do -%>
       <%=hidden_field("user", "id") %> (<%=params[:id] %>)
       <p><label for="fname">First Name</label><br/>
        <%=text_field("user", "fname") %></p>
        
       <p><label for="lname">Last Name</label><br/>
        <%=text_field("user", "lname") %></p>
      <%=submit_tag("Create") %> 
    <% end -%>
    When that form loads, it is populated w/ the First and last name from the first user record in the db.

    Anyway around this - I've tried everything I can think of.

    Thanks,
    Clem

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,458
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    I don't know if this will help as I'm pretty much a newbie. But the forms I have in various tutorials I've tried don't have the "minus" as in "do -%>" i.e.
    Code Ruby:
    <%= error_messages_for 'story' %>
    <% form_for @story do |f| %>
      <p>
        name:<br />
        <%= f.text_field :name %>
      </p>
      <p>
        link:<br />
        <%= f.text_field :link %>
      </p>
      <p>
        description:<br />
        <%= f.text_area :description %>
      </p>
      <p>
        tags:<br />
        <%= f.text_field :tag_list %>
      </p>
      <p>
        <%= submit_tag %>
      </p>
    <% end %>
    Code Ruby:
    <h1>Editing forum</h1>
     
    <%= error_messages_for :forum %>
     
    <% form_for(@forum) do |f| %>
      <p>
        <b>Name</b><br />
        <%= f.text_field :name %>
      </p>
     
      <p>
        <b>Description</b><br />
        <%= f.text_area :description %>
      </p>
     
      <p>
        <%= f.submit "Update" %>
      </p>
    <% end %>
     
    <%= link_to 'Show', @forum %> |
    <%= link_to 'Back', forums_path %>
    Code Ruby:
    <h1>Add new book</h1> 
    <% form_for :book, @book, :url => { :action => "create" } do |f| %> 
    <p> 
    Title: 
    <%= f.text_field :title %> 
    </p> 
     
    <p> 
    Price: 
    <%= f.text_field :price %> 
    </p> 
     
    <p> 
    Subject: 
    <%= f.collection_select :subject_id, @subjects, :id, :name %> 
    </p> 
     
    <p> 
    Description: 
    <%= f.text_area :description %> 
    </p> 
     
    <p> 
    <%= f.submit "Create"%> 
    </p> 
     
    <% end %> 
     
    <%= link_to 'Back', {:action => 'list'} %>

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    I don't know if this will help as I'm pretty much a newbie. But the forms I have in various tutorials I've tried don't have the "minus" as in "do -%>"
    All the minus sign does is suppress the carriage return inserted by Rails after the %>. I use it a lot as I think it makes the output code more readable.

    For example:
    Code:
    <td><%= some_information %></td>
    produces:
    Code:
    <td>stuff
    </td>
    Whereas:
    Code:
    <td><%= some_information -%></td>
    produces:
    Code:
    <td>stuff</td>
    So -%> is a great thing to use after <%=, but actually in most loop and logic enties such as <% end %> its usually clearer to leave off the minus sign as it can cause the next line of code to be appended onto the one before the statement making it less clear to read.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clem_c_rock View Post
    When I call the create_user method, the corresponding create_user.html.erb form is displaying the first
    record's data from the user table in the form fields
    I expect the problem is in your controller. Can you post that code?

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Hailsham, UK
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also try using another browser to see if your main browser's auto-completion is kicking in. View source, do you see the values pre-filled in the HTML?

  6. #6
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My first question would be: why isnt your controller using the REST approach?

    New content should have their form filled with the NEW action and the CREATE action creates the actual content in the DB (then redirect to something else). CREATE actions dont have views associated with them (usually).

    I know this is not a solution to your problem, but it would improve the design of your app.

    Also, why is there a hidden field in the form with an ID? And what is this ID anyway if it is supposed to be a blank form?

    As for your issue, please post the controller action + any before filters.
    Ruby on Rails plugin directory and content: The Rail Station
    Cinemaphotograper visit: Canon 5D Tips

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    NC
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello - this was a total newbie question and was solved by using Address.new in the controller and all was good.

    Thanks for everyone's help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •