onclick getting the correct data - ruby-on-rails

I have an index view of a table called tasks. On each row I want the user to be able to click on Edit or Add Attach (add an attachment).
This is the code:
<% #workorder.tasks.each do |task| %>
<tr>
<td><%= task.position %></td>
<td><strong><%= link_to task.taskname, task_path(task) %></strong></td>
<td>
<a rel="popover" data-content="<%= task.longdesc %>" data-original-title="Description:"><img src='/images/find.ico' width='20' height='20'></a>
</td>
<td><%= task.employee.try(:employee_full_name) %></td>
<td><%= task.taskstatus.try(:statuscode) %></td>
<td><%= task.attachments.count %></td>
<td>
<a href="#" class="answer" data-type="select" data-pk="1" data-resource="task" data-source="/ratings" data-name="rating_id" data-url="/tasks/<%= task.id %>" data-original-title="Select Rating">
<% if task.rating_id != nil %>
<%= textarea_format(task.rating.ratingname) %>
<% end %>
</a>
</td>
<% if current_user.admin == true %>
<td><%= link_to 'Edit', edit_task_path(task), :class => 'btn btn-mini btn-success' %></td>
<td><%= link_to 'Add Attach', new_attachment_path, :class => 'btn btn-mini btn-primary', :onclick => flash[:task_id] = task.id %></td>
<% end %>
</tr>
The Add Attach links to a page for adding the attachment. In order to have it properly attach to the specific task, I use flash to carry forward the task.id.
But, the onclick is always passing the last task.id instead of each button containing the task.id for that row.
Any idea how I could accomplish this?
Thanks!!

I would think your 'Add Attach' link would look like:
<td>
<%= link_to 'Add Attach',
new_attachment_path(task.id),
:class => 'btn btn-mini btn-primary'
%>
</td>
This assumes you have a route entry that looks something like:
match "tasks/:id/add_attach" => "tasks#add_attach" , :as=>"new_attachment"
Then in your tasks_controller.rb:
def add_attach
#task = Task.find(params[:id])
# I don't know what goes here, not enough code posted
#task.save
end

Related

Views in ruby on rails

I am learning to code in ruby on rails environment. I created two models cars and products. I want to have a main page which have a link to cars and products. On clicking each of them they should display their own view pages. Below are view pages for cars and users respectively :
app/views/cars/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Cars</h1>
<table>
<thead>
<tr>
<th>Make</th>
<th>Color</th>
<th>Year</th>
<th colspan="24"></th>
</tr>
</thead>
<tbody>
<% #cars.each do |car| %>
<tr>
<td><%= car.make %></td>
<td><%= car.color %></td>
<td><%= car.year %></td>
<td><%= link_to 'Show', car %></td>
<td><%= link_to 'Edit', edit_car_path(car) %></td>
<td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Car', new_car_path %>
<h4>Import that data!</h4>
<%= form_tag import_users_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</div>
app/views/users/index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>User</th>
<th>Steps</th>
<th>Distance</th>
<th>Minutes Exercised</th>
<th>Hours of Sleep</th>
<th>Calories Burned</th>
<th colspan="24"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
<td><%= user.user %></td>
<td><%= user.steps %></td>
<td><%= user.distance %></td>
<td><%= user.exercise %></td>
<td><%= user.sleep %></td>
<td><%= user.calories %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
<div>
<h4>Import that data!</h4>
<%= form_tag import_users_path, multipart: true do %>
<%= file_field_tag :file %>
<%= submit_tag "Import CSV" %>
<% end %>
</div>
<%= form_tag import_users_path, multipart: true, class: 'form-inline' do %>
<div class="form-group">
<%= link_to "Export CSV", users_path(format: "csv"), class: 'btn btn-primary' %>
</div>
<% end %>
I don't know how to create single main page and provide links to these views. One example would help me. Thanks in advance guys.
Say suppose you have a main page named home
In your home.html.erb write
<%= link_to "cars" cars_path %>
<%= link_to "users" users_path %>
You probably should use rails scaffold to have a look at the functional pages generated by it at first.
Try this .......
<%=link_to "cars" cars_path(car) %>
<%=link_to "users" users_path(user) %>
This link will send you to show method of the controller (users/cars). Then you need to create a show page in your views/cars/show.html.erb and views/users/show.html.erb then you can display their own view/show pages.
Hope this will work for you.
You need to understand concept of routes in rails.
Run command rake routes in your project folder. You will see mapping like this.
Prefix Verb URI Pattern Controller#Action
companies GET /companies(.:format) companies#index
So according to routes, following link will lead to index action of companies controller.
<%= link_to 'Companies', companies_path%>
As you can observe, in companies_path, companies is prefix shown in routes.
For your specific case, following links will work. In your
home.html.erb
<%=link_to "All Cars", cars_path%>
<%=link_to "All Users", users_path%>
Remember there is comma between "All Users" and users_path. Because link_to is just a method with multiple arguments.
For more info,
http://apidock.com/rails/v4.0.2/ActionView/Helpers/UrlHelper/link_to

how to render my form into the index page ruby on rails?

I have a displaying table grid in my index page. I have a new button in the same page. When i click the new button, the form should appear in the same index page at the top of the table grid. How can i do that? i am new to ROR. So please help me in detail.
Here is my index page.
<div id="new_survey_link">
Create a
<%= link_to 'New', new_enr_rds_surv_rdsap_xref_path %>
</div>
<table class="gridView">
<tr class="gridViewHeader">
<th>Section</th>
<th>Questions</th>
<th>Answer</th>
<th>Element</th>
<th>Sub Element</th>
<th>Material</th>
<th>Action</th>
</tr>
<% if #enr_rds_surv_rdsap_xrefs.empty? %>
<td class="empty_data" colspan="7">No Energy/Survey Cross references are currently exist.</td>
<% else %>
<% #enr_rds_surv_rdsap_xrefs.each do |survey| %>
<tr class="<%= cycle('gridViewclickableRowDialog', 'gridViewAltclickableRowDialog') %>">
<td><%= survey.Section %></td>
<td><%= survey.enr_rds_question_2009.question_text %></td>
<td><%= survey.Answer_No %></td>
<td><%= survey.element.Element %></td>
<td><%= survey.sub_element.Sub_Element %></td>
<td><%= survey.renew_schedule.Material %></td>
<td>
<%= link_to 'Edit', '#', :remote => true, class: "create-user" %> |
<%= link_to 'Delete', survey, method: :delete,
confirm: "Are you sure?" %>
</td>
</tr>
<% end %>
<% end %>
</table>
You can add the form already in the page and hide it with style="display:none";
Then bind the click event of the "new" link to a javascript that toggles the visibility. like with jQuery you can have onclick="$('#new_survey_link a').toggle();"

How do i see if a "file_name" is contained in #files?

In my controller i have the following:
#documents = Document.find(:all, :order => "section asc, sub_section asc, position asc, name asc")
#files = Dir.glob("public/downloads/*").sort
In my view i have the following:
<% #documents.each do |d| -%>
<tr class="<%= cycle("","alt") %>">
<td><%= d.name %></td>
<td><%= d.file_name %></td>
<td><%= d.description %></td>
<td>
<%= link_to "Edit", edit_document_path(d) %><br>
<%= link_to "Del", document_path(d), :confirm => "Are you sure boogerhead?", :class => "destroy", :method => :delete %>
</td>
</tr>
<% end -%>
If file_name is not contained in #files then the link on another page that is dependent on that name (file_name) will not work. If there is not a match, I'll color code file_name to indicate there is a problem. How do i check to see if file_name is contained in #files?
Thanks
The Array#include? method checks whether a given item is contained in an array, so you can do:
if #files.include?(d.file_name)
# It is included
else
# It isn't
end
You can also check if a file exists somewhere in your #directory loop:
File.exist?(file_path)
or if I'm reading your code right, you're checking if a document has a certain file, right? If that is so, why not use paperclip gem so you can add an association that your document should have a file.
Or if that's not possible, you can still transfer that logic into your Document model like so:
def has_file
File.exist?("public/downloads/#{file_name}")
end
then in your loop,
<% #documents.each do |d| -%>
<% if d.has_file %>
<tr class="<%= cycle("","alt") %>">
<td><%= d.name %></td>
<td><%= d.file_name %></td>
<td><%= d.description %></td>
<td>
<%= link_to "Edit", edit_document_path(d) %><br>
<%= link_to "Del", document_path(d), :confirm => "Are you sure boogerhead?", :class => "destroy", :method => :delete %>
</td>
</tr>
<% end %>
<% end -%>

Ruby on Rails, Posting Variables

I'm very new to rails so hopefully this should be a quick fix. I'm writing an application that searches a database and reloads the page displaying the desired results. In rails how does one save a input into a text_field and post it so that it can be retrieved and used in the query for retrieving data.
My view:
<title></title>
</head>
<body>
Search Collection <br>
<%= text_field "person", "name" %>
<%= select_tag(:search_id, '<option value="0">Search by</option><option value="1">Make</option><option value="2">Condition</option>
<option value="3">Sport</option>') %>
<%= link_to 'New catalog', new_catalog_path %>
<br>
<br>
<%= link_to "Search", :search_text => , :action => :index %> <br>
<br>
<h1>Results</h1>
<%= will_paginate #catalogs %>
<table border="1">
<tr>
<th>Catalog id</th>
<th>Year</th>
<th>Make</th>
<th>Card number</th>
<th>Number</th>
<th>Condition</th>
<th>Sport</th>
<th>Tracking list</th>
</tr>
<% for catalog in #catalogs %>
<tr>
<td><%= catalog.Catalog_ID %></td>
<td><%= catalog.Year %></td>
<td><%= catalog.Make %></td>
<td><%= catalog.Card_Number %></td>
<td><%= catalog.Number %></td>
<td><%= catalog.Condition %></td>
<td><%= catalog.Sport %></td>
<td><%= catalog.Tracking_List %></td>
<td><%= link_to 'Show', catalog %></td>
<td>
<%= link_to 'Edit', edit_catalog_path(catalog) %></td>
<td>
<%= link_to 'Destroy', catalog, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br>
</body>
My Controller method
def index
#search_text = 'T206 White Border'
#catalogs = Catalog.paginate_by_sql ['select * from catalogs where Make =\''+ #search_text+'\'' , 80000], :page => params[:page]
end
Be gentle if its an easy fix, I'm still getting used to the whole MVC thing
Your question has a lot going on, so let's try to sort through it one piece at a time. First, I'll assume your database has a table called catalogs with a column called make, and that you're using the will_paginate plugin. It looks like you got started by copying and modifying some examples straight from the docs. First, your controller - you don't need the more complex paginate_by_sql, you can use the simpler paginate.
controller:
def index
#catalogs = Catalog.paginate(:all, :conditions => {:make => params[:search]}, :page => params[:page])
end
Now your view, just the stuff relevant to the search:
<% form_tag catalogs_path, :method => :get do %>
<%= text_field_tag 'search' %>
<%= submit_tag 'submit search' %>
<% end %>
And that's it. Good luck!

Proper arguments for initializing a model in a view?

My question template lists answers, and lets somone add a new answer for a question.
But I'm not sure where and how to initialize a new answer for this line:
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
from the context below:
<p>
<b>Body:</b>
<%=h #question.body %>
</p>
<h1>Listing answers</h1>
<table>
<tr>
<th>Body</th>
</tr>
<% #question.answers.each do |answer| %>
<tr>
<td><%=h answer.body %></td>
<td><%= link_to 'Show', answer %></td>
<td><%= link_to 'Edit', edit_answer_path(answer) %></td>
<td><%= link_to 'Destroy', answer, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
<%= link_to 'Edit', edit_question_path(#question) %> |
<%= link_to 'Back', questions_path %>
If I initialize the new Answer like this:
<%= link_to 'New answer', new_question_answer_path(#question, Answer.new) %>
the URL of the next page is garbled with an inspect call to the new Answer.
Try this:
<%= link_to 'New answer', new_question_answer_path(#question) %>

Resources