Rails div not rendering? - html

I have a simple app I'm following from a textbook. It's a store app and on the oft side of the page a shopping cart is shown. However, when the cart is empty it is supposed to be hidden. So I wrote a helper function to hide the cart whenever it is empty. This would have been east enough just to do in the html but it's how the book shows it. It works mostly. If I empty the cart it stops being displayed, but when I first open the page the cart will be invisible even if its not empty! Adding an item to it will then reveal it.
Here's the code:
application.html.erb
<div id="cart">
<% if #cart %>
<% hidden_div_if( #cart.line_items.empty?, id:"cart" ) do %>
<%= render #cart %>
<% end %>
<%end%>
</div>
application_helper.rb
module ApplicationHelper
def hidden_div_if(condition, attributes = {}, &block)
debugger
if(condition)
attributes["style"] = "display: none"
end
content_tag("div", attributes, &block)
end
end
Where I put the debugger statement, I checked the condition and it says it's false. So, I thin it's an issue with the html file. The controller sets the cart before hand with this code at the top.
include CurrentCart
before_action :set_cart
So, I'm not sure why it would show after a button is pressed but not on the initial page load. Any ideas?
thanks

You need to start with <%= in erb if you want to output the return value of a helper method. Change the line in which you call your helper method to:
<%= hidden_div_if(#cart.line_items.empty?, id: "cart") do %>

Related

How to render html.erb partial dynamically depending on the origin of the render call?

I've got a _topbar.html.erb partial with element for that I render from the index of homepage.
I want to call the same partial in other pages, but depending on the endpoint that's rendering it, I want to display a different content.
Is there any way to apply a dynamic content based on the origin of the render on Rails?
One way to do this is by using current_page? So you could do something like:
<!-- _topbar.html.erb -->
<% if current_page?(posts_path) %>
Here are my posts
<% else %>
Check out my <%= link_to "posts page", posts_path %>
<% end %>
(docs are here)

Rails checkbox create a submit action

I have a rails form with checkboxes. What I am trying to do is when the user clicks on a check box the app deletes that entry from the database. What I cannot figure out is how to have the app react when the user checks on the check box (similar action to having a submit button clicked). What should I include in my html so when the user click on a check box the app does something?
Here is my code
<% #items.each do |item|%>
<%if item.email == #user.email%>
<%= form_for #item do |f| %>
<%= f.check_box :to_do %>
<%= f.label item.to_do %><br />
<% end %>
<%end%>
<%end%>
I am iterating through items and if there is a match between the item.email and user email print out the item with a check box next to it. I can include more code if needed. Is there an Ajax call I can make?
As RedBassett said, I'm not 100% sure I agree with the strategy of having a checkbox instantly delete an entry. However, if that form you have is already set up to delete things, you can do it like this:
You don't even need an AJAX call. Just regular Jquery. Add a class to the form, as well as to the checkbox
<%= form_for #item do |f|, :class => "form-class" %>
<%= f.check_box :to_do, :class => "checkbox-class" %>
and in your JS file, use Jquery to tell the form to submit when the user clicks the checkbox.
$(".checkbox-class").on("click", function (){
$(".form-class").submit();
});
Note: this will actually submit the form, it will be as if you checked the box and then pressed a 'submit' button, so you will need to handle the form submission in the controller, if you want to stay on the same page, you will need to redirect back to the same page as this action will cause a reload/redirect
Yes. You can write an AJAX call.
<script>
$('#checkbox_id').change(function(){
$.get('controller/action?item_id='+$(this).val(), function(data, status){
/* Call back */
})
})
</script>
Use borbesaur answer, but remember you can just add the remote: true option to your form so Rails can submit the form using ajax and you avoid reloading all the page again. :)

Ruby on Rails pass button value to next page

I am trying to design a webpage that contains both a radio button field and a list of links. When one of the links is clicked, I would like to pass the associated value of the button to a controller. So far I have the following code:
In my views page:
<div id="radio_buttons">
<%= radio_button_tag 'radioGroup', "1" %> 1
<%= radio_button_tag 'radioGroup', "2" %> 2
<%= radio_button_tag 'radioGroup', "4" %> 4
</div>
...
<div class = "text">
<%= link_to "mylink", search_path(:radioGroup => #radioGroup)%>
</div>
In my Controller:
#radioGroup = params[:radioGroup]
When I add the line 'puts #radioGroup' to my controller, it prints nothing. I have been able to pass different values to a new web page upon a link click (i.e those contained in a text field) but for some reason I can't figure out how to do the same thing with buttons. Does anyone know how to accomplish this?
Thanks!
If you want to submit multiple values to another page you should use an HTML form for it. Check this page for more related info: http://guides.rubyonrails.org/form_helpers.html.

Rails page has wrong html

I have a link to a page in the same folder as this page on my rails site:
<%= link_to 'Special Access', 'followers/special_access' %>
However, when I go to this page it shows a different page on that url.
<p id="notice"><%= notice %></p>
<div id="sent">
<p>Your request has been sent</p>
<%= link_to 'Home', followers_path %>
</div>
I tried deleting the page that the html is from, but first of all I need that page and it also gives me an error.
I edited the controller to contain:
def special_access
format.html { redirect_to followers/special_access }
format.json { render :json => #post }
end
instead of
def show
but that still didn't solve the problem.
How do I get the right html to show up on the right page?
If you do not define a route for special_access, rails will just assume the the special_acces part in the path is the :id of the route for the show page (as the urls look like followers/:id).
So first off, in your routes.rb, find the resources :followers and replace with the following:
resources :followers do
collection do
get :special_access
end
end
And now you should best always use the rails path helpers, so your link would become
<% link_to 'Special Access', special_access_followers_path %>
Here I was assuming the special access is on the collection of the followers, if it should be on a specific follower (which seems more logical to me, but I have no idea of course), you should write
resources :followers do
member do
get :special_access
end
end
And your link would become
<% link_to 'Special Access', special_access_followers_path(#follower) %>
I am not quite sure what you want to do in your controller action, I hope you just want to render an html page (because redirecting to the same url seems silly, and your syntax is wrong there too).
Hope this helps.

Can I have inputs without forms in a Rails view?

I want to have a few check boxes on my page, and a link that accesses the value of these check boxes to pass in a link_to helper. I did not want to use a form because the view essentially has a number of links interspersed, and it doesn't naturally seem to be a form with one logical submit button.
I have something like
<% for p in #some_array %>
<!--other stuff .... -->
<input value=<%= p.id %> id=<%= p.id %> name="selected[]" type=checkbox>
<!--other stuff .... -->
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
But it doesn't seem to recognize the variable selected which stores the inputs. It raises
undefined local variable or method `selected' for #<#<Class:0x000001021b4a38>:0x00000102319a90>
I would say the last line in your code snippet is causing the error:
<%= link_to "View all selected thing(s)", :action => 'show_selected', :selected_things => selected[] %>
As ruby complains about a selected not being defined.
(Although I can't see why you don't want to use a form, as forcing the selections into something you can pass to the link would require some javascript magic.)