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]
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 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 %>
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
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.
This is my partial:
<%= form_for(item, :url => update_cart_path) do |item_form| %>
<tr>
<% #variant = item.variant %>
<%= hidden_field(:variant, :id) %>
<td width="300"><%=item.variant.product.name%> <%= "(" + variant_options(item.variant) + ")" unless item.variant .option_values.empty? %></td>
<td class="price"><%= number_to_currency item.price %></td>
<td class="qty"><%=item.quantity%></td>
<td class="total"><span><%= number_to_currency (item.price * item.quantity)%></span></td>
<td class="edit"><%= link_to(image_tag('/images/admin/icons/edit.png'), '#', :class => 'edit') %>
</td>
</tr>
<% end %>
And I'm calling it as follows:
<%= render :partial =>'shared/cart/line_item' , :collection => #order.line_items, :as => :item %>
I have to create a local variable #variant for the hidden_field to work, and because it doesn't make any sense creating it in the controller, I have created the instance local variable in the view.
Is there a better design for this?
If you are only doing that for the hidden_field helper to work, you can use hidden_field_tag instead:
<%= hidden_field_tag('variant', item.variant.id) %>