Search Multiple Columns In Model Rails 3 - ruby-on-rails-3

I now have a simple search form that will search one column in my model and return results as expected, however I would like to build upon this by being able to search upon 4 columns within the same model, using 4 text_field/Select_tags.
I have looked at Gems that provide this like solr for example but my thinking is that it seems a bit overkill for what i want to achieve, especially on such a small app
the app is simple, you can upload recipes, add recipes to favourites and search recipes.
So far it looks like this
Controller
def search
#countrysearch = Recipe.where(:country_of_origin => params[:search]).all
end
Search Form
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'}) do |s| %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search" %>
<% end %>
Output (View)
<% #countrysearch.each do |r| %>
<tr>
<td><%= r.dish_name %></td>
<td><%= r.country_of_origin %></td>
<td><%= r.difficulty %></td>
<td><%= r.preperation_time %></td>
<td><%= ingredient_names(r.ingredients) %></td>
<td><%= preperation_steps(r.preperations) %></td>
</tr>
I would like my search form to mirror my "Create New recipe Form" with select tags
<%= f.label :dish_name, "Dish Name" %>
<%= f.text_field :dish_name, :placeholder => "Enter Dish Name" %>
<%= f.label :country_of_origin, "Country Of Origin" %>
<%= f.select :country_of_origin, [['Wales'],['Scotland'],['England']], {:include_blank => 'Please Select'} %>
<%= f.label :difficulty, "Difficulty Level" %>
<%= f.select :difficulty, [['Beginner'],['Intermediate'],['Expert']], {:include_blank => 'Please Select'} %>
<%= f.select :preperation_time, [['15..30'],['30..60'],['60..120']], {:include_blank => 'Please Select'} %>
<% end %>
Just some pointers in the right direction would be appreciated. Thank you

You can do it using all the parameters you have passed to controller:
def search
#countrysearch = Recipe.where({:dish_name => params[:search][:dish_name], :country_of_origin => params[:search][:country_of_origin], :difficulty => params[:search][:difficulty], :preperation_time => params[:search][:preperation_time]}).all
end
This should work for you. There are other alternatives, but if you want a simple one, this may fit you.
Edit:
If you don't have other things in your search for, you can do something like this:
<%= form_tag({:controller => 'search', :action => 'search'}, {:method => 'get'}) do |s| %>
<%= text_field_tag :dish_name, params[:dish_name] %>
<%= select_tag "country_of_origin", options_from_collection_for_select(#recipes, "country_of_origin", "country_of_origin_name")
<%= select_tag "difficulty ", options_from_collection_for_select(#recipe, "difficulty ", "difficulty ")
<%= select_tag "preperation_time", options_from_collection_for_select(#recipe, "preperation_time", "preperation_time")
<%= submit_tag "Search" %>
<% end %>
Then in the controlelr just pass the parameters directly
def search
#countrysearch = Recipe.where(params).all
end

Related

Paperclip::AdapterRegistry::NoHandlerError, Using fields_for and partial

currently image is only returning a String. I learned that this is what is causing the error from this question: Paperclip exception : Paperclip::AdapterRegistry::NoHandlerError
Here are the current parameters that I'm seeing:
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"SlvvynUVZCqV4fC3L8Dp1UM9IhDeihko44CFVyamxMU=",
"examples"=>{"66"=>{"image"=>"Ambition flyer 2 (1).jpg",
"description"=>"THis is a photo and what not"},
"67"=>{"description"=>""}},
"commit"=>"Submit",
"collection_id"=>"19"}
How do I get :image to return something like this in params (from other post):
"asset"=>
{"image"=>
#<ActionDispatch::Http::UploadedFile:0x000000056679e8
#content_type="image/jpg",
#headers= "Content-Disposition: form-data; name=\"asset[image]\";
filename=\"2009-11-29-133527.jpg\"\r\nContent-Type: image/jpg\r\n",
#original_filename=""2009-11-29-133527.jpg"",
#tempfile=#<File:/tmp/RackMultipart20120619-1043-yvc9ox>>}
**Currently, I have this in **edit_individual.html.erb****
<h2>Editing <%= #collection.title %> Collection</h2>
<%= form_tag update_individual_collection_examples_path, :method => :put do %>
<% for example in #examples %>
<%= fields_for "examples[]", example do |f| %>
<h2></h2>
<%= render 'pic-upload', :html => { multipart: true }, :f => f, :example => example %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
and this in my _pic-upload.html.erb partial:
<table>
<tr><td><%= image_tag example.image.url, size: "300x300" %></td>
<td><%= f.label :image, "Upload picture" %></td>
<td><%= f.file_field :image %></td>
<td><%= f.label :description, "Description (Optional)" %></td>
<td><%= f.text_area(:description, size: "50x3") %></td>
</tr>
</table>
<% end %>
Solution
Add :multipart => true the respective form_tag:
<%= form_tag(update_individual_collection_examples_path, :method => :put, :multipart => true)
Syntax is important here. The form_tag must include (parenthesis around the methods).
Resource
http://www.saalonmuyo.com/2010/01/27/using-form_tag-in-ruby-on-rails/

Nested table formatting with simple_form, nested_form and twitter-bootstrap

Update: I updated this after doing some digging and realizing that this might be twitter-bootstrap causing the problem.
Here is a rough version of my nested form:
<%= simple_nested_form_for #user, :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
<%= f.input :email %>
<%= f.input :name_first %>
<%= f.input :name_last %>
<table class="table table-striped">
<thead>
<tr>
<th>Active</th>
<th>Company</th>
<th>Role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<%= f.simple_fields_for :roles, :wrapper_tag => :tr do |role_form| %>
<td><%= role_form.hidden_field :id %><%= role_form.input :active, :label => false, :wrapper => false %></td>
<td><%= role_form.association :company, :label => false, :wrapper => false %></td>
<td><%= role_form.input :role, :label => false, :collection => [ "Guest", "User", "Inspector", "Owner"], :wrapper => false %></td>
<td><%= role_form.link_to_remove "Delete", :class => 'btn btn-mini btn-danger' %>
</td>
<% end %>
</tbody>
</table>
<p><%= f.link_to_add "Add a Role", :roles %></p>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', users_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>
When it's rendered the fields in the table rows are indented the same as the parent form via the { :class => 'form-horizontal' }. I just want the fields with no wrapper divs etc. and can't seem to figure it out. I thought the :wrapper => false was the ticket but no luck so far.
Dan
I ended up figuring it out on my own. You have to move the form style (form-horizontal) into a div just around the non-nested fields:
<%= simple_nested_form_for #user do |f| %>
<fieldset>
<div class="form-horizontal">
<%= f.input :email %>
<%= f.input :name_first %>
<%= f.input :name_last %>
<%= f.input :phone %>
<%= f.input :mobile %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
</div>
<div class="tubbable">...
If you want to use a table (as in your initial example) to do the layout, I've patched the nested_form gem here https://github.com/ritchiey/nested_form to allow that.
To specify that you want the new fields appended at the bottom of the tbody and wrapped in a tr, replace your current link_to_add call with:
<%= f.link_to_add "Add a Role", :roles, :container =>'tbody', :fields_element=>'tr'%>
Note: the :container param is a CSS selector.
Not sure if this is what you want, but if you want to remove the div wrapper from an input field, use f.input_field instead of f.input:
= f.input_field :email, label: false, placeholder: 'email'
Add :wrapper => false to the simple_nested_form_for call.
The problem is, that :wrapper => false in simple_fields_for gets overwritten by the default :wrapper => nil in the simple_form_for configuration.
See this link for a setup:
How To: Render nested fields inside a table

Blog - comment count on index page

Am trying to display a 'total comments' number (i.e.7) count beside the article on the index page and not on the article page. Would like to use a ruby method for this as its probably the most straight forward...?
views/articles/_article.html.erb
<div class="article_header">
<b>Title: </b> <%= truncate(article.title, :length => 50) %>
by <%= article.user.username %> on <%= article.created_at.strftime("%d %B, %Y") %>
<b>Detail:</b> <%= truncate(article.body, :length => 225) %>
</div>
<br />
<%= blog.comments.count %>
<%= link_to 'Read', article %>
<% if can? :update, article %>
| <%= link_to 'Edit', edit_article_path(article) %> |
<% end %>
Pass in a variable when you call your partial:
= render "article", :display_count => true
Then in your partial:
<% display_count ||= false %>
<%= display_count ? blog.comments.count : '' %>
The correct way to do this would be:
<td><%= link_to "Comment count = #{article.comments.count}", article_path(article) %>
This will simply add another column to the output on your index page. It doesn't have to be linked if you simply want to display the count:
<td><%= "Comment count = #{article.comments.count}" %>

options within nested form

Trying to make a nested form, which is working fine so far, except i need to put some dropdowns for the user to choose, as well as maybe make a couple of validations, however it seems nothing gets out of the form properly and keep getting errors no matter what I try.
three models.
--configuration
has_many :configoptions
accepts_nested_attributes_for :configoptions
--configoption
belongs_to :configuration
has_many :items
and item
belongs_to :configoption
scope :sorted, order('items.position ASC')
Now, so far I'm creating a nested form, looping through the configoptions, but for each option is possible there's more than one item. So I want to make a drop-down for those options where this is the case.
In my view i have:
<p>
<th>Elements</th>
<th>Quantity</th>
</p>
<%= form_for #config, :url => {:action => 'show', :id => #config.id} do |f| %>
<%= f.fields_for :configoptions do |fp| %>
<p>
<% if :items.count > 1 %>
<%= fp.text_field :name %>
<% else %>
<% fp.select(:items, :name)%>
<% end %>
<%= fp.text_field :quantity %>
</p>
<% end %>
<%= f.submit %>
<% end %>
I get an error obviously telling me that it can't count the :items.
How do you think I can make this work?
Thanks!
<%= form_for #config, :url => {:action => 'show', :id => #config.id} do |f| %>
<%= f.fields_for :configoptions do |fp| %>
<%= fp.text_field :id %>
<%= fp.text_field :name %>
<%= fp.text_field :quantity %>
<% end %>
<%= f.submit %>
<% end %>
OK, I think I figured it out, at least it seems to be doing what I want now.
I modified the view to pass the instance of the configoption into the nested form itself to be able to create the drop downs.
<% for configoption in #config.configoptions %>
<%= f.fields_for :configoptions, configoption do |fp| %>
<p>
<% if configoption.items.count > 1 %>
<%= fp.select (:name, options_from_collection_for_select(configoption.items.sorted, 'name', 'name'))%>
<% else %>

in rails 3.0 I want to use a partial view in a show and in an edit, for edit I want to show the link to delete

<%= question.name %>
Created <%= time_ago_in_words(question.created_at) %> ago.
<% if :action == "edit" then %>
<%= link_to "delete", question, :method => :delete,
:confirm => "You sure?",
:title => question.name %>
<% end %>
Use action_name like so:
<% if action_name == 'edit' %>
<%= link_to 'delete', question, :method => :delete, :confirm => 'You sure?', :title => question.name %>
<% end %>