Rails - Rendering an edit form partial within another partial - ruby-on-rails-3

I have a table that I am trying to make a bit more streamlined. In my view (this view is already a partial (_recipe_ingredient.html.erb), I have the following code, where rendering the partial is not working:
<tr>
<td><%= recipe_ingredient.ingredient.name %></td>
<td><%= recipe_ingredient.ingredient.weight1*recipe_ingredient.quantity %></td>
<td><%= recipe_ingredient.ingredient.description1 %></td>
<td><%= recipe_ingredient.quantity %></td>
<td><%= render(:partial => 'recipe_ingredients/edit', :locals =>
{:recipe_ingredient=> recipe_ingredient}) %></td>
<td><%= link_to 'Remove', recipe_ingredient, :confirm => 'Remove ingredient from
recipe?', :method => :delete %></td>
</tr>
Previously, I was using link_to to edit the recipe_ingredient as follows (which worked fine), but I would like not to have the user go to another page to edit - instead I want the form to be part of the table:
<td><%= link_to 'Edit Quantity', edit_recipe_ingredient_path(recipe_ingredient) %></td>
The edit partial (which the new non-working code calls) looks like:
<h1>Editing recipe_ingredient</h1>
<%= render 'recipe_ingredients/form', :recipe_ingredient => #recipe_ingredient %>
And the standard form partial looks like:
<%= form_for(#recipe_ingredient) do |recipe_ingredient| %>
<% if #recipe_ingredient.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#recipe_ingredient.errors.count, "error") %> prohibited this
recipe_ingredient from being saved:</h2>
<ul>
<% #recipe_ingredient.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= recipe_ingredient.label :quantity %><br />
<%= recipe_ingredient.number_field :quantity %>
</div>
<div class="actions">
<%= recipe_ingredient.submit %>
</div>
<% end %>
Mainly, I'm confused why it works using link_to, but I can't simply render the partial. The error I'm getting is undefined method `model_name' for NilClass:Class in the first line of the form partial.
I've tried taking the "#" off #recipe_ingredient in the form partial, but that doesn't work either.
Thanks in advance for any help.

just create object with #recipe_ingredient in your action i think its an edit action.
#recipe_ingredient = RecipeIngredient.find_by_id(params[:id)
hope this will work fine.

Related

How to use Russian Doll caching on same model

I am building a Rails 3.2 web app.
In this app I am using the russian doll style of caching.
I am fetching projects in a table list and records are cached. The problem is that
when I am for example changing the title of a record the title in the list is not updated.
If this were a association I would use :touch => true but it´s the same model.
This is how I start the listing:
<%= render #projects %>
This is the projects.html.erb template:
<% cache projects do %>
<%= render project %>
<% end %>
This is the project.html.erb template:
<% cache project do %>
<tr>
<td><%= link_to project.title.capitalize, admin_project_path(project) %></td>
<td><%= project.reference %></td>
<td><%= show_status project.status %></td>
</tr>
<% end %>
What am I doing wrong?
Thankful for all input.

Search Multiple Columns In Model 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

Grouping records with headings in Rails 3

I have an articles model in my Rails 3 application. The article model has a column called categories which is set using a select box in the new form view. (It's a select box because the options should never change and there are only four of them).
The index view code I have is as follows:
<% #articles.category.each do |article| %>
<%= article.category %>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.author %></td>
<td><%= article.category %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
<% end %>
I have grouped by category in my controller:
#articles = Article.group(:category).order('title ASC')
However, this results in an exception which points to the following line <% #articles.category.each do |article| %>.
What is the tidiest (from within my view code) way of achieving the following:
Category 1
Article 1
Article 2
Category 2
Article 5
Category 3
Article 8
So each article is listed under it's category.
I'd suggest you to use group_by method (Documentation):
# in your controller
articles = Article.order('title ASC')
#grouped_articles = articles.group_by &:category
# in your view
<% #grouped_articles.each do |category, articles| %>
<%= category %>
<% articles.each do |a| %>
<tr>
<td><%= a.title %></td>
<td><%= a.author %></td>
<td><%= link_to 'Show', a %></td>
<td><%= link_to 'Destroy', a, confirm: 'Are you sure?', method: :delete %> </td>
</tr>
<% end %>
<% end %>

habtm and nested form result to nil

I have 3 models, Product, Variation and Color. I'm using the nested_form gem.
Product has_many :variations
Variation belongs_to :product
Variation has_and_belongs_to_many :colors
Color has_and_belongs_to_many :variations
Through Product form I have nested_form for Variations. I want to associate colors through checkbox but receive undefined local variable or method "color_ids"
Product model
def new
#product = Product.new
1.times { #product.variations.build }
end
def create
#product = Product.new(params[:product])
...
end
My form //edited//
<%= nested_form_for(#product) do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="inline-form">
<%= f.fields_for :variations %>
<p><%= f.link_to_add "Add a variation", :variations %></p>
</div>
<div class="actions">
<%= submit_or_cancel(f) %>
</div>
</div>
<% end %>
And nested form is a basic table with
<table id="new_item">
<tr>
<th>Name</th>
<th>Color</th>
</tr>
<tr>
<td><%= f.text_field :name, :size => 40 %></td>
<td><% for color in Color.all %>
<%= check_box_tag 'variation[color_ids][]', color.id, variation.color_ids.include?(color.id), :id => dom_id(color) %><%= label_tag dom_id(color), color.name, :class => "check_box_label" %>
<% end %>
</td>
</tr>
</table>
I'm guessing that your issue is the part of the form doing color_ids.include?(color.id). I'd need to see the rest of your form erb to tell you how to fix it.
It's going to be something like variation.color_ids.
Another thing to note, that style of for loop is odd to see in typical/idiomatic ruby.
This is more typical:
<% Color.all.each do |color| %>
<%= check_box_tag 'variation[color_ids][]', color.id, color_ids.include?(color.id), :id => dom_id(color) %><%= label_tag dom_id(color), color.name, :class => "check_box_label" %>
<% end %>

How to get selected value in select in rails 3?

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]