开发者

Nested Rails Objects Forms and haml

I've got a rails model class that belongs to other models in the application.. now i want to render an edit form in order to update the records of the relevant objects respectively.. this i have found very confusing..

below is a sample class to illlustrate what i want to achieve:

     class Booking < ActiveRecord::Base` 
          belongs_to :user
          belongs_to :department
          belongs_to :invoice, :class_name => "Department"

     end
  `  

this is also followed by the 开发者_C百科attempt i made at rendering the edit form. I am not familiar with the haml syntax and it appears to have very little documentation that cover this case.

= form_for :booking, :url => booking_path(@booking), :html => { :method => :put } do |form|
  %ul
    - @booking.errors.full_messages.each do |msg|
      %li= msg

      = label :booking, :status
      = form.text_field :status

      - form.fields_for :user do |user_form| 

      = user_form.label :user, :fullname
      = user_form.text_field :fullname

      = form.submit "Update this booking"

Thanks for you answers..


haml is all about indentation. The actual usage of rails helpers etc doesn't change at all. When you indent a line by 2 spaces, it is understood that your indented section is contained within the previous line that was non-indented. Thus this:

%ul#hello
  %li My List Item

Becomes this:

<ul id='hello'>
  <li>My List Item</li>
</ul>

This applies to do blocks also. If you have a rails helper you need to indent the block of the helper just as you would the rest of the markup. Haml will automatically add the end when the indentation ends. You should change your markup to look like this, I've added some comments for clarity:

= form_for :booking, :url => booking_path(@booking), :html => { :method => :put } do |form|
  %ul
    - @booking.errors.full_messages.each do |msg|
      -# This is looking good so far... correct indent inside this `do` block
      %li= msg

      -# I don't feel like we are in the error messages block anymore, but you were still indented here.  I've removed the indentation to indicate that we are no longer in that block.

    -# This will work fine, but is there a reason you didn't use `form.label :status` here?
    = label :booking, :status
    = form.text_field :status

    -# I've changed this to use `= form.` because I believe the other version (-) is deprecated
    = form.fields_for :user do |user_form|
      -# You didn't have this indented correctly.  To be part of the do block you need to indent it appropriately.  Fixed.
      = user_form.label :fullname
      = user_form.text_field :fullname

    -# This is no longer part of the `fields_for` call, so I have removed the indentation again to indicate that this is part of the outer section.
    = form.submit "Update this booking"

Hopefully this was helpful :) Haml might feel a little awkward at first but I have found that it greatly assists development speeds and to me is MUCH easier to read and write. Took me about a week but it grew on me and now I'll never look back :p


I'm currently working through a similar issue,

Here's the parent class...

class Project < ActiveRecord::Base
  has_many :meetings
end

And the child class...

class Meeting < ActiveRecord::Base
  belongs_to :project
end

What I've found is that the form for creating a new Meeting requires both the @project and @meeting (the new meeting object) to be present...

= form_for [@project,@meeting] do |f|
  ...

Whereas the form for editing an existing Meeting only needs the @meeting object...

= form_for @meeting do |f|
  ...

You could take the form out of a partial (assuming you're using the Rails convention of using _form.html.haml for your form) and just have edit.html.haml and new.html.haml, but I suspect this is a comme

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜