Edit wishlist of current_user - ruby-on-rails

I have a problem where on each users profile they have an edit button for a section called wishlist, my problem is that when the user tries to click on the edit button it returns an error saying it cannot find wishlist with id e.g. 34 which is the user.id not the wishlist.id. But from what I can understand from the code that is not what I have wrote.
My code is as followed here:
users/show.html.erb
<div class="post3">
<h1><%= t('.mwg')%></h1><br />
<div id = "table-2">
<table width="240" >
<tbody>
<% Wishlist.where(:user_id => #user).each do |wishlist| %>
<tr><td width="30"><b>1:</b></td><td><%= wishlist.number_1 %></td></tr>
<tr><td width="30"><b>2:</b></td><td><%= wishlist.number_2 %></td></tr>
<tr><td width="30"><b>3:</b></td><td><%= wishlist.number_3 %></td></tr>
<tr><td width="30"><b>4:</b></td><td><%= wishlist.number_4 %></td></tr>
<tr><td width="30"><b>5:</b></td><td><%= wishlist.number_5 %></td></tr>
</tbody>
</table>
</div>
<%if current_user %>
<% if current_user.id == wishlist.user_id %>
<div id="text3"><%= link_to t('.edit'), edit_wishlist_path(#wishlist) %></div><br />
<%end%>
<%end%>
<%end%>
No I tried changing the current_user.id == wishlist.user_id line to the following:
<% if current_user.id == #wishlist.user_id %>
But I get the error of undefined method user_id
If I click on the edit for the original code it tries to find a wishlist who's id matches that of the user but I want it to find a user whos id matches the user_id column.

You need to call edit_wishlist_path(wishlist), not #wishlist.

Related

Error when trying to link to patient profile

I'm getting an error when trying to link_to a patient profile when a provider views his patients list. I have no problem displaying all the names of the patients that belong to the provider but when trying to link to the patient profile I get an undefined method 'id'.
So the way it works is, patients can search for providers and add them to the List model. On the provider side, I just list out all the patients that added that specific provider. Here is my erb code below,
<div class="body">
<div class="body">
<% if #active_patients.count > 0 %>
<table>
<thead>
<tr>
<th>Patient Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% #active_patients.each do |list| %>
<tr>
<td>
<%= list.patient.role.user.first_name %> <%= list.patient.role.user.last_name %>
</td>
<td>
<%= link_to patient_path(id: #patient.id), class: "btn" do %>View<% end %> . #### THIS IS THE LINE
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div class="no-records">
<%= image_tag "icon-no-records", class: "image" %>
<div class="text">You have no patients.</div>
</div><!--no-records-->
<% end %>
</div><!--body-->
</div>
Here is my List model,
class List < ApplicationRecord
belongs_to :membershipable, :polymorphic => true
belongs_to :provider
def patient
membershipable_type=='Patient' ? membershipable : nil
end
def provider_user
patient.try(:user)
end
end
Also here's the error message ->
Let Rails do the work of building the path. Each ActiveRecord model has a to_param method which decides how the instance will be encoded in an URL. By default it returns the model id but it could also be a slug based on the title or another property of the model.
Calling your helper like patient_path(patient) should do the trick.
Additionally, in your current code, you're referring to the previously unused #patient variable, even though it looks like you want to refer to list.patient instead.
Here:
<% #active_patients.each do |list| %>
<tr>
<td>
<%= list.patient.role.user.first_name %> <%= list.patient.role.user.last_name %>
</td>
<td>
<%= link_to patient_path(id: #patient.id), class: "btn" do %>View<% end %> . #### THIS IS THE LINE
</td>
</tr>
<% end %>
you have the variable list available to you. It appears that you get the patient by doing list.patient, as you do here:
<%= list.patient.role.user.first_name %> <%= list.patient.role.user.last_name %>
But, then you try to use a variable called #patient, here:
<%= link_to patient_path(id: #patient.id), class: "btn" do %>View<% end %> .
when you don't have the variable #patient. So, you get your nil error.
Instead, it seems you should do:
<%= link_to patient_path(id: list.patient.id), class: "btn" do %>View<% end %> .
Or, as milgner points out, you could simply do:
<%= link_to patient_path(list.patient), class: "btn" do %>View<% end %> .
Also, you might want to look into the Law of Demeter, which you violate (IMO) when you do:
list.patient.role.user.first_name

Rails class based on index value of in_groups_of

The following is intended to distribute array items into two columns.
<% #entitiquestions.in_groups_of(2, false) do |entitiquestions| %>
<tr>
<% entitiquestions.each do |entitiquestion| %>
<td>
<span data-tooltip class="hint--bottom hint--info" data-hint="<%= entitiquestion.question.query %>">
However, there is a class that should be set based on the index of the array item (if it is in fact an index...)
Can a condition be set somehow based on this position?
You can get the index of the array item by using each_with_index, for example the following will give you a row_index variable which starts from 0 for the first row:
<% #entitiquestions.in_groups_of(2, false).each_with_index do |entitiquestions, row_index| %>
Likewise, you can get the column index with:
<% entitiquestions.each_with_index do |entitiquestion, column_index| %>
Now you have the exact position of the element within the table, so you can use a ternary operator to add a class. As an example, if you wanted to add the highlight class when the row is even you could do:
<span data-tooltip class="hint--bottom hint--info <%= 'highlight' if row_index.even? %>"
Here's a full example:
<table>
<% #entitiquestions.in_groups_of(2, false).each_with_index do |entitiquestions, row_index| %>
<tr>
<% entitiquestions.each_with_index do |entitiquestion, column_index| %>
<td>
<span data-tooltip class="hint--bottom hint--info <%= 'highlight' if column_index.even? %>" data-hint="<%= entitiquestion.question.query %>">
<%= entitiquestion.question.query %>
</span>
</td>
<% end %>
</tr>
<% end %>
</table>

Rails 4: Validate file, then import if conditions met

I am importing a file using SmarterCSV and I have 1 function that validates certain aspects of the file, and then redirects you, displaying the results.
I would like this display to also include a button that says "import", which allows you to import that same file as long as you are satisfied with what is displayed.
How can I pass the file to the second function after redirect without having to select the file again?
# validate file and check display
def check_file
#success, #error = Timecard.check_timecard(params[:file])
redirect_to timecards_path, notice: [#success, #error]
end
#import into database if display is satisfactory
def bulk_upload
x = Timecard.import(params[:file])
redirect_to timecards_path, notice: "Times imported successfully."
end
#view showing display
<% if flash[:notice].is_a? String %>
<%= flash[:notice] %>
<% elsif flash[:notice].is_a? Array %>
<div class="container">
<div class="col-xs-10 col-xs-offset-1">
<table class="table table-hover table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Regular</th>
<th>Overtime</th>
<th>Sick</th>
<th>Vacation</th>
<th>Holiday</th>
</tr>
</thead>
<tbody>
<% notice[0].each do |success| %>
<tr>
<td style="color: green"><%= success[0] %></td>
<% success[1].each do |type| %>
<td><%= type %></td>
<% end %>
</tr>
<% end %>
<% notice[1].each do |failure| %>
<tr>
<td style="color: red"><%= failure[0] %></td>
<td><%= success[1] %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="container-fluid">
<div class="col-xs-12 col-md-6 col-md-offset-3 text-xs-center">
<div class="card card-nav-tabs">
<div class="header header-info">
Import Timecard and Send Email
</div>
<div class="content">
<!-- IMPORT GOES HERE -->
</div>
</div>
</div>
</div>
</div>
<% end %>
There is a nice way to do this. You should move the file to a location within your application and then, store the path of the file in the database against the record you are trying to upload this file to. Consider the following example. Assuming you are uploading this file to a TimeCard record and an instance exist as #time_card
name = params[:upload][:file].original_filename
directory = "public/images/upload"
path = File.join(directory, name)
File.open(path, "wb") { |f| f.write(params[:upload][:file].read) }
#time_card.csv_path = path
Then, redirect the user to the validation error page. Then, after use submits the page you can import the CSV using the file in #time_card.csv_path

trouble coding links which insert search values into ransack form

I've coded in a page in which data from a database is pulled and subsequently when I click on what is displayed it enters a corresponding value into the search function on the application and displays the results, the code can be seen below:
course view:
<!-- Index of all Courses -->
<% provide(:title, "Course") %>
<!--containers for design/layout -->
<div class = "signinstyle">
<div class = "row">
<!--Page information -->
<%= form_tag(degree_new_path, :method => "get", id: "search-data") do %>
<table border="1" class="table">
<thead>
<tr>
<th>Courses</th>
</tr>
</thead>
<tbody>
<% #ads.each do |degree| %>
<tr>
<td> <%= link_to degree.cname, keyword_search_path(search: degree.cname) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Select" %>
<% end %>
<!--closing the design/layout containers -->
</div>
</div>
degree controller (the above view is within this):
class Degree < ActiveRecord::Base
def Degree.search(search)
where("cname LIKE ? OR ucas LIKE ?", "%#{search}%", "%#{search}%")
end
end
search controller (as I use my keyword search in the view):
def keyword_search
#search = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
if params[:search]
#search_degree = Degree.search(params[:search]).order('cname ASC')
end
end
def course
#select = Degree.all.select(:uname, :cname, :ucas, :duration, :qualification, :entry).distinct.order(id: :ASC)
if params[:search]
#select_degree = Degree.search(params[:search])
end
end
I'm trying to replicate the above code so I can click on similar links which will enter data into the ransack search function I have but have been unable to do so, if anybody could help me out it would be appreciated. Below is the code I'm currently trying to get to work:
Searches controller:
def adsearch
#adsearch = Degree.ransack(params[:q])
#data = #adsearch.result
#adsearch.build_condition if #adsearch.conditions.empty?
#adsearch.build_sort if #adsearch.sorts.empty?
end
the view file:
<!-- Index of all Courses -->
<% provide(:title, "Course") %>
<!--containers for design/layout -->
<div class = "signinstyle">
<div class = "row">
<!--Page information -->
<%= form_tag(degree_new_path, :method => "get", id: "search-data") do %>
<table border="1" class="table">
<thead>
<tr>
<th>Courses</th>
</tr>
</thead>
<tbody>
<% #ads.each do |degree| %>
<tr>
<td> <%= link_to degree.subject_group, adsearch_path(name: ucas & value: degree.ucas_letter) %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Select" %>
<% end %>
<!--closing the design/layout containers -->
</div>
</div>
With the last two exerts of code it displays what I'm asking it to display on the initial view but doesn't enter the value I wish it to enter into the ransack search and as such doesn't create a search when clicked upon like the first example does.

undefined method `destroy' for nil:NilClass

My app successfully creates rows for a table every time a user submits some stats. Now I want to provide a delete button to be able to delete some rows.
When I click the delete button I get the error:
undefined method `destroy' for nil:NilClass
Seems like either rails doesn't understand the destroy method in this case for whatever reason, or rails doesn't see anything to destroy, hence the nil:NilClass.
My first question is in identifying which is the case, if either of those.
My second question is fixing it :D
Here's my show.html:
<% provide(:title, "Log" ) %>
<% provide(:heading, "Your Progress Log") %>
<div class="row">
<div class="span8">
<% if #user.status_update.any? %>
<h3>Status Updates (<%= #user.status_update.count %>)</h3>
<table>
<thead>
<tr>
<th>Entry Date</th>
<th>Weight</th>
<th>BF %</th>
<th>LBM</th>
<th>Fat</th>
<th>Weight Change</th>
<th>BF % Change</th>
<th>Fat Change</th>
</tr>
</thead>
<tbody class = "status updates">
<%= render #status_updates %>
</tbody>
<% end %>
</div>
</div>
The "<%= render #status_updates %>" calls the _status_update partial.
<tr>
.
.
.
.
<% if current_user==(status_update.user) %>
<td>
<%= link_to "delete", status_update, method: :delete %>
</td>
<% end %>
</tr>
And then finally, here is the StatusUpdateController
def destroy
#status_update.destroy
redirect_to root_url
end
Here are the parameters on the error page:
{"_method"=>"delete",
"authenticity_token"=>"w9eYIUEd2taghvzlo7p3uw0vdOZIVsZ1zYIBxgfBymw=",
"id"=>"106"}
As you are not showing any code (filters or so) that could assign #status_update, I assume there is none. So, the instance variable #status_update in your in your destroy method is not set. You need to query the class with the object's ID.
Rewrite your destroy method as follows:
def destroy
#status_update = StatusUpdate.find(params[:id])
if #status_update.present?
#status_update.destroy
end
redirect_to root_url
end
Note: Replace the classname StatusUpdate with your actual class name.

Resources