Rails 3.1, can't make link_to image_path? - ruby-on-rails

I want to make a fairly straightforward image link that uses an image from my assets. Getting weird errors. First I tried:
<%= link_to assets_path "town.png", 'index' %>
and got the error
Started GET "/" for 127.0.0.1 at Wed Nov 30 17:27:10 -0500 2011
Processing by PagesController#intro as HTML
Rendered pages/intro.html.erb within layouts/application (114.9ms)
Completed 500 Internal Server Error in 124ms
ActionView::Template::Error (undefined method `assets_path' for #<#<Class:0x10fdc4898>:0x10fdaad58>):
1: <body onload="init();">
2: <div id = "wrapper2">
3: <div class="intro_txt">
4: <%= link_to assets_path "town.png", 'index' %>
5: <br><br>
6: </div>
7: </div>
app/views/pages/intro.html.erb:4:in `_app_views_pages_intro_html_erb__1651075534_2280428740'
then I tried the old
<%= link_to image_tag "town.png", 'index' %>
and got this bizarre error
ActionView::Template::Error (undefined method `symbolize_keys!' for "index":String):
1: <body onload="init();">
2: <div id = "wrapper2">
3: <div class="intro_txt">
4: <%= link_to image_tag "townProjectText.png", 'index' %>
5: <br><br>
6: </div>
7: </div>
app/views/pages/intro.html.erb:4:in `_app_views_pages_intro_html_erb__1651075534_2279838600'
What to do?

<%= link_to image_tag('town.png'), 'index' %>
Put some parentheses

You need some ()
<%= link_to image_tag("town.png"), pages_path %>
Also, you need to use image_tag

<%= link_to image_tag("town.png"), image_path %>
if you want it to go to another page with the image by itself

Yeah some brackets would perhaps help. Try something like this:
<%= link_to(image_tag("town.png", :alt => 'Town Image'), index_url) %>

Related

Template::Error undefined method `[]' for nil:NilClass

Hello I have a Rails application in production, and recently i started getting totally random errors on the loading of the show method of my "complaints" controller .
My "complaints" table has a one-to-one relationship with "interactions" table, you can create an interaction then create a complaint.
I display my data in a table on complaints#index, then I click on a row to get to complaints#show
I get this error :
ActionView::Template::Error (undefined method `[]' for nil:NilClass):
7: <div class="uk-width-expand#m">
8: <h1 class="uk-heading-primary">
9: <%# <%= link_to 'Retour à l\'interaction', interaction_path(#variables[:complaint].interaction, search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
10: <%= I18n.t("kind.#{#variables[:complaint].kind}") %>
11: <% if #variables[:complaint].interaction.date.present? %>
12: - <%= #variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
13: <% end %>
app/views/complaints/show.html.erb:10:in `_app_views_complaints_show_html_erb__763547916_154685640'
But this error appears totaly randomly. That means if I refresh the page, it may launch this time. So because of this, i can't understand where this error comes from, most of the time there is no error, but I get complaints from the app users saying they are really bothered.
Here is my controller show method :
def show
complaint = Complaint.find_by(id: params[:id])
if complaint.blank?
redirect_to complaints_path
return
end
interaction = Interaction.find(complaint.interaction.id)
result = DetailCommande.execute_procedure "p_detail_commande", interaction.do_piece, interaction.do_type
contenu_commande = DetailCommande.execute_procedure "p_contenu_commande", interaction.do_piece, interaction.do_type
#variables = { complaint: complaint, detail_commande: result[0], contenu_commande: contenu_commande }
#params = { search: params[:search] }
end
Here is the part of my complaints#show view that causes trouble :
<div class="uk-flex-middle uk-margin-small" uk-grid>
<div class="uk-width-expand#m">
<h1 class="uk-heading-primary">
<%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
<%= I18n.t("kind.#{#variables[:complaint].kind}") %>
<% if #variables[:complaint].interaction.date.present? %>
- <%= #variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
<% end %>
</h1>
</div>
<div class="uk-width-auto#m">
<% if #variables[:complaint].control.present? %>
<button class="uk-button uk-button-default" disabled>Contrôle</button>
<% else %>
<a href="#modal-control-<%= params[:id] %>" class="uk-button uk-button-warning" uk-toggle>Contrôle</a>
<% end %>
<%= link_to 'Modifier', edit_complaint_path(id: #variables[:complaint].id, search: #params[:search]), class: 'uk-button uk-button-primary' %>
<%= link_to 'Supprimer', complaint_path(#variables[:complaint], search: #params[:search]), method: :delete, data: {confirm: 'Êtes-vous sûr ?'}, class: 'uk-button uk-button-danger' %>
</div>
</div>
And here is a screen of my page when it actually loads. Just simple data display
I don't know if i provided enough informations, but thank you in advance for your help
I think that your application is running on 2 servers , sharing the load by load balancer.
I think so because of the following 2 cases:
See line no. 9 of your log i.e.
9: <%# <%= link_to 'Retour à l\'interaction', interaction_path(#variables[:complaint].interaction, search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
and your html file does not contains this code, this are some changes in show.html i.e.
<%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
This error gone on page refresh, it means that 1 server contains correct code and 1 contains wrong code, so when the request goes on server 1, it displays the page and when it goes on server2, it gives error.
As per the logs, I think that complaint.kind is empty.

Why is resources in the routes.rb file not creating ALL of the routes?

I am making a rails application. After a user has registered (I have
already created user registration with devise), they can fill out this
form that will contain their profile information. The form should
submit a post request to the create action in the informations controller, but for some reason I can't configure the routes properly. When i run rake routes, for informations#create, which is what the form should be going to, it has a blank path. There is also informations#index, which is what I guess its going to now. How do I get the form to go to informations#create if the path is blank? I have done this several times, and i can't find what is wrong. Here is the model:
class Information < ActiveRecord::Base
belongs_to :user
end
Here is the controller:
class InformationsController < ApplicationController
def new
#information = Information.new
end
def create
#information = Information.create(params[:information])
redirect_to student_path
end
def index
end
end
And here is the view for the new action.
<div class="span6 offset3 text-center">
<h1>Edit your information</h1>
<%= simple_form_for #information do |f| %>
<%= f.input :skills %>
<%= f.input :looking_for, :label => 'What help do you need?' %>
<%= f.input :my_idea %>
<%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
<% end %>
</div>
Here is the line in the routes file:
resources :informations
I get the following error:
undefined method `information_index_path' for #<#:0x007f9c00c7b3e0>
Here is rake routes for the files
informations GET /informations(.:format) informations#index
POST /informations(.:format) informations#create
Here is my full stacktrace when trying to load the page that shows the form:
Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
Rendered informations/new.html.erb within layouts/application (64.2ms)
Completed 500 Internal Server Error in 70ms
ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8b23e8>):
2: <div class="span6 offset3 text-center">
3: <h1>Edit your information</h1>
4:
5: <% simple_form_for #information do |f| %>
6: <%= f.input :skills %>
7:
8:
app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334909089600'
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.7ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.1ms)
Started GET "/informations/new" for 127.0.0.1 at 2013-10-21 17:25:09 -0400
Processing by InformationsController#new as HTML
Rendered informations/new.html.erb within layouts/application (8.3ms)
Completed 500 Internal Server Error in 13ms
ActionView::Template::Error (undefined method `information_index_path' for #<#<Class:0x007ff03e8b34a0>:0x007ff03e8708a8>):
2: <div class="span6 offset3 text-center">
3: <h1>Edit your information</h1>
4:
5: <% simple_form_for #information do |f| %>
6: <%= f.input :skills %>
7:
8:
app/views/informations/new.html.erb:5:in `_app_views_informations_new_html_erb__3172218935119285240_70334908978600'
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.0ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
Rendered /usr/local/rvm/gems/ruby-2.0.0-p247#firehose/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (14.0ms)
Your problem is related to the fact that the word information is both the plural and the singular form. Just like water, or paper, or evidence. Rails considers those words uncountable.
There are two things you can do: either name your model something else or edit the file config/initializers/inflections.rb like so:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.uncountable %w( information )
end
Your form should be for one single information object. Not for a collection of information objects. Note that i have changed simple_form_for #informations to simple_form_for #information
<div class="span6 offset3 text-center">
<h1>Edit your information</h1>
<% simple_form_for #information do |f| %>
<%= f.input :skills %>
<%= f.input :looking_for, :label => 'What help do you need?' %>
<%= f.input :my_idea %>
<%= submit_tag "Save", :class => "btn btn-primary btn-large" %>
<% end %>
</div>
Also your failure is not because rails didn't generate information_index_path. Its because the named route for information#index is informations_path. the path is generated wrongly because you are using collection of objects.
Not sure if this will help, but try adding a = in front of simple_form_for. so that it will have a <%= instead of <%

ActionView::Template::Error (undefined method `+' for nil:NilClass):

One of my pages keeps hitting this error:
ActionView::Template::Error (undefined method `+' for nil:NilClass):
15: <div class="look_list">
16: <% collection.each do |look| %>
17: <div class="look_book" id="<%= look.content_id %>">
18: <% thumbnail_image = (look.processing? ? "/assets/processing_placeholder.gif" : (look.image.url(:thumb) + "?#{look.updated_at.to_i}")) %>
19: <%= image_tag thumbnail_image || "/assets/processing_placeholder.gif",:class=> "look_image", :size => "118x118" %>
20: <script type="text/javascript">
21:
app/views/looks/index.html.erb:18:in `block in _app_views_looks_index_html_erb__3409922204803071014_68666020'
app/views/looks/index.html.erb:16:in `_app_views_looks_index_html_erb__3409922204803071014_68666020'
Some background, my site hit into 502 bad gateway 2 days ago. We managed to restart the site. However, it caused some connection issue with mongodb. This was resolved after we restart the DB. However, this one page keep hitting the error above. This has never happened before. Any one can help?
what is the value of look.image.url(:thumb) before line 18 executes? That is where the error is thrown, and it is the only place I am seeing a +
I'd check your data, to see if the crash caused data loss somewhere, in particular, on the data needed for that method
Try out something like this .....
<div class="look_list">
<% collection.each do |look| %>
<div class="look_book" id="<%= look.content_id %>">
<% thumbnail_image = look.image.url(:thumb) unless look.processing? %>
<% thumbnail_image.nil? ? "/assets/processing_placeholder.gif" : (look.image.url(:thumb) + "?#{look.updated_at.to_i}") %>
<%= image_tag thumbnail_image ,:class=> "look_image", :size => "118x118" %>
</div>
<% end %>
</div>

Embed youtube video in rails

I'm trying to embed video in pages. I tried two different ways but no luck
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Name:</b>
<%= #video.name %>
</p>
<p>
<b>Url:</b>
<%= #video.url %>
</p>
<p>
<% #video_tag( #video.url , :size => "560x315", :controls => true, :autobuffer => true ) %>
<%= youtube_video #video.url%>
</p>
<%= link_to 'Edit', edit_video_path(#video) %> |
<%= link_to 'Back', videos_path %>
The video does not show neither does the links below it.
Any tips would be appreciated. Thank you
this is the debug result
ActionView::MissingTemplate in Videos#show
Showing
/Users/atbyrd/Documents/sites/city/app/views/videos/show.html.erb
where line #15 raised:
Missing partial shared/video with {:handlers=>[:erb, :builder,
:coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: *
"/Users/atbyrd/Documents/sites/city/app/views" *
"/Users/atbyrd/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.1/app/views"
Extracted source (around line #15):
12: 13: 14: <% #video_tag( #video.url , :size => "560x315",
:controls => true, :autobuffer => true ) %> 15: <%= youtube_video
#video.url%> 16: 17: 18: <%= link_to 'Edit',
edit_video_path(#video) %> |
Rails.root: /Users/atbyrd/Documents/sites/city Application Trace |
Framework Trace | Full Trace
app/helpers/application_helper.rb:4:in youtube_video'
app/views/videos/show.html.erb:15:in
_app_views_videos_show_html_erb__1532956397246631491_70281299458880'
app/controllers/videos_controller.rb:18:in `show'
you have forgotten to close a bracket
<%= video_tag( #video.url %>
should be
<%= video_tag( #video.url ) %>
UPDATE:
Try using video_path instead of video_tag.
2nd UPDATE:
here's how I do this myself on my own site:
1 - create a partial called _video.html.erb (I actually use haml, but erb will do if you prefer it) and put it in a folder like views/shared or something ad put the following code in it:
<iframe width="490" height="275" src="<%= url %>" frameborder="0" allowfullscreen></iframe>
2 add the following method to application_helper.rb
module ApplicationHelper
# this method will embed the code from the partial
def youtube_video(url)
render :partial => 'shared/video', :locals => { :url => url }
end
end
3 - now just call this in your views with:
<%= youtube_video #video.url %>
This works ok for me
I got it, you forgot to use erb syntax. Try:
<iframe width="560" height="315" src= "<%= #{#video.url} %>" frameborder="0" allowfullscreen></iframe>
#miaout17 is right, you can't use string interpolation in HTML, so you have to wrap it in Erb (e.g. <%= %>).
Also you're missing an ending ')' in your links below which is probably why they aren't working.

RoR: partials with resources

I have a problem, trying to render partials in ruby on rails with the short notation for resoures. Somehow, RoR displays simply nothing that has to do with the partial, but i get no errors as well. I mean, the resulting HTML looks like the call for the partial simply wouldn't be there. I'm also confused, because i can see in the log, that rails rendered the partial 2 times (that would be ok, i have two test entries in the dev db), but i get no output in the browser. What am i doing wrong? Thanks in advance! This is my code:
app/views/tutorials/_tutorial.html.erb:
<div class="tutorial">
<h3><%= link_to tutorial.title, tutorial %></h3>
<span class="person"><%=h tutorial.tutor %></span>
<span class="time"><%=h german_time(tutorial.starts_at) %></span>
<span class="location"><%=h tutorial.location %></span>
<div class="description"><%=h tutorial.description %></div>
<% if admin? %>
<%= link_to 'Edit', edit_tutorial_path(tutorial) %> |
<%= link_to 'Delete', tutorial, :confirm => 'Are you sure?', :method => :delete %>
<% end %>
</div> <!-- end .tutorium -->
app/views/tutorials/index.html.erb:
<h2>Tutorials</h2>
<% render :partial => #tutorials %>
<% if admin? %>
<%= link_to 'New Tutorial', new_tutorial_path %>
<% end %>
console log:
Processing TutorialsController#index (for 127.0.0.1 at 2009-12-07 23:39:00) [GET]
Tutorial Load (0.6ms) SELECT * FROM "tutorials"
Rendering template within layouts/application
Rendering tutorials/index
Rendered tutorials/_tutorial (7.1ms)
Rendered tutorials/_tutorial (3.7ms)
Completed in 22ms (View: 17, DB: 1) | 200 OK [http://localhost/tutorials]
Try it with an equals:
<%= render :partial => #tutorials %>

Resources