Ruby on Rails - link_to - quotes in class - ruby-on-rails

This is what I want:
class = "navbar-toggle" data-toggle = "collapse" data-target = ".navMenuCollapse"
This is what I got:
class="\"navbar-toggle\" data-toggle = \"collapse\" data-target = \".navMenuCollapse\""
This is what I tried:
<%= link_to 'test', welcome_index_path, {:class => '\"navbar-toggle\" data-toggle = \"collapse\" data-target = \".navMenuCollapse\"'} %>
Is there any opportunity to get quotes in the class-tag ?
Thanks for help.

Try the below code:
<%= link_to 'test', welcome_index_path, class: 'navbar-toggle', data: {toggle: 'collapse', target: '.navMenuCollapse'} %>

This should work:
<%= link_to 'test', welcome_index_path, :class => 'navbar-toggle', :'data-toggle' => 'collapse', :'data-target' => '.navMenuCollapse' %>

Related

Rails form_with to link_to

I have a form_with like this:
= form_with model: product.product_posts.new do |f|
= hidden_field_tag :container, '#related-products-list'
= f.hidden_field :product_id
= f.hidden_field :post_id, value: #post.id
= f.submit "+", class: "btn btn-info m-btn m-btn--icon m-btn--icon-only m-btn--pill add-action-button"
How to get the same with a link_to to get full div clickable instead of the "+" button?
=link_to {SOLUTION} do
.mydiv
You can use Javascript to achieve this. If you use jquery,
$(document).ready(function() {
$('.mydiv').click(function () {
$('#form_id').submit();
});
});

Add $ next to price input inputted by user

I have a recipe website made with rails and some haml on c9.io. On the show page, I'd like to display the price that the user inputted in a simple form with a dollar sign next to it. I tried using a <p> tag, however the dollar sign appears on another line.
Here is my show.html.haml file:
#post_show
%h1= #post.title
%p.username
Shared by
= #post.user.name
about
= time_ago_in_words(#post.created_at)
.clearfix
.post_image_description
= image_tag #post.image.url(:medium)
.description= simple_format(#post.description)
%h6 Notes:
.notes= simple_format(#post.notes)
%h6 Price:
%p
.price= (#post.price)
.post_data
= link_to "Visit Link", #post.link, class: "button", target: "_blank"
= link_to like_post_path(#post), method: :get, class: "data" do
%i.fa.fa-thumbs-o-up
= pluralize(#post.get_upvotes.size, "Like")
= link_to dislike_post_path(#post), method: :get, class: "data" do
%i.fa.fa-thumbs-o-down
= pluralize(#post.get_downvotes.size, "Dislike")
%p.data
%i.fa.fa-comments-o
= pluralize(#post.comments.count, "Comment")
- if #post.user == current_user
= link_to "Edit", edit_post_path(#post), class: "data"
= link_to "Delete", post_path(#post), method: :delete, data: { confirm: "Are you sure?" }, class: "data"
#random_post
%h3 Check this out
.post
.post_image
= link_to (image_tag #random_post.image.url(:small)), post_path(#random_post)
.post_content
.title
%h2= link_to #random_post.title, post_path(#random_post)
.data.clearfix
%p.username
Share by
= #random_post.user.name
%p.buttons
%span
%i.fa.fa-comments-o
= #random_post.comments.count
%span
%i.fa.fa-thumbs-o-up
= #random_post.get_likes.size
#comments
%h2.comment_count= pluralize(#post.comments.count, "Comment")
- #comments.each do |comment|
.comment
%p.username= comment.user.name
%p.content= comment.content
%h2 Share your opinion:
= render "comments/form"
And here is my posts' form.html.haml:
%div.container
= simple_form_for #post do |f|
= f.input :image
= f.input :title
= f.input :link
= f.input :description
= f.input :notes
= f.input :price
%br
= f.button :submit, class: "btn btn-info"
Help would be greatly appreciated.
EDIT:
So now, I have added the following:
%span .input-group-addon $ .price= (#post.price)
However, the dollar sign is on the top line, and the price is on the bottom.
p is block content so will cause a new line by default in html. You can either handle this with CSS or use something that is inline like a span tag
Haml input with preceding dollar sign just include in your code as you want to:
Or just use classes:
.input-group
%span.input-group-addon $
%input.form-control{:placeholder => "Price", :type => "text"}/
Looks like: http://jsfiddle.net/s6Xu6/1/

Two select_date in form_tag

I have a form_tag with two "select_date"
<%= form_tag installation_path, :method => :get, :class => 'form-search' do %>
<%= select_date(date = Date.current, options = {}, html_options = {}) %>
<%= select_date(date2 = Date.current, options = {}, html_options = {}) %>
In backend I dont receive date2, I only receive date but in the view 2 select_date are displayed. When I inspect params I only see 1 and I dont know why
{"utf8"=>"✓", "date"=>{"day"=>"21", "month"=>"11", "year"=>"2015"}...}
Use prefixes:
<%= form_tag installation_path, :method => :get, :class => 'form-search' do %>
<%= select_date(Date.current, prefix: "date1") %>
<%= select_date(Date.current, prefix: "date2") %>
You will get:
{"utf8"=>"✓", "date1"=>{"day"=>"21", "month"=>"11", "year"=>"2015"}, "date2"=>{"day"=>"21", "month"=>"11", "year"=>"2015"}...}

Rails strips html class tag from button_to

I have the following in my view:
<%= button_to 'Check In', controller: "posts", action: :check_in, id: #post.id, :class => "btn", :style => "display:inline" %>
For some reason, the :class and :style do not end up in the HTML that is rendered. I have tried putting this class and style on other ERB tags and they get rendered from there. Why are they being stripped on this tag only?
button_to(name = nil, options = nil, html_options = nil, &block)
so, you need to wrap the options and html_options attributes to hashes
<%= button_to 'Check In', { controller: "posts", action: :check_in, id: #post.id }, { :class => "btn", :style => "display:inline" } %>
Many tag helpers require HTML attributes to be in a :html key.
<%= button_to 'Check In', controller: "posts", action: :check_in, id: #post.id, :html => {:class => "btn", :style => "display:inline"} %>

ruby haml don't get the results I want (beginner)

I'm struggling with haml not injecting the content at the right place, here is part of the haml :
%div.form-group{:id => 'container'}
- if !#data.nil?
%p= t('users.data_name')
= link_to t('users.delete'), 'javascript:void(0)', :class => 'delete', :data => {:id => #data.id}
%div.holder.thick
= image_tag #data.path
- else
= link_to t('users.upload'), 'javascript:void(0)', :class => 'btn btn-large'
%div.holder
= image_tag '/assets/missing_image.png'
The problem is that the holder or holder thick are not inside my container but outside. Why is this? What am I not aligning properly?
Yes, the if should be indented if its content is meant to go into #container.
#container.form-group
- if !#data.nil?
%p= t('users.data_name')
= link_to t('users.delete'), 'javascript:void(0)', :class => 'delete', :data => {:id => #data.id}
.holder.thick
= image_tag #data.path
- else
= link_to t('users.upload'), 'javascript:void(0)', :class => 'btn btn-large'
.holder
= image_tag '/assets/missing_image.png'
if your container is %div.form-group then you have just to indent the rest so that haml understands it's inside that div that your content should go
so it'll look like that
%div.form-group{:id => 'container'}
- if !#data.nil?
%p= t('users.data_name')
= link_to t('users.delete'), 'javascript:void(0)', :class => 'delete', :data => {:id => #data.id}
%div.holder.thick
= image_tag #data.path
- else
= link_to t('users.upload'), 'javascript:void(0)', :class => 'btn btn-large'
%div.holder
= image_tag '/assets/missing_image.png'

Resources