Display Table in "Show" Page using the same related ID - ruby-on-rails

I wanted to show the others related Items of a same thing when finishing adding something.
I have a normal crud scaffold, so when I add a "Task" which is related to a project, I wanted to when it redirect to "Show" to a table be formed showing other "tasks" of the same related project...
show.html.erb
<table>
<thead>
<tr>
<th>Seq</th>
<th>Descr</th>
<th>Seqpai</th>
<th>Type</th>
<th>Hour</th>
<th>Pid</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #lookup.each do |lookup| %>
<tr>
<td><%= task.seq %></td>
<td><%= task.descr %></td>
<td><%= task.seqpai %></td>
<td><%= task.typo %></td>
<td><%= task.hour %></td>
<td><%= task.projeto.name %></td>
</tr>
<% end %>
</tbody>
tasks controller =>
def lookup
#taskete = Task.where(#projeto_id)
end
Tasks Belong to Projeto and Projeto has many Tasks

I don't understand what's the problem. But if the source code of your controller you define variable called "#taskete", then in the view you are iterating over "#lookup".
Your loop in view should be: #taskete.each do |task|

Tasks belong to projecto and projecto has many tasks. So to show all the tasks of the projecto of the current task do...
<% #taskete.projecto.tasks.each do |task| %>
If you only want to show the other tasks (i.e. don't show the current task in the list, only the related tasks) you may want to do...
<% #taskete.projecto.tasks.each do |task| %>
<% next if #taskete == task %>
...which will skip the current loop if the task in the collection is #tasketo

Related

Rails create table row on button click

So this may be slightly confusing but i hope to make it work fine.
So at the moment i have this a table, Which looks like this,
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>EventName</th>
<th>Description</th>
<th>Initial Provider</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<% #newevents.each do |ne| %>
<tr>
<td><%= ne.id %></td>
<td><%= ne.product_name %></td>
<td><%= ne.description %></td>
<td><%= ne.merchant_name %></td>
<td><%= link_to "Edit Event", edit_event_path(ne.id), class: "btn btn-info" %></td>
</tr>
<% end %>
</tbody>
</table>
At the moment i have this loading in from DBTable1
Now, I'm wanting it so that when you click on the edit button. It will take you to a page where it can load in information from DBTable1 but save the information into DBTable2
At the moment. DBTable2 has a DBTable1_id field inside. But i haven't worked out fully how to use it. DBTable1 does have has_one DBTable2 inside
How do i go about making this so that when you edit your actually creating a new row in DBTable2 from the view?
Seems like you need to simply adjust the controller action (or create a new action) that acts just like "update", but creates a new one instead.
Actually, you can probably do this by changing edit_event_path(ne.id) to something else like, create_event_path(ne.id) or new_event_path(ne.id).
Although, don't know enough about your app (including what it's routes are) to be sure.

Remove duplicates in view Rails

A minor, perhaps simple, question here. Let's say my DB returns duplicates. For example, I have multiple rooms that contain different start and end times.
My current view looks like:
<table>
<thead>
<tr>
<th>Location</th>
<th>Status</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #courses.each do |course| %>
<% if course.lec_exam.eql?("LEC")%>
<tr>
<td><%= course.location %></td>
<td><%= course.status %></td>
<td><%= link_to 'Edit Status', edit_course_path(course) %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
I'd like to clean this up a bit and remove the duplicates. Since each course has a location and start and end times, the same location will get displayed multiple times. What is the best approach to prevent this and display the unique locations, and then ensure that the status is correctly marked (i.e. closed means the current time is between the start and end time for each course that uses that location)? I have a few ideas but I'm not certain where to start. I can provide more information as needed.
Thanks!
Probably you can use Distinct SQL operator
http://guides.rubyonrails.org/active_record_querying.html
But I didn't get idea of your seconds part of question

How do I save dynamically generated API content from a view to a SQLite3 database?

I have an application where the user can search for albums by artist. My search makes an API call to iTunes and I display the top 25 results as a simple table:
<table>
<thead>
<tr>
<th>Artist</th>
<th>Album</th>
<th>Genre</th>
<th></th>
</tr>
</thead>
<tbody>
<% #search_results.each do |album| %>
<tr>
<td><%= album['artistName'] %></td>
<td><%= album['collectionName'] %></td>
<td><%= album['primaryGenreName'] %></td>
<td><%= link_to "Add to Library", albums_url, method: 'post' %></td>
</tr>
<% end %>
</tbody>
</table>
In the last column I want to have a button that the user could click which would save that row (a specific album) to the database. I would only be saving the three columns listed to the database.
I'm having a little trouble figuring out the logic behind scraping my own page and saving those objects, and how that would work in my controller. For reference I have a simple Rails 4 application using SQLite3 in development.
The dead simplest way to do this, no JavaScript etc., would be to make a link
= link_to 'Save', albums_url({ :album => { :artist_name => album['artistName'], :collection_name => album['collectionName'], :primary_genre_name => album['primaryGenreName'] }) , method: 'post'
where the hash argument to albums_url(hash_argument) is the parameters for the album.

Rails 3 each do ignore nil values

I am building an html table that should include name, rating1, rating2, and rating3. rating 1-3 come from different models than name.
resources :names do
resource :rat1,:rat2,:rat3
end
Inside of my html table I'd like to include the ratings from within each of these tables but I would like to automatically skip over or ignore tables that are nil. This is because :names may only have a :rat1 and not a :rat2 or :rat3. My view should look something like this.
<table>
<thead>Name</thead>
<thead>Rating 1</thead>
<thead>Rating 2</thead>
<thead>Rating 3</thead>
<% #names.each do |name| %>
<tr>
<td><%= name.nametext %></td>
<td><%= name.rat1.rating %></td>
<td><%= name.rat2.rating %></td>
<td><%= name.rat3.rating %></td>
</tr>
<% end %>
</table>
Except that if name.rat1 is nil it will either a.) replace the value with N/A OR b.) it will leave this field blank and move on to the next.
What is the cleanest way to do this?
::UPDATE::
So my issue is that the name.rat1 is nil and the name.rat1.rating is an undefined method of a nil class so both of these options will throw the same undefined method of a nil class error regardless of the || or helper method. At least thats what my current tests are showing. Any other options? or different workarounds? I'd like to avoid having to put a validation loop like this for every rat1-3
<% unless name.rat1.nil? %>
<%= name.rat1.rating %>
<% end %>
There has to be a simpler way.
I would probably create a helper method in names_helper.rb
def show_rating(rating)
if rating.present?
rating
else
"default value"
end
end
Then use it in the view:
<%= show_rating name.rat1.rating %>
OFFTOPIC Your table structure is wrong. It should have <thead><tr><th>Name</th><th>Rating1</th>..so on..</tr></thead>
So, in your case you can use the condition while rendering the rating values as:
<table>
<thead>
<tr>
<th>Name</th>
<th>Rating 1</th>
<th>Rating 2</th>
<th>Rating 3</th>
</tr>
</thead>
<tbody>
<% #names.each do |name| %>
<tr>
<td><%= name.nametext %></td>
<td><%= name.rat1.rating || 'N/A' %></td>
<td><%= name.rat2.rating || 'N/A' %></td>
<td><%= name.rat3.rating || 'N/A' %></td>
</tr>
<% end %>
</tbody>
</table>

Displaying data from a collect in rails

I currently have models that can be described as follows:
Songs can have many setlists through Allocations
Allocations belong to songs and setlists
Setlists can have many songs in them through allocations
Songs have a title, artist, and a musical key.
Basically I'm setting up the new setlist view where a musician can select any existing songs from the library to add to a setlist. I want to do something along these lines:
<thead>
<th>Title</th>
<th>Artist</th>
<th>Root Key</th>
</thead>
<tbody>
INSERT CODE HERE TO DISPLAY DATA
</tbody>
At the moment I'm using the following code to get the data but I don't know if there's a way to separate it out into the relevant cells in the table:
<% songs = Song.all.collect {|s| [ s.title, s.artist, s.key ] } %>
<% songs.sort! %>
I'm not sure if this is this is the best way to go about doing this so if anyone could suggest an alternative that would be fantastic too. Thanks in advance!
Fetching data is controller's responsibility.
def index
#songs = Song.select([:title, :artist, :key]).all
end
And view:
<tbody>
<% #songs.each do |song| %>
<tr>
<td><%= song.title %></td>
<td><%= song.artist %></td>
<td><%= song.key %></td>
</tr>
<% end %>
</tbody>
I managed to solve the problem as follows:
<tbody>
<% songs.each do |n| %>
<tr>
<td><%= n[0], '#' %></td>
<td><%= n[1], '#' %></td>
<td><%= n[2], '#' %></td>
</tr>
<% end %>
</tbody>
I'm still not sure whether or not this is best practice or not so if anyone know of a better way please let me know. Thanks :)

Resources