Image_tag First Image - ruby-on-rails

Carrierwave use, I would like to show the first picture of a publication.
Try the following:
<% #autos.each do |auto| %>
<tr class="list">
<td><%= image_tag auto.fotos.first.to_s , size: "50x50" %></td>
<td><%= auto.ciudad %></td>
<td><%= auto.marca %></td>
<td><%= auto.modelo %></td>
<td><%= auto.version %></td>
<td><%= auto.año %></td>
<td><%= auto.hp %></td>
<td><%= auto.km %></td>
<td><%= link_to 'Show', auto %></td>
<td><%= link_to 'Edit', edit_auto_path(auto) %></td>
<td><%= link_to 'Destroy', auto, method: :delete, data: { confirm: 'Are?' }%></td>
</tr>
<% end %>
This makes it look like hexadecimal, not illustrated.
Any ideas?

You need to pass the image_tag an image URL. Instead of calling to_s on the model, you need to call url on the uploader attribute.
auto.fotos.first.foto.url

Related

Rails, concatenating 2 variables and display them in view

thanks in advance for your help!
I would like to concatenate 2 variables and display both in one cell in table in the view. More specifically I want to display column "game score" e.g. 5:7 by concatenating #game.team1_score and #game.team2_score.
Can you help out with this one line?
<% #games.each do |game| %><tbody>
<tr>
<td><%= game.id %> </td>
<td><%= game.team1 %></td>
<td><%= game.team2 %></td>
<td><%= game.team1_score ":" game.team2_score %></td>
<td><%= game.updated_at %></td>
<td><%=link_to("Detail", game_path(game.id), class: 'action show') %></td>
<td><%=link_to("Edit", edit_game_path(game.id), class: 'action edit') %></td>
<td><%=link_to("Delete", game_path(game.id), method: :delete, class: 'action delete') %> <br></td>
<% end %>
</tr>
try this!
<td><%= game.team1_score %>:<%= game.team2_score %></td>

Best way to deal with an error caused by a method returning no values?

The following code is implemented to read in values and fill in a table with the appropriate values, however when one of the methods doesn't return anything it produces an error and crashes the page.
<tr class="event-row" >
<td><%= event.description %></td>
<td><%= event.contact.name %></td>
<td><%= event.start.strftime('%H:%M') %></td>
<td><%= event.end.strftime('%H:%M') %></td>
<td><%= link_to "edit", edit_event_path(event) %></td>
<td><%= link_to "delete", event, method: :delete, data: {confirm: "Are you sure?"} %>
<td><%= link_to "show", event_path(event) %></td>
</tr>
How can you check to see if the methods are returning no values?
you can also check if that value is present with if
<tr class="event-row" >
<td><%= event.description %></td>
<td><%= event.contact.name if event.contact %></td>
<td><%= event.start.strftime('%H:%M') if event.start %></td>
<td><%= event.end.strftime('%H:%M') if event.end %></td>
<td><%= link_to "edit", edit_event_path(event) %></td>
<td><%= link_to "delete", event, method: :delete, data: {confirm: "Are you sure?"} %>
<td><%= link_to "show", event_path(event) %></td>
</tr>
You can use try
Try will returns nil rather than raising an exception
event.try(:description)
event.try(:contact).try(:name)
event.try(:start)
event.try(:end)
For more clarification, have a look at this
try public method
You can use try(:name) or delegate with allow_nil:
add this to your event model:
delegate :name, to: :contact, prefix: true, allow_nil: true
and this in your view:
<td><%= event.contact_name %></td>

How to displaying an specific attributes with a many to many association in rails

I have a two way many-to-many assoication between 3 models: work.rb, category.rb, categorywork.rb
Within the work#index using <%= work.categories %> renders some wonky looking html markup
<% #works.each do |work| %>
<tr>
<td><%= work.name %></td>
<td><%= work.subtitle %></td>
<td><%= work.categories %></td>
<td><%= link_to 'Show', work %></td>
<td><%= link_to 'Edit', edit_work_path(work) %></td>
<td><%= link_to 'Destroy', work, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I'm trying to target speific attributes of the association like "name".
Unfortunately when using <%= work.categories.name %> it gets weirder with:
How do i target just the name or just the description?
Try this out:
<%= work.categories.pluck(:name) %>

rails path helper doesnt work

Hello im learning Rails on my testing app and i have this code
<% #categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= link_to 'Show', backend_category %></td>
<td><%= link_to 'Edit', edit_backend_categories(category) %></td>
<td><%= link_to 'Destroy', backend_category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
and Rake routes shows me this
home_index GET /home/index(.:format) home#index
root / home#index
contact /contact(.:format) home#contact
backend_root /backend(.:format) backend/admin#index
backend_categories GET /backend/categories(.:format) backend/categories#index
POST /backend/categories(.:format) backend/categories#create
new_backend_category GET /backend/categories/new(.:format) backend/categories#new
edit_backend_category GET /backend/categories/:id/edit(.:format) backend/categories#edit
backend_category GET /backend/categories/:id(.:format) backend/categories#show
PUT /backend/categories/:id(.:format) backend/categories#update
DELETE /backend/categories/:id(.:format) backend/categories#destroy
but im getting error that backend_category doesnt exist
here is image
http://www.nahraj-obrazek.cz/?di=213711395092
whats wrong ? Thank you
You must add _path to your link_to helper url, like so
<% #categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= link_to 'Show', backend_category_path(category) %></td>
<td><%= link_to 'Edit', edit_backend_category_path(category) %></td>
<td><%= link_to 'Destroy', backend_category_path(category), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Following Luis answer, any show path needs a reference to an object. So I believe it should be
<td><%= link_to 'Show', backend_category_path category %></td>
Which can also be written as:
<td><%= link_to 'Show', category %></td>

Rails 3.1 associations?

I have a Rails 3.1.1 application with the following models:
Company
Member
The two models have the following associations:
Company - has_many :members
Member - belongs_to :company
When adding members I can enter the company ID number and the record is linked successfully, I can lookup members through the company etc.
When I am working on the member show view I would like to 'pull' in details of the company.
Currently I have the following in the show view:
<h1>Listing members</h1>
<table>
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Email</th>
<th>Qualifications</th>
<th>Membership</th>
<th>Company</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #members.each do |member| %>
<tr>
<td><%= member.name %></td>
<td><%= member.mobile %></td>
<td><%= member.email %></td>
<td><%= member.qualifications %></td>
<td><%= member.membership %></td>
<td><%= #member.company.company_id %></td>
<td><%= link_to 'Show', member %></td>
<td><%= link_to 'Edit', edit_member_path(member) %></td>
<td><%= link_to 'Destroy', member, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
How do I go about pulling in a field from the related company? For example the company model has two fields (latitude and longitude).
Your loop should look like this:
<% #members.each do |member| %>
<tr>
<td><%= member.name %></td>
<td><%= member.mobile %></td>
<td><%= member.email %></td>
<td><%= member.qualifications %></td>
<td><%= member.membership %></td>
<td><%= member.company_id %></td>
<td><%= member.company.latitude %></td>
<td><%= link_to member.company.name, member.company %></td>
<td><%= link_to 'Show', member %></td>
<td><%= link_to 'Edit', edit_member_path(member) %></td>
<td><%= link_to 'Destroy', member, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
company_id is defined on the Member model, you cannot access it through member.company.company_id. To access a field on the related company model, use member.company.my_field.
These will only work in the members loop, as they access the |member| variable which is passed to the block.
#member.company.latitude
et cetera. Please let me know if you want clarification or more information.

Resources