Ruby on rails-Tabular form for input taken from user - ruby-on-rails

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.

Related

Active record is not display table

As working on the Active Record as i have work on different function for active reocrd like Avg, sum and count as it display working fine and also Chart,
but one things is baffle me and i still cannot get it working and it should be working fine, as i cannot get display all data list table like
<table id="dttb" class="table table-hover">
<thead>
<tr>
<th> full name </th>
</tr>
</thead>
<tbody>
<tbody>
<% #user.each do |user| %>
<tr>
<td><%= user.fullname %></td>
</tr>
<% end %>
</tbody>
</tbody>
</table>
as it should be working as the error is kept displayed
undefined method `each' for nil:NilClass
as I look up information and most of them are mention .each do, seems I am doing wrong as I have used
<%= User.count(:user) %>
and
<%= column_chart User.group(:provider).count(:user) %>
and it seems working fine as query function.
so I tried again with find_each
<% User.find_each do |user| %>
<tr>
<td><%= puts user.fullname %></td>
</tr>
<% end %>
and the error is gone but it does not display at the data and it's show blanks unless I put 'link_to' but they keep display like
and I have put on AdminController.rb
class AdminController < ApplicationController
before_action :authenticate_user!
def index
#user = User.all
#tools = Tool.all
end
end
seems I miss something, I have look google or stackover flow, most of them answer are very same as this code as I wrote
Update: as I am able to get some data like a phone number or email
Here is code i wrote
<% User.find_each do |user| %>
<tr>
<td><%= link_to user.id, user %></td>
<td><%= link_to user.email, user %></td>
<td><%= link_to user.created_at.strftime('%v'), user %></td>
<td><%= link_to user.fullname, user %></td>
<td><%= link_to user.phone_number, user %></td>
</tr>
<% end %>
but frustration with fullname as it should be displayed but it not
Set #user (or better #users) in the controller:
def index # or the actual action name
#users = User.all # or User.order(:fullname)
end

Facing issues with link_to "Edit" in rails

I am trying to display all the metrics in a table format with an edit option. But, I end up with the below error
In Index view, I am able to see all the data. But when I click on edit link, it is not redirecting to edit view where I have different columns to be showed.
index view:
<%= form_for :metrics_controller, url: metrics_path(#metric), method: :get do |f| %>
<table id="metrics">
<thead>
<tr id="AllMetricColumnNames">
<th id="CommentsColumn">Comments</th>
<th id="EditColumn">Edit</th>
</tr>
</thead>
<% #metricAll.each do |data| %>
<tr id="AllMetricValues">
<td id="Comments"><%= data.Comments %></td>
<td id="EditButton"><%= link_to "Edit", edit_metric_path(#metricAll) %></td>
<% end %>
</tr>
</table>
<% end %>
Controller:
class MetricsController < ApplicationController
def index
#metricAll = Metric.all
end
def show
#metric = Metric.find(params[:id])
end
def edit
#metric = Metric.find(params[:id])
end
private def post_params
params.require(:metric).permit(:Metric, :Comments)
end
end
routes:
root 'metrics#index'
get 'index' => 'metrics#index'
get 'edit' => 'metrics#edit'
resources :metrics
You're passing ALL the metrics for the edit route. Move from
<td id="EditButton"><%= link_to "Edit", edit_metric_path(#metricAll) %></td>
to
<td id="EditButton"><%= link_to "Edit", edit_metric_path(data) %></td>
data is the current metric in your code
According to your screenshot, the error is within the model.
Also, as mentioned by others, you should remove those get routes as the resources :metrics will generate the necessary routes for all your CRUD actions a.ka. for the index, show, edit, new, create, update, destroy.
My guess is that the metric.rb file has a belongs_to :automated_thresholding relationship but the metrics database table is missing the field automated_thresholding_id.
You should create a migration to add that field
add_reference :metrics, :automated_thresholding

How to render slightly difference partial in "index" page and "show" page

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 %>

Displaying has and belongs to many associations in index

Hey I'm new to Rails and all this so bear with me, thanks!
I have two models:
class User < ApplicationRecord
has_and_belongs_to_many :sports
end
class Sport < ApplicationRecord
has_and_belongs_to_many :users
end
My users have a few different sports that they can choose each. I'm simply trying to display all users in a table, along with which sports they do. However.. the only way I've managed to get anything without an error is by using current_user as shown below. I've been looking how to do this for hours... I know it's going to be stupidly simple but I just can't figure it out or even know how to go in the right direction.
# users_controller.rb
def index
#users = User.all
#sports = current_user.sports
end
# users/index.html.erb
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= link_to user.email, user %></td>
<% #sports.each do |s| %>
<td><%= s.name %></td>
<% end %>
</tr>
<% end %>
That's my current code but obviously this shows only the signed in users associations and repeats it for the other users like this:
<table>
<tr>
<th>Name</th>
<th>Sport 1:</th>
<th>2:</th>
</tr>
<tr>
<td>User 1 (current_user)</td>
<td>Football</td>
<td>Running</td>
</tr>
<tr>
<td>User 2</td>
<td>Football (User 1's Sports)</td>
<td>Running </td>
</tr>
</table>
Thanks in advance.
You can try using the following and deleting #sports = current_user.sports:
<% user.sports.each do |s| %>
<td><%= s.name %></td>
<% end %>
using user.sports while looping through each of the user will lead to N+1 queries on your database. You can change your controller method to something like
def index
#users = User.all.eager_load(:sports)
end
and then in html
<% user.sports.each do |s| %>
<td><%= s.name %></td>
<% end %>
This will load users along with left_outer_join on sports table and this will save to lot of extra queries on your database.
For Info you can refer this good blog.
Thanks

Ruby on Rails: Push Posts in to a hash and then show them

I'm creating a twitter-copy and right now I'm trying to get all the posts from all the users you follow and then show them on the home page. I've done this in PHP before, but I'm new at RoR so I might be trying to do this the wrong way.
a User has many Subscriptions
and a Subscription belongs to User
a User has many Posts
and a Post belongs to User
This is what i've got so far:
session_controller.rb
def get_posts
#sub = #current_user.subscriptions.first
Post.where("user_id = ?", #sub.following_id).find_each do |tweet|
render partial: 'shared/tweet', locals: {tweet: tweet}
end
end
I know .first gets only the first subscription, but I wanted to try to get just something out.
home.html.erb
<table>
<tr>
<th>Username</th>
<th>Tweet</th>
</tr>
<%= yield %>
</table>
_tweet.html.erb
<div class="tweet">
<td>Username here somehow</td>
<td><%= tweet.content %></td>
</div>
But right now nothing is coming up in the table.. So, what am I doing wrong? (am I doing anything right at all?)
Try this:
session_controller.rb
def get_posts
#sub = #current_user.subscriptions.first
#tweets = Post.where("user_id = ?", #sub.following_id)
end
home.html.erb
<table>
<thead>
<tr>
<th>Username</th>
<th>Tweet</th>
</tr>
</thead>
<tbody>
<% #tweets.each do |tweet| %>
<%= render 'shared/tweet', tweet: tweet %>
<% end %>
</tbody>
</table>
_tweet.html.erb
<tr class="tweet">
<td><%= tweet.user.name %></td> # Not sure
<td><%= tweet.content %></td>
</tr>
EDIT:
To get all the tweets for all the subscritions:
following_ids = #current_user.subscriptions.map(&:following_id)
#tweets = Post.where(user_id: following_ids)

Resources