So I'm doing some basic validation and trying to add an arbitrary key to the flash hash (you can do that right?)
So in my controller I have this... "previousaction" posts to this page.
if params[:home_value] == "Select One"
flash.now[:home_value] = "Please select a home value"
render "previousaction"
else
#set controller vars.. render this action as normal
end
And in the view:
<% if home_value %>
<h6 id="notice" style="color:red;padding-bottom:5px;"><%= home_value %></h6>
<%= label_tag "Estimated Home Value", "Estimated Home Value", :style => "color:red;"%><br/>
<% else %>
<%= label_tag "Estimated Home Value", "Estimated Home Value" %><br/>
<% end %>
But I get this error when trying to load the controller action (the first time):
undefined local variable or method `home_value'
Tips appreciated :)
For Dave:
In a previous action/view I use flash[:notice] like this:
if params[:zip_code].length != 5
flash.now[:notice] = "Invalid zipcode. Please enter your 5-digit zipcode"
render "firstpage"
else
and then in the view
<% if notice %>
Is flash[:notice] a special flash key for rails?
home_value won't magically be associated with the flash, it's just being treated as a local variable; try accessing the flash hash with :home_value directly.
That said, what's the purpose of using an arbitrary key?
Related
I currently trying to do something like this in the applicaiton.html.erb
<h1> <% print "Customer id #{current_user}"%> </h1>
but I get nothing. the 'Customer id' is not being printed either. What am I doing wrong ?
Thanks for your time
If you're in a Rails view, using erb, then print or puts are not needed.
The <%= %> allows you to render (print) the expression that's contained inside, while <% %> allows you to create Ruby logic without rendering it, the same way <%# %> works for comments, so you could do:
<h1>
<%= "Customer id #{current_user.id} "%>
</h1>
Or:
<h1>Customer id <%= current_user.id %></h1>
No need to concatenate, and in both cases accessing to the current_user id attribute, otherwise it'd return the whole User object.
I'm creating a blog on rails. Everytime a post is created on the new page I want to be back at the index page and see a flash message saying "You post was saved". On the layout I have the following:
<body>
<%= flash.each do |key, value| %>
<p><%= value %></p>
<% end %>
<%= yield %>
</body>
The problem is that I have now on every page (index, new, etc.) curly brackets on the top and I don't know why. Also, instead of just having a message "Your post was saved.". It also appears the following: "{"notice"=>"Your post was saved."}". This is the code for the message hold on the controller (posts_controller.rb):
def create
#post = Post.new(post_params)
if #post.save
redirect_to posts_path
flash[:notice] = "Your post was saved."
else
render "new"
end
end
I'm beginning with Rails, thanks for the help.
See update below for explanation
remove the = in <%= flash.each.... %> Should just be <% flash.each.....%>
<% flash.each do |key, value| %>
<p><%= value %></p>
<% end %>
To keep it simple, when you want your ruby code to render something onto the page, use <%= %>, ex: to show the current time you would use <%= Time.now %>. For other things like settings variables in your view without text rendering on the page, use <% %>.
For example, say I want to assign a variable equal to the current time so I can use it below in the view, I would use <% time = Time.now %> then I can show that time on the page with <%= time %>.
<% time = Time.now %>
<p>The current time is <%= time %>.</p>
In devise.en.yml,i kept signed_out message empty as
sessions:
signed_out: ""
i am having a condition where if failed attempt > 3 then we have to make user signed out.
For this i wrote in the controller as:
if params[:failed_attempt].to_i > 3
current_user.update_attribute(:status, false)
redirect_to destroy_user_session_path(#user), :notice =>
"locked"
end
I am able to update attribute and able to logout but couldnot able to display notice or flash message. Please try to help me out.
I am getting all the messages except this. I also tried :
redirect_to destroy_user_session_path(#user)
flash[:notice] = "locked"
But no use.
Most likely you are setting the flash variable but not actually rendering out the content... try this:
In your application.html.erb before the <%= yield %> paste in code like this:
<% flash.each do |name, msg| %>
<div data-alert id="flashes" class="alert-box <%= name %>">
<%= content_tag :div, msg, id: "flash_#{name}" %>
×
</div>
<% end %>
You can style it afterwards whatever way you want
This done the trick. I just changed my signout as:
sign_out(current_user), :notice =>"locked"
instead of
redirect_to destroy_user_session_path(#user), :notice => "locked"
In my rails table i have a text_field which my_website, so <%= f.text_field :my_website %>
and so on the show.html.erb there is
Go to my website
and that works fine
but say the user doesnt input anything in the form for my_website, how would i make it work so that this partGo to my website hides if the user doesnt input my_website
Basically something like this
if user puts in my_website, show
Go to my website
else
show nothing
I assume this is after form submission. You can just use an if statement.
<% if #user.my_website %>
<%= link_to "Go to my website", #user.my_website %>
<% end %>
Alternatively, the solution below will not display "Go to my website" if the user inputs a bunch of whitespaces for the my_website field. blank? will return true if #user.my_website is nil or contains an empty string.
<% unless #user.my_website.blank? %>
<%= link_to "Go to my website", #user.my_website %>
<% end %>
I have an action like this:
def add_credit_card
if request.post?
unless params[:conditions]
flash[:error] = 'You need to accept!'
end
end
end
This action renders the following view:
<%= form_tag do %>
<fieldset>
<%= check_box_tag "conditions"%> I agree to the <%= link_to "Terms and Conditions", consumer_terms_and_conditions_url, :target => "_blank" %>
</fieldset>
<%= submit_tag "Submit" %>
<% end %>
When I do a GET to that action no errors are shown. When I do a submit with that box checked no errors are shown. When I do the first submit without that box checked the error is shown, but the problem comes when I do another submit and the checkbox is not checked, the errors are still there.
My questions are:
Why is that happening?
What would be a better approach to deal with this situation, where a form is not attached to a model and the errors have to be shown just when the user has submitted the form?
Since you're using the same action, you'll want to use flash.now so the flash hash does not persist to the next action.
flash.now[:error] = 'You need to accept!'
Also, it's not essential, but consider using:
<%= check_box_tag 'conditions', 'accepted' %>
and then checking the value of the params[:conditions] for the string "accepted" i.e.:
unless params[:conditions]=='accepted'
flash.now[:error] = 'You need to accept!'
end