English 中文(简体)
Need help modifying multiple models example from advanced rails recipes book
原标题:

I m developing a simple rails app for my own use for learning purposes and I m trying to handle 2 models in 1 form. I ve followed the example in chapter 13 of Advanced Rails Recipes and have got it working with a few simple modifications for my own purposes.

The 2 models I have are Invoice and InvoicePhoneNumber. Each Invoice can have several InvoicePhoneNumbers. What I want to do is make sure that each invoice has at least 1 phone number associated with it. The example in the book puts a remove link next to each phone number (tasks in the book). I want to make sure that the top-most phone number doesn t have a remove link next to it but I cannot figure out how to do this. The partial template that produces each line of the list of phone numbers in the invoice is as follows;

<div class="invoice_phone_number">
  <% new_or_existing = invoice_phone_number.new_record? ?  new  :  existing  %>
  <% prefix = "invoice[#{new_or_existing}_invoice_phone_number_attributes][]" %>

  <% fields_for prefix, invoice_phone_number do |invoice_form| -%>
    <%= invoice_form.select :phone_type, %w{ home work mobile fax } %>
    <%= invoice_form.text_field :phone_number %>
    <%= link_to_function "remove", "$(this).up( .invoice_phone_number ).remove()" %>
  <% end -%>
</div>

Now, if I could detect when the first phone number is being generated I could place a condition on the link_to_function so it is not executed. This would half solve my problem and would be satisfactory, although it would mean that if I actually wanted to, say, delete the first phone number and keep the second, I would have to do some manual shuffling.

The ideal way to do this is presumably in the browser with javascript but I have no idea how to approach this. I would need to hide the remove link when there was only one and show all remove links when there is more than one. The functionality in the .insert_html method that is being used in the add phone number link doesn t seem adequate for this.

I m not asking for a step-by-step how-to for this (in fact I d prefer not to get one - I want to understand this), but does anyone have some suggestions about where to begin with this problem?

问题回答

There is a counter for partial-collections:

<%= render :partial => "ad", :collection => @advertisements %>  

This will render "advertiser/_ad.erb" and pass the local variable ad to the template for display. An iteration counter will automatically be made available to the template with a name of the form partial_name_counter. In the case of the example above, the template would be fed ad_counter.

For your problem of detecting whether a row is the first one or not, you could add a local variable when calling the partial:

<%= render :partial =>  mypartial , :locals => {:first => true}  %>

As it would be much easier to detect in the main file, whether a row is the first or not I guess.

Instead of detecting whether a phone number is the first, you could also detect whether a phone number is the only one. If not, add remove links next to all numbers otherwise, do not display the remove link. Note that besides showing/hiding the link, you also need to add code, to prevent removing of the last number by (mis)using an URL to directly delete the number instead of using your form.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签