0
votes

I'm working on a advanced search form. I am working in views/searches. Now I have attributes created for user profiles that I have been using such as zip code, age, gender, career, religion, education, etc. I want to use these fields for my advanced search.

When I include the f.label and text fields I get a undefined method. I'm hoping I don't have to recreate each attribute for the search form, as that would not make much sense considering I have already done all these attributes for the user profile. Any help would be greatly appreciated!

/searches/new.html (for search):

<%= form_for @search do |f| %>
    <div class="field">
        <%= f.label :keywords %><br/>
        <%= f.text_field :keywords%>
    </div>
    <div class="field">
        <%= f.label :zip_code %><br/>
        <%= f.text_field :zip_code %>
    </div>

    <div class="actions"><%= f.submit "Search" %></div>
    <% end %>

/users/new.html (for the user profile):

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% @user.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

    <div class="field">
        <%= f.label :email %><br/>
        <%= f.text_field :email %>
    </div>
    <div class="field">
        <%= f.label :password %><br/>
        <%= f.password_field :password %>
    </div>
    <div class="field">
        <%= f.label :username %><br/>
        <%= f.text_field :username %>
    </div>
    <div class="field">
        <%= f.label :zip_code %><br/>
        <%= f.text_field :zip_code %>
    </div>
    </div>
    <div class="field">I'm a
            <%= f.select :gender, ['man', 'woman']%>
            interested in 
            <%= f.select :gender, ['women', 'men', 'both']%>
            </div>
            <div class="field">
            Age <%= select(@object, :age, (18..75).to_a) %> to  <%= select(@object, :age, (18..75).to_a) %>
            </div>
    <div class="actions"><%= f.submit %></div>
<% end %>
1
It's for both of them. - pwz2000

1 Answers

1
votes

Your @search object is a Search object (or whatever it actually is), not a User.

Rails doesn't know your Search object doesn't actually have those fields, so when it tries to retrieve the fields that don't exist, it'll blow up.

There are any number of ways around this, including giving your Search a User property.

You could also create a new User and pass it to a user form partial as f, that's probably the approach I would take, although I don't know precisely what it would look like without trying it.