SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member Eggman's Avatar
    Join Date
    Nov 2008
    Location
    Eau Claire, WI
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Hash does not contain all form fields

    I'm a ROR newbie. I am developing an app to provided web access to a database. I have a DB table with 4 columns. They are id, board_id, workorder_id, and SerialNumber. I have a view that contains a form to create a row in the table. The code is as follows:
    <h1>New uut</h1>

    <p>Work Order
    <%= Workorder.find(params[:id]).WONumber %></p>

    <% form_tag :action => 'create' do %>

    <p>Board Number
    <%= select("uut", "board_id", Board.findall).collect { |b| [b.RSI_Number, b.id] } ) %></p>
    <p><%= hidden_field_tag "workorder_id", params[:id] %></p>
    <%= render artial => 'form' %>
    <%= submit_tag "Create" %>
    <% end %>

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

    When I click on Create I get an error because the workorder_id is null. The parameters passes to the controller are:
    {"commit"=>"Create", "workorder_id"=>"8", "uut"=>{"SerialNumber"=>"jjj", "board_id"=>"1"}}

    I can work around this problem by getting the workorder_id parameter directly and updating the Uut class before saving it, but my question is - Why isn't workorder_id include as part of the uut hash?

    Thanks,

  2. #2
    SitePoint Member Eggman's Avatar
    Join Date
    Nov 2008
    Location
    Eau Claire, WI
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After further research I am getting a better understanding of this issue. I still don't fully have my answer but will continue to work on it. Ignore this post for the moment.

    Regards

  3. #3
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can replace your hidden_field_tag with:
    Code:
    <input type="hidden" id="uut_workorder_id" name="uut[workorder_id]" value="<&#37;= params[:id] %>" />
    The problem was the hidden_field_tag wasn't creating the right name for the field.

  4. #4
    SitePoint Member Eggman's Avatar
    Join Date
    Nov 2008
    Location
    Eau Claire, WI
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks. That works. I try using input with type=hidden before but I must have had something else wrong as it did not work. Thanks again.

    Eggman

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This might be a better solution:
    Code:
    <&#37;= hidden_field "uut", "workorder_id", params[:id] -%>
    Also this:
    Code:
    Workorder.find(params[:id]).WONumber
    should be done at the controller with something like:
    Code:
    @workorder = Workorder.find(params[:id])
    and then passed to the view. The view code them becomes:
    Code:
    <p>Work Order <%= @workorder.WONumber %></p>
    That will then allow you to catch instances where an id isn't passed to the page. At the moment such an occurance is going to cause errors.

    Likewise:
    Code:
    Board.findall
    Should be done at the controller with something like:
    Code:
    @boards = Board.find(:all)
    and then passed to the view where the code would become:
    Code:
    <%= select("uut", "board_id", @boards).collect { |b| [b.RSI_Number, b.id] } ) %>

  6. #6
    SitePoint Member Eggman's Avatar
    Join Date
    Nov 2008
    Location
    Eau Claire, WI
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the advice. However the first code you suggested did not work as show but I change it to:
    Code Ruby:
    <%= hidden_field "uut", "workorder_id", :value => params[:id] -%>

    and it worked. By following your next suggestion to get the workorder in the controller I was able to change it to:
    Code Ruby:
    <%= hidden_field "uut", "workorder_id", :value  => @workorder.id] -%>
    Jim

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep. Should have tested the syntax before I posted.

    However, I think what you've ended up with will be easier to manage and reuse.


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
  •