How to render slightly difference partial in "index" page and "show" page - ruby-on-rails

There is a model Company that has many DailyDatum.
I want to show the daily data in companies/:id/daily_data and daily_data/index. But in the company's page I don't want to show company.name column.
views/daily_data/_daily_datum.html.erb
<tr>
<td><%= daily_datum.company.name %></td>
# This company.name needs to be shown when the partial is called from daily data index.
<td><%= daily_datum.column1 %></td>
<td><%= daily_datum.column2 %></td>
</tr>
views/daily_data/index.html.erb
<table>
<thead>
<tr>
<th>Company Name</th>
<th>Daily Datum1</th>
<th>Daily Datum2</th>
</tr>
</thead>
<%= render #daily_data %>
</table>
views/companies/daily_data.html.erb
<table>
<thead>
<tr>
<!--<th>Company Name</th>-->
<th>Daily Datum1</th>
<th>Daily Datum2</th>
</tr>
</thead>
<%= render #daily_data %>
</table>
How should I handle situation like this? Does I need to create another partial HTML?

This might be overkill since you're only trying to conditionally render one single field, but the "right" approach would be to create a helper.
I'd recommend creating a helper to conditionally render one of two partials for #daily_data depending on the path.
companies_helper.rb
def is_companies_index_path?
current_page?(companies_index_url)
end
def is_companies_show_path?
current_page?(companies_show_url)
end
def render_appropriate_partial
render 'daily_data_a' if is_companies_index_path?
render 'daily_data_b' if is_companies_show_path?
end
Then in your views you can simply call:
<% render_appropriate_partial %>
And it will render the appropriate partial based on which route/url your on.

You can use Routing Paramters to know what is the current action/controller, so with an if condition you can add the company name or not.
<% if action_name == 'index' && controller_name == 'daily_data' %>
<th>Company Name</th>
<% end %>

Related

How to display value of a field from a different table by using the value of a different column in another table

I realize the heading is a little confusing but my problem is quite simple. I hae two models in my rails 5 app. User and Expense. Each expense belongs_to a user. I have an index page where all expenses are being listed. I can list the user IDs for each expense from the expenses table but I want to instead look up the name of the user (in column username) in the users table and display it with the expense instead. The view I have written is below. But it doesn't work.
<p id="notice"><%= notice %></p>
<h1>Teamjournals</h1>
<table style="padding: 2px; width: 50%" border="2px" align="center">
<thead>
<tr>
<td align="center"><%= link_to new_expense_path, :class =>"btn btn-success btn-wide" do%>Add New Expense<% end %></td>
</tr>
<tr>
<th>User</th>
<th>Expense Date</th>
<th>Currency</th>
<th>Expense Amount</th>
<th>Description</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #expenses.each do |expense| %>
<tr>
<td><%= User.joins(:expense).where('expense.user_id = ?', #user.id) %></td>
<td><%= expense.expense_date %></td>
<td><%= expense.currency.currency %></td>
<td align="right"><%= expense.expense %></td>
<td><%= expense.description %></td>
</tr>
<% end %>
</tbody>
</table>
Ok so in your iteration over #expenses you have this line:
<%= User.joins(:expense).where('expense.user_id = ?', #user.id) %>
you can change it to this:
<% user = expense.user %>
Note that I'm using <% not <%= because I'm just trying to assign a variable, not print the output to html.
Then after defining user you can say <%= user.name %>.
You should read a bit more about active record associations, but here's a few side comments about the query you've shown
User.joins(:expense).where('expense.user_id = ?', #user.id)
In this case, you should use the method generated by belongs_to instead of writing a query. But in situations where you do want to write a custom query, you should only be using where when you want to get an array. In this case you're looking for a single record so you could use find_by. Furthermore, the joins you're doing here is unnecessary
# any of these works
user = User.where('id = ?', expense.user_id).first
user = User.where(id: expense.user_id).first
user = user.find_by(id: expense.user_id)

How to properly refactor a simple if else logic in views for RoR [duplicate]

This question already has answers here:
Rails: An elegant way to display a message when there are no elements in database
(10 answers)
Closed 8 years ago.
I'm a newbie at Rails and I'm having trouble wrapping my head around refactoring logic from views. Let's say I have a simple Post model. In the index view, I want specific content to be displayed if there are posts or not. Basically, if there are any posts, display this specific content or else this other content.
Here is my index.html.erb view for Posts:
<div class="content">
<% if #posts.any? %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<p>There are no posts!</p>
<% end %>
</div>
Now, the way I refactored this was by creating a couple of helpers and partials like so:
posts_helper.rb (which renders the partials according to the if logic):
module PostsHelper
def posts_any
if #posts.any?
render 'this_content'
else
render 'this_other_content'
end
end
end
In the partials, I just used the exact content in the if else statement.
_this_content.html.erb partial:
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
</tr>
</thead>
<tbody>
<% #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
</tr>
<% end %>
</tbody>
</table>
_this_other_content.html.erb partial:
<p>There are no posts!</p>
Finally, the refactored index.html.erb (which would call the helper method):
<div class="content">
<%= posts_any %>
</div>
The problem is, I'm just not convinced that this is the correct Rails way of refactoring. If any of you could shed some light on this, I would highly appreciate it!
Thanks!
You're doing it right, and better than many people I know. :)
A few minor adjustments...
I would move the render from the helper to the erb, and just use the helper to return the right name of what to render.
Your erb code and helper code:
<%= posts_any %>
def posts_any
if #posts.any?
render 'this_content'
else
render 'this_other_content'
end
end
I suggest:
<%= render posts_any %>
def posts_any
#posts.any? ? 'this_content' : 'this_other_content'
end
Next, I personally like to render a collection using a partial.
Yours:
<% #posts.each do |post| %>
I suggest:
<%= render partial: "post", collection: #posts %>
And in the comment below, user kyledecot suggests even terser:
<%= render #posts %>
Then create the file _post.html.erb like this:
<tr>
<td><%= post.title %></td>
<td><%= post.content %></td>
</tr>
Some developers think that it's overkill to render a collection using a partial, in the case where the partial is not used anywhere else.
I personally think it's helpful, and especially useful when a project has multiple coders some of whom may be changing the table row data results.

Ruby on rails-Tabular form for input taken from user

I want to print in tabular form the entries given by user. The table will contain 3 columns for name,registration number and classes attended, however the output is not as expected. Its first printing all the names and then all the registration numbers and so on. I want to print a name and the registration number according to name and classes attended.
</head><table width="100%">
<tr>
<th>NAME</th>
<th>REGISTRATION NUMBER</th>
<th>CLASSES ATTENDED</th>
</tr>
<tr>
<% #name_students.each do |t| %>
<th><%= t.name_student %></th><br>
<% end %>
<% #reg_nos.each do |t| %>
<th><%= t.reg_no %></th><br>
<% end %>
<% #classes_ats.each do |t| %>
<th><%= t.classes_at %></th><br>
<% end %>
</tr>
</table>
Here is my controller action.
class PagesController < ApplicationController
def home
#name_students = Page.all
#reg_nos = Page.all
#classes_ats = Page.all
end
def list
#name_students = Page.all
#reg_nos = Page.all
#classes_ats = Page.all
end
def add
Page.create(:name_student => params[:nam])
Page.create(:reg_no => params[:reg])
Page.create(:classes_at => params[:cls])
redirect_to :action => 'home'
end
end
If I understand what you're doing, you should probably have PagesController#home return something like #pages = Page.all object and display the data kind of like this:
<thead>
<tr>
<th>Name</th>
<th>Registration Number</th>
<th>Classes Attended</th>
</tr>
</thead>
<tbody>
<% #pages.each do |p| %>
<tr>
<td><%= p.name %></td>
<td><%= p.registration_number %></td>
<td><%= classes_attended(p.classes_attended) %></td>
</tr>
<% end %>
</tbody>
Above, classes_attended(p) is a call to a helper method that you would use to map the class names of the classes that student attended into an display-able array. Really, that kind of display logic might be better in a decorator, but a helper method should be fine for now.
Let me know if I've totally misunderstood what you're doing, and I'll delete my answer.
Edit to add:
Example index method:
def home
#pages = Page.all
end
Also, looking at your question again, is there a reason you're creating three Page objects with one attribute each instead of one Page object with all three attributes? It should probably look something like Page.create(:name_student => params[:nam], :reg_no => params[:reg], :classes_at => params[:cls]). That's pretty much the only way the solution I posted will work. Again, though, I might be totally misunderstanding what you're going for.

Rails 3: retrieving different attribute depending on loop count?

I have a loop in one of my views to display a table like so:
Each category object has 5 attributes called: level_1, level_2, level_3, level_4, level_5.
There will be an array with all the category objects. so there could be any amount of categories and no necessarily 3.
what would the best way to draw this up be? i have something like this at the moment but dont know how to select the relevant category level attribute in the 5.times loop.
<table border="0">
<tr>
<th>Maturity</th>
<% for category in #categories %>
<th><%= category.category_title %></th>
<% end %>
</tr>
<% 5.times do |i|%>
<% level = 5 - i %>
<tr>
<td>Level <%= level %> Maturity</td>
<% for category in #categories %>
<td><%= category.level_ #put in the level value here so it selects the relevant attraibute %></td>
<% end %>
</tr>
<% end %>
</table>
you need to change the rendering of level with this:
<% for category in #categories %>
<td><%= category.send("level_#{level}") %></td>
<% end %>
send is used to call a method on an object so that you can compose your method at runtime.
If you categories as variable no. then you shouldn't make it columns, but rows. And then levels will be you columns e.g.
<table>
<tr>
<th>Level 1</th>
<th>Level 2</th>
<th>Level 3</th>
<th>Level 4</th>
<th>Level 5</th>
<tr>
<% #category.each do |c| %>
<tr>
<td>#category.level_1<td>
<td>#category.level_2<td>
<td>#category.level_3<td>
<td>#category.level_4<td>
<td>#category.level_5<td>
<th>
<% end %>
Now in above code you may replace hard coded level_#{no} with iterations.

Rails Has Many Forms

I'm having a bit of trouble getting forms for a has_many association to work for a shopping basket. The view is displaying the basket and has a table row for each item. Each row contains a text field so that the user can set the quantity.
The problem is that only the first item row quantity is being passed through in params, even if the second item's quantity has changed too.
Can anyone help please?
Thanks,
Roger
The output of params in the debugger is below, only one line_item is being passed through.
(rdb:2624) e params
{"commit"=>"Recalculate",
"authenticity_token"=>"7TKnhmbBPFiKLzVqTipzH8PDyCrOnKiFixGQ37XDGNY=",
"_method"=>"put", "utf8"=>"✓", "action"=>"update", "id"=>"4",
"line_item"=>{"quantity"=>"3", "id"=>"6"}, "controller"=>"baskets"}
app/controllers/basket_controller.rb
class BasketsController < ApplicationController
def update
begin
#basket = Basket.find(params[:id])
# Can't do anything here yet since the correct parameters aren't being passed through.
rescue ActiveRecord::RecordNotFound
logger.error "..."
end
redirect_to basket_path
end
end
app/views/baskets/show.html.erb
<%= form_for #basket do |f| %>
<table id="items">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<% #basket.line_items.each do |item| %>
<%= form_for item do |g| %>
<tr class="<%= cycle('alternate', '') %>">
<td><%= item.product.name %></td>
<td>
<span class="decrement-quantity"><b>-</b></span>
<%= g.text_field :quantity %>
<span class="increment-quantity"><b>+</b></span>
</td>
<td class="price"><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>
<% end %>
<tr class="totals">
<td>Total</td>
<td><%= #basket.total_quantity %></td>
<td class="price"><%= number_to_currency(#basket.total_price) %></td>
<td></td>
</tr>
</tbody>
</table>
<%= f.submit 'Recalculate' %>
<% end %>
You're just creating a new form within the other form. Rails doesn't do anything magical just because you nest one form within another - which is what's causing the issue you're seeing.
The way to handle this situation is to use the fields_for helper and nested_attributes_for - see NestedAttributes for more information too.
I would checkout Railscasts: Complex Forms Part 1. After you watch that, you may be interested in watching Parts 2 & 3.
Ryan Bates covers using fields_for in an understandable and easy to learn fashion.

Resources