I have the following table, and I want the name of my "restaurant" to be clickable and link to the page of that restaurant.
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= rest.name %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
How am I going to add something like rest_path to:
Link text
You could try this :
<%= link_to rest.name, rest %>
If you have defined a resource restaurant in your config/routes.rb, it will target the url given by the helper restaurant_path(rest) (ie /restaurants/id_of_restaurant).
As the restaurant is a child of a city you might want to have an url like /cities/id_of_city/restaurants/id_of_restaurant so you could try the following :
<%= link_to rest.name, city_restaurant_path(#city, rest) %>
Be sure to have this in your config/routes.rb in order to generate the corresponding helpers.
resources :cities do
resources :restaurants
end
Then you will be able to see all the available helpers for your routes using the command 'rake routes' in the terminal.
Okay so I did something like this
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Adress</th>
<th>City</th>
<th>No. of Recipies</th>
</tr>
<% #city.restaurants.each do |rest| %>
<tr>
<td><%= link_to rest.name, rest %></td>
<td><%= rest.adress %></td>
<td><%= rest.city.name %></td>
<td>5</td>
</tr>
<% end %>
</table>
but now the link is very ugly when I hover on it? How do I get rid of that?
Related
I'm creating a rails app for my expenses. I have several models like user, spending, currency. I put relations between them, everything works. I can add a new expense that is shown on a index (all the expenses from all users) and I decided to loop through the expenses on the user show page, where the current_user see his own expenses.
I also grouped the expenses by date (month and year) and I have a "Today" group, which shows all the expenses entered on the day. It looks like this in the controller :
def show
#user_spendings = #current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page =>
10)
#Retrives all messages and divides into two groups todays messages and other messages
#grouped_spendings = #user_spendings.group_by{ |t| t.date.to_date == DateTime.now.to_date }
if #user_spendings.present?
#Create month wise groups of messages
#month_wise_sorted_spendings = #user_spendings.group_by{ |t| (Date::MONTHNAMES[t.date.month] + " " + t.date.year.to_s) }
end
end
This is my view :
<% if #grouped_spendings.present? && #grouped_spendings[true].present? %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<h3>Today</h3>
<tbody>
<% #grouped_spendings[true].each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% if #month_wise_sorted_spendings.present? %>
<% #month_wise_sorted_spendings.each do |hash_elements|%>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<% date_in_link = "#{hash_elements.first}" %>
<h3><%= link_to(date_in_link, detail_result_path) %></h3>
<tbody>
<% hash_elements.last.each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% end %>
I would like to create a link on the month and year and open a page where all the informations are looped again but only for this specific month of the year.
I already achieved the linking with this :
<% date_in_link = "#{hash_elements.first}" %>
<h3><%= link_to(date_in_link, detail_result_path) %></h3>
To avoid mixing the controllers etc... I decided to create new controller called detail and a result.html.erb page where I will loop the expenses about the specific month and year.
Which way should I take with this ? Is it a good option to do a new controller ? And how can I get the infos back according to the date on my new page ?
I thought about passing some params via the link_to but I'm not sure it's the best solution. Maybe there is something easier but I don't know or see it.
If anyone can help would be great !
Thanks alot
I found the answer but it's on a other post I did as I had an other problem before this one. So the link is here
Feel free to ask question if any
I am trying to submit a grid form like the below one,
show.html.erb
<%= form_for :datadef, url: update_all_vmodule_path do |fdf|%>
<table id="csvfiles" class="display" width="100%">
<thead>
<tr>
<th width="10%">Column No</th>
<th width="20%">Column name</th>
<th width="20%">Data type</th>
<th width="15%">Column format</th>
<th width="10%">Length</th>
<th width="25%">Comments</th>
</tr>
</thead>
<tbody>
<% #datadefrecords.each do |ddre| %>
<%= fields_for "datadef_records[]", ddre do |dr_fld| %>
<tr>
<td><%= ddre.column_no %></td>
<td><%= ddre.column_name %></td>
<td><%= dr_fld.collection_select :column_datatype, Datadef.select(:column_datatype).uniq, :column_datatype, :column_datatype, {:prompt => true} %> </td>
<td><%= dr_fld.text_field :column_format %></td>
<td><%= dr_fld.text_field :column_length %></td>
<td><%= dr_fld.text_field :comments %></td>
</tr>
<% end %>
<% end %>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="2"><%= fdf.submit 'Save' %> <%= link_to("<button>Cancel</button>".html_safe, cancelmodal_vmodule_path ) %></td>
</tr>
</tbody>
</table>
<% end %>
<% end %>
This the method i am calling it to submit
def update_all
session[:datadef] = nil
#updatedatadef = Datadef.all.where(:id => params[:datadef_records]).update_all(update_all_params)
redirect_to migproject_path(params[:vmodule][:migproject_id])
end
This is the strong parameters
def update_all_params
params.require(:datadef_records).permit( :column_datatype, :column_format, :column_length, :comments)
end
This is how i get the parameters from form
{"utf8"=>"✓",
"authenticity_token"=>"QrX8JYYYQOlfwKgAABJd0+A7VpugS2Y6n8doDsuKqeM=",
"datadef_records"=>{"12"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"},
"13"=>{"column_datatype"=>"varchar",
"column_format"=>"2",
"column_length"=>"2",
"comments"=>"2"}},
"commit"=>"Save",
"id"=>"2"}
But i get this "Empty list of attributes to change" error which is not allowing me to write it in table. And i am not able to identify what could be the error.
Thanks in advance.
I think the problem is to do with your use of update_all, although I'm not 100% sure as to the syntax of the problem.
From what I understand, update_all needs a hash of actual params to populate your data; even then, it will only update what you pass to it (kind of like update_attributes).
Here's a good reference:
data = params[:datadef_records]
#updatedatadef = Datadef.update(data.keys, data.values)
This negates the use of your strong_params, which I'd recommend using. I'd have to spend some time thinking about getting it to work.
I generated a scaffold for a To-Do List app, and I left out some columns to add later.
I ran the command to create a migration to add a new column named client and I changed my files so that it shows on the projects index and form, but when i enter something into the client field and submit, it doesn't save the information and remains blank..
Update 1:
Here is whats in my routes:
'
Rails.application.routes.draw do
root :to => 'projects#index'
resources :projects
end
'
Here is my index view:
'
<h1 id="title">Project List</h1>
<table>
<thead>
<tr id="headers">
<th>Title</th>
<th>Client</th>
<th>Description</th>
<th>Hours</th>
<th>Done</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody class="col-md-2" id="listItems">
<% #projects.each do |project| %>
<tr id="table">
<td><%= project.title %></td>
<td><%= project.client %></td>
<td><%= project.description %></td>
<td><%= project.hours %></td>
<td><%= project.done %></td>
<td><%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %></td>
<td><%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %></td>
<td><%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path, id:"new" %>
<footer id="footer">Copyright 2014 Kira Banks</footer>
'
To keep your application secured, Rails has a feature called Strong Parameters and the docs says:
It provides an interface for protecting attributes from end-user
assignment. This makes Action Controller parameters forbidden to be
used in Active Model mass assignment until they have been whitelisted.
So, basically you need to whitelist the new client attribute in the Projects controller by adding it to the list:
class ProjectsController < ApplicationController
# ...
# at the end of the file
private
def project_params
params.require(:project).permit(:title, :description, :hours, :done, :client)
end
end
I am creating a rails application that replicates an online dictionary. I have already implemented the alphabetical_paginate gem but I want to add a search bar that can filter through the words as you type for the word you're looking for. Is there a gem that could do this or is there a straight forward way of filtering with a search bar?
My controller looks like this:
def index
#words, #alphaParams = Word.all.alpha_paginate(params[:letter], {:default_field => "all"}){|word| word.word}
end
View:
<%= alphabetical_paginate #alphaParams %>
<table>
<thead>
<tr>
<th>Word</th>
<th>Wordtype</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<div id="pagination_table">
<tbody>
<% #words.each do |word| %>
<tr>
<td><%= word.word %></td>
<td><%= word.wordtype %></td>
<td><%= word.description %></td>
<td><%= link_to 'Show', word %></td>
</tr>
<% end %>
</tbody>
</div>
</table>
Datatables is what you are looking for.
Here are the links to get started:
http://datatables.net/
https://github.com/rweng/jquery-datatables-rails
I am new to ruby and rails so thought I'd ask a question on conventions.
I have a view which produces a list of items in a table and I was asked to make a change and in doing so have added a case statement to the view which I don't think is the correct way of doing things so thought that I would double check.
The change I made was just to add a class to the tr depending on the value of the last table column.
list.rhtml
<table width="100%">
<tr>
<th style="width: 80px;">ID #</th>
<th>Organisation</th>
<th>Product</th>
<th>Carrier</th>
<th>Carrier Ref</th>
<th>Post Code</th>
<th>Status</th>
</tr>
<%= render :partial => 'circuit/list_item', :collection => #circuits %>
</table>
list_item.rhtml
<%
# code I have added
#tr_class = ''
case list_item.status
when 'Handover'
#tr_class = ''
when 'Unprocessed'
#tr_class = 'high_priority'
when 'Ceased'
#tr_class = 'low_priority'
else
#tr_class = ''
end
# end of newly added code
%>
<!-- the class part is new aswell -->
<tr class="<%= #tr_class %>">
<td><a href='/circuit/update/<%= list_item.id %>'><%= list_item.id_padded %></a></td>
<td><%= list_item.organisation.name if list_item.has_organisation? %></td>
<td><%= list_item.product_name %></td>
<td><%= list_item.carrier.name %></td>
<td><%= list_item.carrier_reference %></td>
<td><%= list_item.b_end_postcode %></td>
<td><%= list_item.status %></td>
</tr>
Is there a Rails pattern or convention that can get the case statement out of this view?
If understand properly your question, I think you should put the case statement inside a helper function:
app/helpers/list_helper.rb
module ListHelper
def tr_class_for_status(status)
case status
when 'Unprocessed'
'high_priority'
when 'Ceased'
'low_priority'
else
''
end
end
end
_list_item.rhtml
<tr class="<%= tr_class_for_status(list_item.status) %>">