Rails renders my raw data in html - ruby-on-rails

I've setup a Rails project:
User->Project->Sample
When I view my Projects as table everything is fine. I render a table template:
<%= render 'layouts/projects_table' %>
but when I render my samples for a project
<%= render 'layouts/samples_table' %>
I get the table but before the table I get my raw data rendered:
[#<Sample id: 28, name: "abcd", size: 11, quantity: 11.0, created_at: "2013-04-04 09:58:50"> ... ]
ProjectsController:
def show
#project = Project.find(params[:id])
#samples = #project.samples
end
_samples_table:
<table id="samples" class="display">
<thead>
<tr>
<th>
Sample Name
</th>
<th>
Size
</th>
<th>
Quantity
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<%= #samples.each do |sample| %>
<tr>
<td>
<%= link_to sample.name, project_sample_path(#project, sample) %>
</td>
<td>
<%= sample.size %>
</td>
<td>
<%= sample.quantity %>
</td>
<td>
<% if !sample.libraries.any?%>
<%= link_to 'Del', project_sample_path(#project, sample),
:confirm => 'Are you sure?', :method => :delete %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Everything else works fine.
Any help would be appreciated!
Oliver

Remove the = from the loop definition
<%= #samples.each do |sample| %>
should be
<% #samples.each do |sample| %>

You're outputting the return value of .each.
<%= #samples.each do |sample| %>
should be
<% #samples.each do |sample| %>

Related

Layout not rendering properly as expected

In my index action I have
#posts = Post.all
This is my index.html.erb.
<div class="row">
<div class="columns large-12 small-12 medium-12">
</div>
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<%= #posts.each do |post| %>
<tr>
<td> <%= post.id %> </td>
<td> <%= post.title %> </td>
<td> <%= post.body %> </td>
<td > <%= link_to 'Show' %> </td>
<td > <%= link_to 'Edit', edit_post_path(post) %> </td>
<td> <%= link_to 'Destroy', post, method: :delete %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'NEW POST', new_post_path %>
</div>
It is rendering fine except that the #posts object is printing in a form like below just above the table tag in my browser. I know it is something silly but I can't figure that out.
[#<Post id: 4, title: "new title", body: "this is new body", created_at: "2015-12-21 07:44:42", updated_at: "2015-12-21 10:31:31">, #<Post id: 7, title: "new title", body: "dfdsfd", created_at: "2015-12-21 09:40:01", updated_at: "2015-12-21 09:40:01">]
Turns out it was something really really silly! The erb template engine requires = with erb tags to display the data while the logic is written without using the =.
It was just a simple change like this:
<% #posts.each do |post| %> # without the = symbol
hai just remove the '=' in the #posts.each line.
<%#posts.each do |post| %>, thats it.
<div class="row">
<div class="columns large-12 small-12 medium-12">
</div>
<p id="notice"><%= notice %></p>
<h1>Listing Posts</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
<th>Body</th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td> <%= post.id %> </td>
<td> <%= post.title %> </td>
<td> <%= post.body %> </td>
<td > <%= link_to 'Show' %> </td>
<td > <%= link_to 'Edit', edit_post_path(post) %> </td>
<td> <%= link_to 'Destroy', post, method: :delete %> </td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to 'NEW POST', new_post_path %>
</div>

Dynamically update field value in Rails form

I have a form in my ruby app which has a field total_hours_spent and different fields for breakup of the various activities like design_hours, analysis_hours, coding_hours etc.. I am looking for a mechanism where as soon as all the break up hours fields are filled, the total hours field should be updated dynamically. Sad thing is I dont know Ajax or Jquery. I know javascript but don't know how to use it with rails.
So what I am doing now is once the user submits the form, the values are calculated and then stored in the data base. Is there a way to dynamically display the field value using only rails?
Below is the form code: I would like to add two add a field total_hors which would be automatically populated with the value build_hours + test_hours + analysis_hours and other few fields..how to do this?
<%= nested_form_for(#rfsestimation) do |f| %>
<% if #rfsestimation.errors.any? %>
<h2> These errors prohibited the RFS from being saved </h2>
<font color="red">
<ul>
<% #rfsestimation.errors.full_messages.each do |msg| %>
<li><%= msg %> </li>
<% end %>
<% end %>
</ul>
</font>
<div class = "col-md-6">
<h4>IRIS AME RFS Estimation Summary Sheet</h4>
<table class = "table">
<tr> <td> RFS Number </td>
<td> <%= f.text_field :number, :placeholder => "Enter RFS Number" %> </td>
</tr>
<tr> <td> RFS Name </td>
<td> <%= f.text_field :name, :placeholder => "Enter RFS Name" %> </td>
</tr>
<tr> <td> Date of Estimation </td>
<td> <%= f.date_select :date_of_estimation, :placeholder => "Enter Date of Estimation" %> </td>
</tr>
<tr> <td> Request Type </td>
<td> <%= f.select :request_type_id , Rfsestimation.request_types.map { |k, v| [k.humanize, v] } %> </td>
</tr>
<tr> <td>Band</td>
<td> <%= f.select :band_id , Rfsestimation.band_types.map { |k, v| [k.humanize, v] } %> </td>
</tr>
</table>
<br/>
<table class = "table">
<thead>
<tr>
<td>Task</td>
<td>Effort(Hours)</td>
<td>Comments</td>
</tr>
</thead>
<tbody>
<tr>
<td>
Analysis / Requirements Gathering
</td>
<td>
<%= f.fields_for :rfstaskset do |rfs_task| %>
<%= rfs_task.number_field :analysis_hours %>
</td>
<td>
<%= rfs_task.text_area :analysis_comments, :size=> "40x1" %>
</td>
</tr>
<tr>
<td>
Design
</td>
<td>
<%= rfs_task.number_field :design_hours %>
</td>
<td>
<%= rfs_task.text_area :design_comments, :size=> "40x1" %>
</td>
</tr>
<tr>
<td>
Build
</td>
<td>
<%= rfs_task.number_field :build_hours %>
</td>
<td>
<%= rfs_task.text_area :build_comments, :size=> "40x1" %>
</td>
</tr>
<tr>
<td>
Test
</td>
<td>
<%= rfs_task.number_field :test_hours %>
</td>
<td>
<%= rfs_task.text_area :test_comments, :size=> "40x1" %>
</td>
</tr>
<tr>
<td>
UAT Support
</td>
<td>
<%= rfs_task.number_field :UATSupport_hours %>
</td>
<td>
<%= rfs_task.text_area :UATSupport_comments, :size=> "40x1" %>
</td>
</tr>
<tr>
<td>
Deployment Support and
Deployment instruction document
</td>
<td>
<%= rfs_task.number_field :DepSupport_hours %>
</td>
<td>
<%= rfs_task.text_area :DepSupport_comments, :size=> "40x1" %>
</td>
</tr>
<tr>
<td>
contingency
</td>
<td>
<%= rfs_task.text_field :hours_per_day, :value => "10%", :disabled => "true" %>
</td>
<td>
<%= rfs_task.text_area :contingency_comments, :size=> "40x1" %>
</td>
</tr>
</tbody>
<% end %>
</table>
<%= f.submit "Create RFS", class: "btn btn-success"%>
</div>
<div class = "col-md-6">
<table class="table">
<br/><br/>
<tr>
<td> Hours/day : </td> <td> <%= f.text_field :hours_per_day, :placeholder => "Enter hours per day", :value => "8.8", :disabled => "false" %> </td>
</tr>
<tr>
<td> Estimated Start Date : </td> <td> <%= f.date_select :estimated_start_date %> </td>
</tr>
<tr>
<td> Estimated End Date : </td> <td> <%= f.date_select :estimated_end_date %> </td>
</tr>
</table> <br/> <br/> <br/> <br/><br/> <br/><br/>
<%= f.fields_for :rfsnote do |rfs_note| %>
<%= rfs_note.text_area :notes, :size=> "60x20", :placeholder => "Enter RFS Notes" %>
<% end %>
</div>
<% end %>
You can use before_save callback
e.g
before_save :update_total_houres
def update_total_houres
total_hours_spent = design_hours + analysis_hours + coding_hour
end
also using JavaScript
$(document).ready(function(){
$('.hours').change(function() {
update_total_hours();
});
});
function update_total_hours()
{
var total = 0;
var analysis_hours = $(".analysis_hours").val();
var design_hours = $(".design_hours").val();
var coding_hour = $(".coding_hour").val();
total = sum all
//just update the total to sum
$('.total').text(total);
}
You can make callback before_create OR before_save in model, that will store automatically in database, like:
before_create :calculate_total_hours
def calculate_total_hours
total_hours_spent = design_hours, analysis_hours, coding_hours
end
You can also do via jquery, but do something in model will be strong business logic
so if you have the required values in the database,you can directly use it as instance variable and show it in your views..for example
def create
#here you create/save the required data in db
#user=User.new(params[:new])
if #user.save
#get the values you need
#time_used=#user.time_used
#build_hrs=#user.build_hrs
...
...
#and so on
#all these values will be available in the view directly
redirect_to users_show_url
else
render :new
end
end##method ends
end

Ruby on Rails - Delete something from an array

I have a question with this code. In this view I iterate over an array but I want to have the option to delete an entry from the array. Here is my code
view
<table>
<tr>
<th>Ware</th>
<th>Anzahl</th>
<th>Einzelpreis</th>
<th>Gesamtpreis</th>
</tr>
<% counter = 0 %>
<% $itemarray.each do |iarray| %>
<tr>
<% #items.each do |item| %>
<% if iarray.to_i == item.id %>
<th> <%= item.name %> </th>
<th> <%= $anzahlarray[counter] %> </th>
<th> <%= item.price.round(2) %> € </th>
<% preistemp = preistemp = item.price * $anzahlarray[counter].to_f %>
<th> <%= preistemp.round(2) %> € </th>
<th> <%= link_to 'Show', item_path(item) %> </th>
<th> <%= link_to 'Destroy', :hidden_param => 'counter' , method: :deleteshoppingcartentry(counter), data: {confirm: 'Are you sure?' }%></th>
<% end %>
<% end %>
</tr>
<% counter += 1 %>
<% end %>
</table>
controller
def deleteshoppingcartentry
$itemarray.delete_at($entrynumber)
$anzahlarray.delete_at($entrynumber)
redirect_to "/orders/new"
end
maybe you could help me out
You can't delete items from a global array variable. Use a database instead. Learn more about Rails' ActiveRecord.

Render ajax query in Rails

I am tryign to render an Ajax query , but I just dont figure out how.
This is the form:
<%= form_tag(root_path, :method => "post",:id =>"formid", :remote => true) do %>
<%= label_tag(:number1, "El que quiero") %>
<%= text_field_tag :quiero, nil, :class => 'drug_autocomplete' %>
<%= hidden_field_tag :number1 %>
<%= label_tag(:number1, "El modelo que tengo") %>
<%= text_field_tag :tengo, nil, :class => 'drug_autocomplete' %>
<%= hidden_field_tag :number2 %>
<%= label_tag(:size1, "Talla de el que tengo") %>
<%= text_field_tag :size1%>
<%= submit_tag("Do it!",:id =>"submit_but") %>
<% end %>
This is pretty much the controller
def index
size1 = params[:size1]
number1 = params[:number1]
number2 = params[:number2]
quiero = params[:quiero]
tengo = params[:tengo]
if (item1 and item2)
#itemsok = Contribution.where("first_item_id = ?",item1.id).where("second_item_id = ?",item2.id).where("second_item_grade = ?",size1)
end
Ok, so when I push the button to send, I can see that the consult in the database is being made, when I write correct values it returns it and all works perfectly.
What I dont know how to do is to render the items from the query.
In the view I have a table where I want to fill the information in...
I've already done it without AJAX, and it is like this.
<%if not #itemsok.nil?
#itemsok.each do |searches|
%>
<table>
<tr>
<td style="width:100px;"><%= searches.first_item.description %> </td>
<td style="width:150px; font-size:30px;"><%= searches.first_item_grade %> </td>
<td style="width:150px;"><%= searches.second_item.description %> </td>
<td style="width:150px; font-size:30px;"><%= searches.second_item_grade %> </td>
<td style="width:150px; font-size:18px;">Show </td>
</tr>
</table>
Ok, so it takes the variable in the controller, and if is not nill, it renders the data (wich in a normal request, I can access reloading the page)
How can I render the items from my query?
Any hint would be very appreciated.
Thank you.
For example, _search.html.erb
<%if not #itemsok.nil?
#itemsok.each do |searches|
%>
<table>
<tr>
<td style="width:100px;"><%= searches.first_item.description %> </td>
<td style="width:150px; font-size:30px;"><%= searches.first_item_grade %> </td>
<td style="width:150px;"><%= searches.second_item.description %> </td>
<td style="width:150px; font-size:30px;"><%= searches.second_item_grade %> </td>
<td style="width:150px; font-size:18px;">Show </td>
</tr>
</table>
search.js.erb
$('#container').html("<%= escape_javascript(render :partial => 'search/search')%>");

How to manage multiple non nested Models in a rails form

i have two tables(adverb,word) and two controllers(adverbs,words).i want to have one form to add and edit the records of two tables.
adverb controller:
class AdverbsController < ApplicationController
def new
#adverb=Adverb.new
end
def create
#adverb=Adverb.create(params[:adverb])
if #adverb.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def index
#adverb =Adverb.find(:all)
end
def edit
#adverb =Adverb.find(params[:id])
end
def update
if #adverb.update_attributes(params[:adverb])
redirect_to :action => 'index'
else
redirect_to :action => 'edit'
end
end
end
and words controller:
class WordsController < ApplicationController
def new
#word=Word.new
end
def create
#word=Word.create(params[:word])
if #word.save
redirect_to :action => 'index'
else
render :action => 'new'
end
end
def index
#word=Word.find(:all)
end
def edit
#word=Word.find(params[:id])
end
def update
if #word.update_attributes(params[:word])
redirect_to :action => 'index'
else
redirect_to :action => 'edit'
end
end
end
I use Partial form
<html>
<div id="tabs">
<ul>
<li>word </li>
<li>Adverb </li>
</ul>
<div id="tabs-1">
<table >
<tr>
<td align="right">
Artikel :
</td>
<td align="center">
<%= w.label :Artikel ,'der' %><%= w.radio_button :Artikel,'der',:checked =>false%>
<%= w.label :Artikel ,'die' %><%= w.radio_button :Artikel,'die',:checked =>true %>
<%= w.label :Artikel ,'das' %><%= w.radio_button :Artikel,'das',:checked =>false %>
</td>
</tr>
<tr>
<td align="right">
Neues Wort:
</td>
<td>
<%= w.text_field :Name ,:size => '31*45'%>
</td>
</tr>
<tr>
<td align="right">
Synonome :
</td>
<td>
<%= w.text_field :syn ,:size => '31*45'%>
</td>
</tr>
<tr>
<td align="right">
Bedeutung :
</td>
<td>
<%= w.text_area :bedeutung , :size =>'30*45' %>
</td>
</tr>
<tr>
<td align="right">
Beispiel :
</td>
<td>
<%= w.text_field :beispiel ,:size => '31*45' %>
</td>
</tr>
<tr>
<td>
Kommentar:
</td>
<td>
<%= w.text_area :kommentar ,:size =>'30*45' %>
</td>
</tr>
<tr>
<td align="center">
<%= w.submit submit_lable %>
</td>
</tr>
</table>
</div>
<div id="tabs-2">
<%= fields_for (#adverb) do |av| %>
<table>
<tr>
<td align="right">
Adverb :
</td>
<td>
<%= av.text_field :name %>
</td>
</tr>
<tr>
<td align="right">
Bedeutung :
</td>
<td>
<%= av.text_field :bedeutung %>
</td>
</tr>
<tr>
<td align="right">
Synonome :
</td>
<td>
<%= av.text_field :syn %>
</td>
</tr>
<tr>
<td>
<%= w.submit submit_lable %>
</td>
</tr>
</table>
<% end %>
</div>
</div>
</html>
and im my new.html.erb :
<html>
<body>
<%= form_for(#word) do |w| %>
<%= render :partial => 'form',:locals => {:w =>w,:submit_lable =>'mmmm'} %>
<% end %>
</body>
</html>
but an error occured:
NoMethodError in Words#new
Showing /home/babak/kurs/app/views/words/_form.erb where line #197 raised:
undefined method `model_name' for NilClass:Class
how can i solve my problem?
thank you for your helps
I assume this exception is raised when getting the new action of the words controller. In your _form.html.erb, you have the following code:
<%= fields_for (#adverb) do |av| %>
<table>
<tr>
<td align="right">
Adverb :
</td>
<td>
<%= av.text_field :name %>
</td>
</tr>
<tr>
<td align="right">
Bedeutung :
</td>
<td>
<%= av.text_field :bedeutung %>
</td>
...
</table>
<%= end %>
I see nowhere in your new action of the words controller where #adverb is declared. Instance variables default to nil, which is why you see this error.

Resources