To learn Rails, I am writing a simple guestbook app that does not use a database.
Anyway, this is what my view looks like:
views/guest_book_pages/home.html.erb
<h1>Guest Book</h1>
<%= #userinput %>
<%= form_for(:guestbook) do |f| %>
<%= f.label :input %>
<%= f.text_field :input %>
<%= f.submit "Sign" %>
<% end %>
And the controller looks like this:
controllers/guest_book_pages_controller.rb
class StaticPagesController < ApplicationController
def home
#userinput = params[:guestbook]["input"]
end
end
Whenever I change the "input" to a symbol :input, the application breaks and gives me a warning that says: undefined method `[]' for nil:NilClass
What is the reason for this? Why can't I use a symbol?
update: Now it won't even work with the string. What is going on?
update#2: It works with both symbols and string. The only problem is that it will not load the first time. If I can get the page to load, then either will work. How can I get the page to load?
Action use to be handle something, and render view.
when you inter home, the home action has be called, and no param posted now.
for your code, home action should just be empty, it just to render the home_page.
your handle code should move to some action like sign_in, whitch handle the form post and you can get the params.
The first time you load the page the params var is not set. It is only when you submit your form back that there are params
Try
#userinput = params[:guestbook]["input"] || ''
which will initialize the #userinput to an empty string if the params is not found
edit:
This will check if the params has the key guestbook first, then will either set the instance var userinput to an empty string or the value of [guestbook][input] if it exsists.
If all else fails, the instance var is initialized to an empty string to prevent an error in your view.
if params.has_key?(:guestbook)
#userinput = params[:guestbook]["input"] || ''
else
#userinput = ''
end
Related
Im trying to display all the images from active storage on an index page. Note: The image will display in the show page
Heres what i have on the index page:
<% #imagelists.each do |il| %>
<%= image_tag(#il.image) &>
<% end %>
I can do the following which shows the active storage data
<% #imagelists.each do |il| %>
<%= #il.image &>
<% end %>
When you do this:
<% #imagelists.each do |il| %>
You are defining a variable il which is available within that each block.
However, in your code you are referencing #il - an instance variable which doesn't exist. Unlike a normal variable, when you reference an instance variable which isn't defined, you just get nil, not a NoMethodError.
So, just use il instead of #il and you'll be fine.
class ProfileController < ApplicationController
def show
#user = current_user
#first_name = #user.first_name
#last_name = #user.last_name
end
def settings
end
def pics
#photos = current_user.photos.all
end
end
in the view of _pics.html.erb, I have
<% #photos.each do |p| %>
<%= image_tag p.image(:medium) %>
<% end %>
If I change it to current_user.photos.each do |p|, it works, which is weird. I don't get an error from this code on my other computer.
In a comment you said, that you render the pics partial from your show view. Since the show view is rendered by the show action and the show action does not set the #photos variable, you can't use that variable. So to fix your problem, you'd need to set the variable in the show action.
You seem to think that rendering the pics partial will invoke the pics action, but that's not the case. An action will only be invoked if an URL is accessed that's mapped to that using the routing system. Rendering partials does not invoke any actions.
Also it should just be #photos = current_user.photos without the all.
Apologies for the long wined title, in my app I am trying to retrive e the last record from the database that matches the params I have passed to it.
#goals = Weight.last(:is_goal=>true,:user_id=>#user.id)
In my views I want to run a conditional that checks if there are any present and if it has it will display a div.
<% if #goals.any? %>
<% #goals.each do |goal| %>
<p><%= goal.amount %></p>
<% end %>
<% end %>
But for some reason this throws a no method error NoMethodError at /
undefined method 'any?'. If I change the .last to .all it works
#goals = Weight.all(:is_goal=>true,:user_id=>#user.id)
Is there any reason behind this or have I found a bug?
Well, .last method returns an object, .all method returns an array of objects.
And .any? is an array method. You can't call .any? on an object, it will tell you that there is no method unless you have created one.
I have the following .erb view in a Sinatra app:
<% sessions.each do |session| %>
<%= session.balance_beginning %>
<%= session.balance_ending %>
<% end %>
It works as expected, displaying the beginning and ending balances recorded for each session. I would like to calculate the net balances from within the .erb file, but I can't figure out how to do it. I have tried variations of this:
<% sessions.each do |session| %>
<%= session.balance_ending - session.balance_beginning %>
<% end %>
That doesn't work. I receive the following error in Sinatra:
undefined method `-' for nil:NilClass
How do I do what I'm trying to do?
Right #Zabba, in this case I think you would add a method to your Session model so you could call session.net_balance.
Then in your balance_ending and balance_beginning methods you would want to handle nil, either raise an error or return zero if that is valid.
If I don't handle view correctly, Production environment show 500.
<%= image_tag post.user.image_url %>
This could be
<%= image_tag post.user.image_url if post.user && post.user.image_url %>
but I am little careless and forgot this issue several times.
How can I prevent this? How can I use <%= image_tag nil %> in production environment without raising 500?
image_tag must have a source, Rails can do nothing with it, but raise an exception.
You can write a helper like this:
module ApplicationHelper
def safe_image_tag(source, options = {})
source ||= "default.jpg"
image_tag(source, options)
end
end
or simply check for nil directly in a view. Anyway you have to do something to prevent an error.