rails, form select, pluck 2 fields - forms

I'm trying to simply display 2 fields (first name and last name) in a form select (f.select). Here is my code:
<%= f.select :person, User.where(verified_person: 't').pluck(:first_name, :last_name) %>
With the code above, the select drop-down field only displays first name. I'm using Active Record 4.2 and Rails 4. Any help?

Try Using This Code:
<%=f.select :person, options_for_select(User.where(verified_person: 't').collect {|user| ["#{user.first_name} - #{user.last_name}", user.first_name] }), :include_blank => true%>

I ended up using this.. seems to work for me:
<%= f.select(:person) do %>
<% User.where(verified_person: 't').each do |user| -%>
<%= content_tag(:option, user.first_name + " " + user.last_name, value: user.id) %>
<% end %>
<% end %>

Related

Using inputs_for with #conn

I am attempting to create a form like this:
<%= form_for #form_object, registration_path(#conn, :register), [class: "register_form"], fn f -> %>
<div class="register_alert">
<%= Web.PartialView.render("flash_alert.html", conn: #conn) %>
<%= inputs_for f, :user, [default: %{name_first: nil}], fn fu -> %>
<%= label fu, :name_first, "FIRST NAME", class: "register_label" %>
<%= text_input fu, :name_first, class: "register_text-field"%>
<% end %>
<%= submit "Sign Up", class: "register_register-button" %>
</div>
<%end%>
That #form_object is a the conn passed in at first because I have no changeset until they enter data (create form). However, I keep getting the oh so helpful 'argument error' crash when trying to run this. If I remove the inputs_for section, everything is fine. How do you use inputs_for with a connection object in phoenix?
Phoenix 1.4.0 being used.
Plug.Conn is a struct containing nearly everything about the connection.
Phoenix.HTML.Form.form_for accepts the connection object as a first argument.
Since you were even of less help than Phoenix providing exactly zero information about the error message, I’d suggest the issue is in a well-formedness of #form_object. You clearly have #conn object there on hand, pass it as the first argument:
<%= form_for #conn, registration_path(#conn, :register) ...

Simple Search with Rails - Search Results on Separate Page

Im starting out in rails and trying to incorporate a simple search but am only getting so far.
the string looks ok but doesn't seem to execute to the results page. there seems to be a number of issues nil methods, actions missing or routes falling over when I try include restful resources.
I want search on one page (search) and the results to populate to another results page (map).
Both come under the PagesController and are actions within it.
The table is called towns and the user entries and CRUD area it is controlled by the TownsController and has an association with devise User_id.
There is then the pages controller which has search,map,about,contact pages.
class PagesController < ApplicationController
def index
end
def search
#towns = Town.search(params[:search])
end
def page
end
def map
end
end
--------------------------
class Town < ActiveRecord::Base
geocoded_by :name
after_validation :geocode
belongs_to :user
def self.search(search)
if search
search_condition = "%" + search + "%"
where(['townName LIKE ? OR townDescription LIKE ?', search_condition, search_condition])
end
end
-------------------------
views
search.html.erb
<div class="form-group">
<%= form_tag(pages_map_path , method: "get") do %>
<p>
<%= text_field_tag :search, params[:search], class: 'search-text' %>
<%= submit_tag "Search", :name => nil, class: 'btn btn-primary btn-lg'%>
</p>
<% end %>
results --> to map page
map.html.erb
<ul>
<% #towns.each do |town| %>
<li><%= link_to town.name,
:action => 'map', :id => town.id %></li>
<% end %>
</ul>
---------------------------
routes
Rails.application.routes.draw do
devise_for :users
get "pages/search"
get "pages/index"
get "pages/contact"
get "pages/about"
get "pages/map"
get "pages/page"
match ':controller(/:action(/:id))', :via => [:get, :post]
root 'pages#search'
end
Solution:
Column names in search query were incorrect
townName LIKE ? OR townDescription should be = name LIKE ? OR description
updated results page call to:
<ul>
<% #towns.each do |town| %>
<h2><li><%= link_to #name, controller: 'towns', :action => 'show', :id => town.id %></li></h2>
<% end %>
</ul>

Filtering #getFilesAtPath results in Docpad

In Docpad, the following code (using a Query-Engine helper and eco) pulls a list of file names from a directory tree and addds their url to an array:
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).toJSON() : %>
<% images.push(file.url) %>
<% end %>
How might I limit the query to a subset of files, say only PNGs?
So like stated in my answer to your other question: What methods can be called on Docpad's Query tools?
Object returned by your query has some additional default metadata you can't see. As you can see here http://docpad.org/docs/meta-data, one of the metadata is "extension". So you can query with condition like:
extension:'png'
So your code might look like (notice findAll part that gives you a possibility to set search condidtions):
<% images = []; %>
<% for file in #getFilesAtPath({relativeOutDirPath: 'images/'}).findAll(extension:'png').toJSON() : %>
<% images.push(file.url) %>
<% end %>
Or if you want to return all files and trigger different actions on different extensions you could:
<% images = []; %>
<% for file in #getFilesAtPath(relativeOutDirPath: 'images/').toJSON() : %>
<% if file.extension is 'png' : %>
<% images.push(file.url) %>
<% end %>
<% end %>

Rails 3 fields_for different layout for existing records/new records, file uploads

I have a Content model which has one or many Audio files which need to be added by the new/edit form.
What I have did is created the models with this relationship:
class Audio < ActiveRecord::Base
belongs_to :content
has_attached_file :audiofile,
end
class Content < ActiveRecord::Base
...
has_many :audios
accepts_nested_attributes_for :audios, :allow_destroy => true
end
Now in my new Content form I have the following:
<% f.fields_for :audios do |audiof| -%>
<%= f.label :audiofile, 'Audio file:' %>
<%= audiof.file_field :audiofile %>
<% end -%>
What I need it to do is show me the file_field only for a new Audio file and for the existing ones just print me a file size,name and probably a delete button.
I have also created a new record in the controller with:
#content.audios.build
I am using Rails 3.0.3 with Paperclip plugin for upload. Sorry if the question is too nooby.
Thanks.
From my memory, you will be able to access to the instance of the object within the fields_for statement.
Try something like that :
<% f.fields_for :audios do |audiof| -%>
<% if audiof.object.new_record? %>
<%= f.label :audiofile, 'Audio file:' %>
<%= audiof.file_field :audiofile %>
<% else %>
<%= "Filename = #{audiof.object.audiofile.filename}" %>
<%= "url = #{audiof.object.audiofile.url}" %>
<% end %>
<% end -%>
If audiof.object returns nil(In that case, it is not the good name), check by displaying all the public methods <% = raise audiof.public_methods.inspect %>
The object method should return an instance of an Audio class.

Rails 3 Edit Multiple Records in a Single Form

I've been stuck on this problem for a couple of days now.
I've have some success with Railscasts Episode #198, but that one is for Rails 2. There have been some changes in Rails 3 that make it so the code provided in Episode #198 won't work.
The problem lies within the edit_individual.html.erb:
Original Code (provided by Ryan # Railscasts):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
Modified Code (simply changed fields_for to form_for):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% form_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
In the new code, each record is placed within a form of their own, all inside one single form (which is the one I only want).
My question is, how can I get the code provided by Railscasts Episode #198 to work in Rails 3?
Here is a link to the Railscast I mentioned:
http://railscasts.com/episodes/198-edit-multiple-individually
Thank You,
c.allen.rosario
I found the solution. Just need to modify the following line in the code provided by Ryan # Railscasts:
<% fields_for "products[]", product do |f| %>
and change it to:
<%= fields_for "products[]", product do |f| %>
Notice, that the <% has been modified to <%=.
final solution:
<% form_tag update_individual_products_path :method => :put do %>
<% for product in #products %>
<%= fields_for "products[]", product do |f| %>
<h2><%= h product.name %></h2>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
I was wondering if anyone could explain this solution to me. From what I understand you should only need a <% in front of the fields_for.
c.allen.rosario
The change in Rails 3 from <% fields_for to <%= fields_for is because it was confusing that form_for, form_tag, etc... were using <% form... %> even though they they were outputting html code.
With Rails 3, since they output html code, they use <%=.
Please note that your first line is deprecated:
<% form_tag update_individual_products_path, :method => :put do %>
should be
<%= form_tag update_individual_products_path, :method => :put do %>
Same for all form tags.