当你想要的属性存储在变量中时,如何在Rails中编写Object.attribute?

时间:2014-03-02 23:07:21

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我有一个带有一堆属性的college对象(acceptance_rate,tuition ...)。我有一系列属性以"人类可读的格式" (例如,接受率)称为@data_listing。我有一个函数,它接受人类可读的属性并返回名为get_data_attribute_name的实际属性(Acceptance Rate => acceptance_rate)。

如何以college.attribute格式正确的方式致电attribute

这是我的代码,但它给了我这个错误:undefined method 'data_attr' for #<College:0x007fb1df017578>

<% if flash[:notice] %>
    <div class="alert alert-success"><%= flash[:notice] %></div>
<% end %>

<div id="form">
    <%= simple_form_for :comparison, :url => '/comparison/update' do |f| %>
        <%= f.input :college, required: false, collection: ['University of Pittsburgh', 'University of Wisconsin', 'Colgate', 'Adelphi University'], :input_html => { :multiple => true } %>
        <%= f.input :section, required: false, collection: ['Professors', 'Classes', 'Difficulty', 'Campus', 'Food', 'Dorms', 'Safety', 'Social Atmosphere', 'Parties', 'Actuarial Science', 'Agriculture', 'Applied Math', 'Astronomy', 'Biology', 'Chemistry', 'Computer Science', 'Earth Science', 'Geology', 'Linguistics', 'Mathematics', 'Neuroscience', 'Physics', 'Statistics', 'Anthropology', 'Criminology', 'Economics', 'History', 'Political Science', 'Psychology', 'Sociology', 'Urban Studies', 'Women\'s Studies', 'Art', 'Classics', 'English', 'Film', 'History and Philosophy of Science', 'Literature', 'Music', 'Philosophy', 'Religious Studies', 'Theatre', 'African', 'Chinese', 'French', 'German', 'Italian', 'Japanese', 'Jewish', 'Latin American', 'Middle Eastern', 'Russian', 'Spanish', 'Other', 'Accounting', 'Finance', 'Management', 'Marketing', 'Bioengineering', 'Chemical', 'Civil', 'Computer', 'Electrical', 'Engineering Science', 'Industrial', 'Materials Science and Engineering', 'Mechanical', 'Nuclear', 'Architecture', 'Athletic Training', 'Communications/Journalism', 'Information Science', 'Nursing', 'Nutrition', 'Pharmacy', 'Public Health', 'Rehab Science', 'Social Work'], :input_html => { :multiple => true } %>
        <%= f.input :data, required: false, collection: ['Public/Private', '2-year/4-year', 'National University/Liberal Arts College', 'Academic Semester', 'Location', 'Setting', 'Endowment', 'Athletic Division', 'Nickname', '4-Year Graduation Rate', '6-Year Graduation Rate', 'Degree Types Offered', 'ROTC Offered', 'US News Ranking', 'Student-to-Faculty Ratio', 'Listing of Top-25 Graduate Programs', 'Class Sizes', 'Where Graduates Continue Study', 'Most Popular Majors', 'Undergraduate Enrollment', 'Graduate Enrollment', 'In-State/Out-of-State', 'Male/Female', '% in Fraternities', '% in Sororities', '% of Undergraduates Living On-Campus', '% of Freshman Living On-Campus', 'Ethnicity Distribution', 'In-State Tuition', 'Out-of-State Tuition', 'Room & Board', 'Average Annual Cost by Family Income Level', '% of those With Need who Got Aid', '% Who Got Need Fully Met', '% of Total Need Met', 'Scholarships/Loans', 'SAT Math', 'SAT Reading', 'SAT Writing', 'SAT Composite', 'ACT Math', 'ACT English', 'ACT Writing', 'ACT Composite', 'SAT Math Distribution', 'SAT Reading Distribution', 'SAT Writing Distribution', 'ACT Math Distribution', 'ACT English Distribution', 'ACT Composite Distribution', '% Who Submit SAT', '% Who Submit ACT', 'Acceptance Rate', '% Admitted Who Enrolled', 'Admissions Criteria', 'High School GPAs', 'High School Class Ranks'], :input_html => { :multiple => true } %>
        <%= f.button :submit, class: 'btn btn-success btn-block' %>
    <% end %>
</div>

<h1>Comparison Table</h1>
<% if !@college_listing.nil? && !@summary_listing.nil? || !@data_listing.nil? %>

<% @college_listing.shift %>
<% @summary_listing.shift %>
<% @data_listing.shift %>

<table class="table table-bordered table-striped">
    <thead>
        <th>&nbsp;</th>
        <% @college_listing.each do |college| %>
          <% @colleges.each do |college_instances| %>
            <% if college_instances.name == college %>
              <th><%= link_to "#{college}", "/#{college_instances.url}"  %></th>
            <% end %>
          <% end %>
        <% end %>
    </thead>
    <tbody>
        <% @summary_listing.each do |category| %>
            <tr>
                <th><%= category %></th>
                <% @college_listing.each do %>
                    <td>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</td>
                <% end %>
            </tr>
        <% end %>
        <% if !@summary_listing.empty? && !@data_listing.empty? %>
            <tr>
                <td>&nbsp;</td>
                <% @college_listing.each do %><td>&nbsp;</td><% end %>
            </tr>
        <% end %>
        <% @data_listing.each do |data| %>
          <% data_attr = get_data_attribute_name(data) %>
          <tr>
            <th><%= data %></th>
            <% @college_listing.each do |college_listing| %>
              <% @colleges.each do |college| %>
                <% if college.name == college_listing %>
                  <td><%= "#{college.data_attr}" %></td>
                <% end %>
              <% end %>
            <% end %>
          </tr>
        <% end %>
    </tbody>
</table>

<% end %>

1 个答案:

答案 0 :(得分:1)

您应该使用send方法。

college.send(data_attr)