I have a basketball app with a many-to-many relationship where a Coach could have coached multiple teams, and a team can have many Coaches.
Coaches_Controller.rb
def index
#coaches = Coach.joins(:teams).select("coaches.first_name, coaches.last_name, teams.team_level")
respond_to do |format|
format.html # index.html.erb
format.json { render json: #coaches }
end
end
Index.html.erb
<% #coaches.each do |coach| %>
<tr>
<td><%= link_to coach.first_name, coach_path(coach) %></td>
<td><%= coach.last_name %></td>
<td><%= coach.team_level %></td>
<td>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_coach_path(coach), :class => 'btn btn-mini' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
coach_path(coach),
:method => :delete,
:confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
:class => 'btn btn-mini btn-danger' %>
</td>
</tr>
<% end %>
I am getting this error, and I'm not quite sure why...
http://i.stack.imgur.com/5a6oB.png
Ideas? I feel like it is something small I'm not seeing... Thanks!
One thing I can see that is wrong is that you don't have coaches.id in your select. You need the id for coach_path(coach) to work. Try this:
#coaches = Coach.joins(:teams).select("coaches.id, coaches.first_name, coaches.last_name, teams.team_level")
Not sure if this solves the join error you are getting.
Related
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/
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
Not able to understand why the same command Category.find(:all , :order => 'name ASC') works in the rails console but not returning anything in the template and controller
please help
_form.html.erb
<% #all_categories.each do |cat| -%>
<li><%= check_box_tag('categories[]' , cat.id , #post.categories.collect{|c| c.id}.include?(cat.id))%>
<%= cat.name %></li>
<% end -%>
posts_controller.rb
#all_categories = Category.find(:all , :order => 'name ASC')
rails console
#all_categories = Category.find(:all , :order => 'name ASC')
Category Load (0.2ms) SELECT `categories`.* FROM `categories` ORDER BY name ASC
=> [#<Category id: 1, name: "General", short_name: "general", description: "This is a general category">]
error
undefined method `each' for nil:NilClass
posts_controller
def edit
#user_list = get_user_list
#post = Post.find(params[:id])
end
def update
post_params = params[:post]
author_id = post_params.delete(:author_id)
##all_categories = get_all_categories
#all_categories = Category.find(:all , :order => 'name ASC')
checked_categories = get_categories_from(params[:categories])
removed_categories = #all_categories - checked_categories
#post = Post.find(params[:id])
#post.author = User.find(author_id) if #post.author_id != author_id
if #post.update_attributes(post_params)
perform_operation_on_categories
flash[:notice] = "Post was successfully updated."
redirect_to :action => 'show' , :id => #post
else
#user_list = get_user_list
render :action => 'edit'
end
end
index view
<h1><% #page_title = 'Listing posts' %></h1>
<%= content_tag('p',link_to('« Back', :controller => 'staff',:action =>'menu')) %>
<table>
<tr>
<th>Created at</th>
<th>Title</th>
<th>Author</th>
<th>Categories</th>
<th>Status</th>
<th>Comments</th>
</tr>
<% #posts.each do |post| %>
<tr class="<%= cycle('row1','row2') %>">
<td><%= post.created_at.strftime('%m/%d/%y %I:%m %p')%></td>
<td><%= h(post.title)%></td>
<td><%= h(post.author.display_name) if post.author%></td>
<td><%= post.categories.collect {|cat| cat.name}.join(", ")%></td>
<td><%= h(post.status)%></td>
<td><%= post.comments_count %></td>
<td><%= link_to('Preview',{:action =>'show',:id=>post,:target => '_blank'})%></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Delete', post, :method => :delete, :data => { :confirm => 'Are you sure you want to permanently remove this post?' } %></td>
</tr>
<% end %>
</table>
<%= will_paginate(#posts) %>
<br/>
<%= link_to 'New Post' , :action=> 'new' %>
_form.html.erb (the part where i am getting error)
<table>
<tr>
<th>Title</th>
<td><%= text_field(:post,:title,:size => 40 , :style => "font-size: 1.5em;")%></td>
<td rowspan="2">
<div class="categorylist">
<h2>Categories:</h2>
<ul>
<% #all_categories.each do |cat| -%>
<li><%= check_box_tag('categories[]' , cat.id , #post.categories.collect{|c| c.id}.include?(cat.id))%>
<%= cat.name %></li>
<% end -%>
</ul>
edit.html.erb
<% #page_title = 'Edit Post' -%>
<%= content_tag('p',link_to('« Back', :action => 'index'))%>
<%= form_tag(:action => 'update') do -%>
<%= render(:partial => 'form')%>
<%= submit_tag('Create', :style => 'margin: 1.5em 0 0 100px;')%>
<% end %>
<%= link_to('Preview Post' , {:action=>'show',:id => #post} ,:target => '_blank')%>
Just guessing from the code you posted but it seems to me that you should put the
#all_categories = Category.find(:all , :order => 'name ASC')
in the edit method that will be called to display your view and not only in the update method that will be called to persist your changes.
The same eventually applies to new, index and show methods
HTH
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
I want to know how to get the selected value of <select>. I only know how to populate this.
Here's is my code in index.html.erb. I used this to populate the <select> dropdown menu.
<h1>Trap</h1>
<%= form_for #search do |f| %>
<p>
Employee Code:
<%= f.select(:empcode_contains, #employee.collect {|e| [ e.empcode, e.id ]}) %>
</p>
<p class="button"><%= f.submit "Search" %></p>
<% end %>
<p>
Sort by:
<%= sort_link #search, :empcode %> |
<%= sort_link #search, :date_entry %> |
</p>
<table>
<tr>
<th>Empcode</th>
<th>Date entry</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #traps.each do |trap| %>
<tr>
<td><%= trap.empcode %></td>
<td><%= trap.date_entry %></td>
<td><%= link_to 'Show', trap %></td>
<td><%= link_to 'Edit', edit_trap_path(trap) %></td>
<td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Trap', new_trap_path %>
And in my controller traps_controller.rb:
def index
#search = Trap.search(params[:search])
#traps = #search.all
#employee = Employee.all
Trap.all.each do |t|
Employee.create(:empcode => t.empcode)
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #traps }
end
end
Pls tell me how to get the selected value if the user selects a value. I don't have any idea.
Try (in the controller) :
value = params[:empcode_contains]
or you could fetch the whole object this way:
search = params[:search]