Okay, I've been having this problem for quite a while now and I can't figure it out. I have a bare-bones app with one controller called 'home' and one action called 'index'. I am using the default layout 'application.html.erb', and in that layout I am using the following:
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
my application.css file contains the following code below the manifest portion:
body {
background-color: blue;
}
and my index file only contains the following snippet:
<p>This is my content!</p>
When I try to access my page, I get the following error:
JSON::ParserError in Home#index
Does anyone know what is happening?
Thanks in advance!
EDIT
class HomeController < ApplicationController
def index
end
end
Related
I am trying to access a weather API that shows the icon of current weather.
My current code is:
response = HTTParty.get('http://api.openweathermap.org/data/2.5/weather?id=5911606&appid=734d8f2204043326b51df724c5c917f4', format: :json)
body = JSON.parse(response.body)["weather"][0]
#icon=body["icon"]
In html i am using the icon like:
<%= link_to image_tag('http://openweathermap.org/img/w/' + #icon +".png", :size => '130x130', :style => 'margin-top:45px;'), root_path%>
Its working fine when i first starts the server, but as I clicked any other link its gives me the following error:
nil is not a valid asset source
Does anybody know how to fix this?
it seems you are getting an empty #icon object. you can check for for empty icon object with #icon.blank? like following
<%= link_to image_tag('http://openweathermap.org/img/w/' + #icon +".png", :size => '130x130', :style => 'margin-top:45px;'), root_path unless #icon.blank? %>
if you can also think if having a helper method to display a default icon if the #icon is retuning a blank value
That means when you open any other links, I presume it calls a different actions where your code for #icon is not getting called and it is nil.
if you want #icon to be available for all the actions. You have to probably do it in a before action where you call the method populating #icon
I want to make an Navigation with specific Tags.
These Tags are for example: HTML, CSS and Javascript.
So when i click on one of them it will show all posts with these tag.
How can i achieve that?
My code for the Navigation right now looks like this (it's in the Application.html.erb)
<%= link_to "Computer", tag_list.Computer %>
I get this Error:
undefined local variable or method `tag_list' for #<#:0x007feec764ff88>
tag_list is a local variable or method, so unless you've created it in a helper that's your first issue. The second is that called .Computer on it doesn't work because tag_list is a method that created by the gem to list all an objects tags, and calling the . (also knowing as chaining) is attempting to call a method named Computer, which doesn't exist, that should just be a string and strings have to be quoted.
So, in your layout view, you can do
= link_to "Computer", tagged_posts_url(tag: "Computer")
Then in your posts_controller.rb add an action called tagged
def tagged
if params[:tag].present?
#posts = Post.tagged_with(params[:tag])
else
#posts = Post.all
end
end
To maintain a DRY set of views, you can even tell it to render the index view since you most likely already have a list of posts, now it will look exactly the same but only contain posts with that tag. e.g.
def tagged
if params[:tag].present?
#posts = Post.tagged_with(params[:tag])
else
#posts = Post.all
end
render "index"
end
Then in your config/routes.rb add a route for your new controller action under your existing post route
resources :posts do
collection do
get "/posts/tagged", as: :tagged
end
I got it myself.
Here is the Code:
<%= link_to 'Computer', { :controller => 'posts', :action => 'index', :tag => 'Computer'} %>
The controller looks like this:
def index
if params[:tag]
#posts = Post.tagged_with(params[:tag]).order('created_at DESC')
else
#posts = Post.all.order('created_at DESC')
end
end
I'm learning to use Carrierwave the first step is obviously to upload a picture and see if it's effectively inserted to the database. One detail that could be important is that this code is written in a Rails Engine and namespaced (Wanker)
I generated an uploader following the instruction of the gem, everything went good (Wanker::PicturesUploader)
I made a model CompanyDetailImage with a picture string field (MySQL) and added this line
mount_uploader :picture, Wanker::PicturesUploader
Then I made a view and a form
<%= f.fields_for [:wanker, #company, #company_detail, #company_detail_images] %>
<%= i.label :picture %>
<%= i.file_field :picture %>
<% end %>
The params[:company][:company_detail_image]["picture"] in the controller will have this inside of it
[#<ActionDispatch::Http::UploadedFile:0x007fe613b81f40
#content_type="image/png",
#headers=
"Content-Disposition: form-data; name=\"company[company_detail_image][picture][]\"; filename=\"Screen Shot 2015-02-04 at 8.18.58 PM.png\"\r\nContent-Type: image/png\r\n",
#original_filename="Screen Shot 2015-02-04 at 8.18.58 PM.png",
#tempfile=# <File:/var/folders/2w/lw3glw5d58g25qvv4cx6yk0m0000gn/T/RackMultipart20150213-22947-np2et6>>]
Which for me seemed good. But when I try this
#company_detail_image = Wanker::CompanyDetailImage.new
#company_detail_image.picture = params[:company][:company_detail_image]["picture"]
#company_detail_image.save!
It returns this
ActiveRecord::RecordInvalid: Validation failed: Picture You are not allowed to upload nil files, allowed types: jpg, jpeg, gif, png
Does someone has an idea why it doesn't catch the picture ? Thank you guys ;)
Try this:
#company_detail_image.picture = params[:company][:company_detail_image]["picture"].first
This is because your ["picture"] param is returning an array rather than the object itself (which is the first item in that array).
View:
!!!
%html
%head
%title= full_title(yield(:title))
=stylesheet_link_tag "application", media: "all"
=javascript_include_tag "application"
=csrf_meta_tags
=render 'layouts/shim'
%body
=render 'layouts/header'
.container
=flash.each do |key, value|
%div{class: "alert alert-#{key}"} #{value}
Controller
def create
#user = User.new(params[:user])
if #user.save
flash[:success] = "This is Correct"
redirect_to #user
else
flash[:wrong] = "no"
render 'new'
end
end
Regardless of the flash (:success or :wrong or otherwise) it always compiles the entire hash as html (below)
Output:
<!DOCTYPE html>
…
<div class='container'>
<div class='alert alert-wrong'>no</div>
{:wrong=>"no"}
</div>
</body>
</html>
I have no idea why {:wrong=>"no"} is being displayed. I've been staring at this terminal for hours. What's interesting is that the hash is being outputted with the container id, but not in the alert class. It feels like an indentation problem, but I went through several permutations with no success.
You need to use a - rather than a = when you call the each block:
-flash.each do |key, value|
%div{class: "alert alert-#{key}"} #{value}
From the docs:
It’s also possible to embed Ruby code into Haml documents. An equals sign, =, will output the result of the code. A hyphen, -, will run the code but not output the result.
So you're seeing the hash because = will output the result of the each block (the hash itself, i.e. {:wrong=>"no"}).
Environment: Rails 2.3.11 w/ MySQL 5.0
Here is my slideshow model:
class Slideshow < ActiveRecord::Base
validates_presence_of :title, :description
end
Using the console, if I run:
Slideshow.new(:title => "", :description => "").save!
it returns:
Validation failed: Title can't be blank, Description can't be blank
which is correct.
However, when I submit a blank HTML form to the create action:
def create
#slideshow = Slideshow.new(params[:slideshow])
if #slideshow.save
redirect_to(...)
else
render(:action => 'new')
end
end
only the :title field fails validation. I've verified that what is being passed in the params is:
Parameters: {"commit"=>"Submit", "slideshow"=>{"title"=>"", "description"=>""}, "action"=>"create", "controller"=>"manage/slideshows"}
Why is the description field NOT failing validation here?
Thanks.
Try this :
validates_length_of :description
for more details ... http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_length_of
when You submit a blank HTML form to the create action, it should go in else of create action and your form should have this line:
<%= f.error_messages %>
to show you the errors.
This turned out to be a syntax issue. It was occurring on a testing server where there were two models with very similar names (one an updated version of the other). During testing I used the wrong one. My apologies for any unnecessary head-scratching :)