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
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 have an invoice controller and two model invoice and invoice_line_items and create a tax_line_items using invoice_line_item class. Below is my code :
Here is my controller code :
class InvoicesController < ApplicationController
def new
#menu = 'Income'
#page_name = 'Create new invoice'
#invoice = Invoice.new
#invoice.invoice_line_items.build
#invoice.tax_line_items.build
respond_to do |format|
format.html
format.xml { render :xml => #invoice }
end
end
end
My model for invoice:
class Invoice < ActiveRecord::Base
has_many :invoice_line_items
has_many :tax_line_items, :class_name => "InvoiceLineItem",:dependent => :destroy
accepts_nested_attributes_for :invoice_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
accepts_nested_attributes_for :tax_line_items, :reject_if => lambda {|a| a[:account_id].blank? } , :allow_destroy => true
end
and in my forms i used :
<%= form_for(#invoice) do |f| %>
<%= render 'shared/form_error', :object => #invoice %>
<% #invoice.invoice_line_items.each_with_index do |invoice_line_item, index| %>
<%= render "invoice_line_items", :invoice_line_item => invoice_line_item, :index => index %>
<% end %>
<% #invoice.tax_line_items.each_with_index do |tax_line_item, tax_index| %>
<%= render "tax_line_items", :tax_line_item => tax_line_item, :tax_index => tax_index %>
<% end %>
<%end%>
partials :
1) invoice_line_items:
<tr id="row<%= index %>" valign="top" >
<%= hidden_field_tag "invoice[invoice_line_items_attributes][#{index}][id]",invoice_line_item.id%>
<td valign="top">
<%= select_tag "invoice[invoice_line_items_attributes][#{index}][account_id]", options_from_collection_for_select(#from_accounts, :id, :name,:selected => invoice_line_item.account_id ), :include_blank => true, :class=>"full" %>
<!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
</td>
<td><%= text_area_tag "invoice[invoice_line_items_attributes][#{index}][description]",invoice_line_item.description, :class => "full", :cols => 10, :rows=>1 %></td>
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][quantity]",invoice_line_item.quantity, :class => 'full', :id => 'quantity', :onkeydown => "return numbersOnly(event);", :size => 5, :maxlength => 25 %></td>
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][unit_rate]",invoice_line_item.unit_rate, :class => 'full', :id => 'unit_cost', :onkeydown => "return numbersOnly(event);", :size => 5, :maxlength => 20 %></td><!--Jquery code is in application.js-->
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][discount_percent]", invoice_line_item.discount_percent, :class => 'full', :id => 'discount', :onkeydown => "return numbersOnly(event);", :maxlength => 5, :size => 5%></td>
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][amount]", invoice_line_item.amount, :class => 'full', :id => 'total', :readonly => 'readonly', :size => 5%></td>
<td><%= link_to image_tag("/images/black_icon/ic_cancel.png"),{:action => :remove_line_item, :index => index}, :remote => true unless index == 0 %></td>
</tr>
and 2) tax_line_items:
<tr id="tax_row<%= tax_index %>" valign="top" >
<%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][id]",tax_line_item.id%>
<%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][tax]", tax_line_item.tax, :value => 1 %>
<td style="background:#EDF4FF"></td>
<td class="ta-right" style="background:#EDF4FF" colspan="2"><label>Add Tax:</label></td>
<td class="ta-right" colspan="2" style="background:#EDF4FF">
<%= select_tag "invoice[tax_line_items_attributes][#{tax_index}][account_id]", options_from_collection_for_select(#tax_accounts, :id, :name,:selected => tax_line_item.account_id ), :include_blank => true, :class=>"full" %>
<!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
</td>
<td style="background:#EDF4FF"><%= text_field_tag "invoice[tax_line_items_attributes][#{tax_index}][amount]", tax_line_item.amount, :class => 'full', :id => 'tax', :onkeydown => "return numbersOnly(event);", :size => 5%></td>
<td style="background:#EDF4FF"><%= link_to image_tag("/images/black_icon/ic_cancel.png"),{:action => :remove_tax_item, :tax_index => tax_index}, :remote => true %></td>
</tr>
I can add new line item entry easily and edit it when their is no tax item. The problem is
when their is an tax item created in any entry and later try to edit this entry then my line items row appears multiple time for example suppose i have a tax item them a new row will appear for invoice line item ans an extra row for tax line items. i think this will happen because i have used a common table for both the line items. I did this because i have to save both line items entry in same table as both accounts came from same table.
I am very thankful if anyone have the right answer.Thanks a lot
As this answer suggests, you need to add a 'type' column to this table to distinguish between invoice and tax items. It will remove a lot of complications from your code as Rails will do the work of identifying which record belongs to which model class from the common table.
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.
i am facing a new situation. I know someone definitely had faced these type of situation:
I have an invoices table and an invoice_line_items table. Till now every thing is going well with has_many and belongs to association. Now i want to add tax accounts in invoice items, for that i don't want to create a separate table, hence i make below changes in my invoice model :
invoice.rb:
class Invoice < ActiveRecord::Base
has_many :invoice_line_items
has_many :tax_line_items, :class_name => "InvoiceLineItem", :dependent => :destroy
accepts_nested_attributes_for :invoice_line_items, :reject_if => lambda {|a|a[:account_id].blank? } , :allow_destroy => true
#validations
validates_presence_of :invoice_line_items
validates_associated :invoice_line_items
end
invoice_line_item.rb:
class InvoiceLineItem < ActiveRecord::Base
belongs_to :invoice
end
and in my controller i did:
class InvoicesController < ApplicationController
def new
#invoice = Invoice.new
#invoice.invoice_line_items.build
#invoice.tax_line_items.create
#from_accounts = TransactionType.fetch_from_accounts(current_company.id,'sales')
#to_accounts = TransactionType.fetch_to_accounts(current_company.id, 'sales')
#tax_accounts = TransactionType.fetch_from_accounts(current_company.id, 'tax')
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #invoice }
end
end
end
in my form i did:
for tax accounts
<% #invoice.invoice_line_items.each_with_index do |invoice_line_item, tax_index| %>
<%= render "tax_line_items", :invoice_line_item => invoice_line_item, :index => tax_index %>
<% end %>
and for invoice items:
<% #invoice.invoice_line_items.each_with_index do |invoice_line_item, index| %>
<%= render "invoice_line_items", :invoice_line_item => invoice_line_item, :index => index %>
<% end %>
and here are my partials:
1)_invoice_line_items.html.erb:
<tr id="row<%= index %>" valign="top" >
<%= hidden_field_tag "invoice[invoice_line_items_attributes][#{index}][id]",invoice_line_item.id%>
<td valign="top">
<%= select_tag "invoice[invoice_line_items_attributes][#{index}][account_id]", options_from_collection_for_select(#from_accounts, :id, :name,:selected => invoice_line_item.account_id ), :include_blank => true, :class=>"full" %>
<!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
</td>
<td><%= text_area_tag "invoice[invoice_line_items_attributes][#{index}][description]",invoice_line_item.description, :class => "full",:rows =>2 %></td>
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][quantity]",invoice_line_item.quantity, :class => 'amount', :id => 'quantity', :onkeydown => "return numbersOnly(event);", :size => 8, :maxlength => 25 %></td>
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][unit_rate]",invoice_line_item.unit_rate, :class => 'amount', :id => 'unit_cost', :onkeydown => "return numbersOnly(event);", :size => 8, :maxlength => 20 %></td><!--Jquery code is in application.js-->
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][discount_percent]", invoice_line_item.discount_percent, :class => 'amount', :id => 'discount', :onkeydown => "return numbersOnly(event);", :maxlength => 5, :size => 8%></td>
<td><%= text_field_tag "invoice[invoice_line_items_attributes][#{index}][amount]", invoice_line_item.amount, :class => 'full', :id => 'total', :readonly => 'readonly', :size => 5%></td>
<td><%= link_to 'Remove',{:action => :remove_line_item, :index => index}, :remote => true unless index == 0 %></td>
</tr>
2) _tax_line_items.html.erb:
<tr id="tax_row<%= tax_index %>" valign="top" >
<%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][id]",invoice_line_item.id%>
<td></td>
<td colspan="2" class="ta-right"><label>Add Tax:</label></td>
<td class="ta-right" colspan="2" style="background:#EDF4FF">
<%= select_tag "invoice[invoice_line_items_attributes][#{tax_index}][account_id]", options_from_collection_for_select(#tax_accounts, :id, :name,:selected => invoice_line_item.account_id ), :include_blank => true, :class=>"full" %>
<!-- <a href="/accounts/new?account_head_id=10" > New item</a> -->
</td>
<td style="background:#EDF4FF"><%= text_field_tag "invoice[invoice_line_items_attributes][#{tax_index}][amount]", invoice_line_item.amount, :class => 'full', :id => 'tax', :onkeydown => "return numbersOnly(event);", :size => 5%></td>
<td><%= link_to 'Remove',{:action => :remove_tax_item, :index => tax_index}, :remote => true %></td>
</tr>
My motto is to save both items accounts and tax accounts in invoice_line_items table's account_id column, i hope some buddy has the answer, thanks in advance
The issue you have is #invoice.invoice_line_items and #invoice.tax_line_items will return the same values since they both are using the same id to join.
Seems like you could use Single Table Inheritance (you will have to scroll down to find the section in the API). Using a type column in the invoice_line_items table, you can have model inheritance.
class InvoiceLineItem < ActiveRecord::Base
belongs_to :invoice
end
class TaxLineItem < InvoiceLineItem
belongs_to :invoice
end
then you can cleanup you associations in Invoice
class Invoice < ActiveRecord::Base
has_many :invoice_line_items
has_many :tax_line_items, :dependent => :destroy
end
i got the solution any how i did this:
<% #invoice.tax_line_items.each_with_index do |tax_line_item, tax_index| %>
<%= render "tax_line_items", :tax_line_item => tax_line_item, :index => tax_index %>
<% end %>
and in the partial of _tax_line_items :
<tr id="tax_row<%= tax_index %>" valign="top" >
<%= hidden_field_tag "invoice[tax_line_items_attributes][#{tax_index}][id]",invoice_line_item.id%>
<td></td>
<td colspan="2" class="ta-right"><label>Add Tax:</label></td>
<td class="ta-right" colspan="2" style="background:#EDF4FF">
<%= select_tag "invoice[tax_line_items_attributes][#{tax_index}][account_id]", options_from_collection_for_select(#tax_accounts, :id, :name,:selected => invoice_line_item.account_id ), :include_blank => true, :class=>"full" %>
</td>
<td style="background:#EDF4FF"><%= text_field_tag "invoice[tax_line_items_attributes][#{tax_index}][amount]", tax_line_item.amount, :class => 'full', :id => 'tax', :onkeydown => "return numbersOnly(event);", :size => 5%></td>
<td><%= link_to 'Remove',{:action => :remove_tax_item, :index => tax_index}, :remote => true %></td>
</tr>
and this is working for me....
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]