Button_to scroll down to specific section - html

At the top of the user's profile I have this button:
<%= button_to 'Make a detailed enquiry', user_path(#user), :class=>"enquirySection", :method => :get %>
Then at the very bottom of the page I am rendering a form in which the viewer can make an enquiry:
<div class="enquirySection">
<%= render "enquiries/form" %>
</div>
I want the user to be scrolled down to this form when (s)he clicks on the button so I added the ":class=>"enquirySection"" and "div class="enquirySection"". It doesn't work.
I tried something similar to that but I think it does not related to my case.
Any clue of how is this done in Rails with button_to?
Thank you.

You don't need a class, but an id
try
<%= button_to 'Make a detailed enquiry', user_path(#user, anchor: :enquirySection), :method => :get %>
or simply
<%= link_to 'Make a detailed enquiry', user_path(#user, anchor: 'enquirySection) %>
and then
<div id="enquirySection">
<%= render "enquiries/form" %>
</div>
would do the job.

Related

Submitting multiple forms in one click

I was looking at the question below, which has a good answer for what I will ask, but I have a doubt. I cannot comment yet so that is why I am making another post.
How to submit multiple, duplicate forms from same page in Rails - preferably with one button
This is my view for the form
new.html.erb
<h1 class="page-title">Nuevo Precio</h1>
<hr class="title-division">
<div class="alta-form-container">
<%= form_tag "/precios" do %>
<% 2.times do %>
<%= render 'form', precio: #precio %>
<% end %>
<%= submit_tag "Submit", class: "btn btn-success" %>
<% end %>
<%= link_to 'Lista Precios Cliente', client_precios_path %> <br><%= link_to 'Ver', [#client, #precio] %> <br>
</div>
and my _form.html.erb
<div class="field">
<%= label_tag :precio, "Precio" %>
<%= text_field_tag "precios[][precio]" %>
</div>
<div class="field">
<%= text_field_tag "precios[][cant_acomodato]" %>
</div>
<div class="field">
<%= hidden_field_tag "precios[][client_id]" %>
</div>
When I submit the form I get an error that says
'No route matches [POST] "/precios"'
which I'm guessing is because on my new.html.erb I wrote form_tag "/precios" do
Any thoughts on what I should change or edit? thanks in advance
actually you have only one form here, because _form.html.erb will generate only inputs, that according to form below
<%= form_tag "/precios" do %>
that will generate
form action="/precios" method="post">
if you want leave it like that, you should add to your config/routes.rb
post '/precios', to: 'controller_name#method_name', as: :controller_name_method_name
your situation:
post '/precios', to: 'PreciosController#create', as: :precios_create
also check this
documentation

Remove unexpected content on a website

While following a tutorial on building a Ruby-on-Rails blogging website, I'm running into some unexpected results. The project so far is stored on https://github.com/khpeek/jumpstart-blogger.
The main page is an "Articles" page, which looks like this:
So far, so good (except for the somewhat curious position of the "Create a New Article" button, which used to be directly below the articles).
The appearance of "All Articles" is governed by app/views/articles/index.html.erb, which reads
<h1>All Articles</h1>
<ul id="articles">
<% #articles.each do |article| %>
<li>
<%= link_to article.title, article_path(article), class: 'article_title' %>
</li>
<% end %>
</ul>
<%= link_to "Create a New Article", new_article_path, class: "new_article" %>
The h1 heading is the first thing in the .html.erb file, and also the first thing that appears on the web page.
However, if I click on an article link, say "Article with Ruby Tag", I see the page below:
Besides the desired box with the article, tags, and comments, there are also two submit buttons and "<< Back to Articles List" buttons which are neither desired nor expected.
The appearance of this page is governed, as I understand it, by app/views/articles/show.html.erb, which reads
<h1><%= #article.title %></h1>
<p>
Tags:
<% #article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</p>
<% if #article.image.exists? %>
<p><%= image_tag #article.image.url %></p>
<% end %>
<p><%= #article.body %></p>
<h3>Comments (<%= #article.comments.size %>)</h3>
<%= render partial: 'articles/comment', collection: #article.comments %>
<%= render partial: 'comments/form' %>
<%= link_to "<< Back to Articles List", articles_path %>
<% if logged_in? %>
<%= link_to "delete", article_path(#article), method: :delete, data: {confirm: "Really delete the article?"} %>
<%= link_to "edit", edit_article_path(#article) %>
<% end %>
The first line in this file is the h1 header, but the 'unexpected' contents seems to come before that. So I'm having trouble seeing where to start to remove this content. Any pointers?
You're dealing with layout Rails' concept. Read this.
Anyway, you probably have a layout file in app/views/layouts.
check your application.html.erb in layouts folder.. it is rendering in header on some condition

Adding DatePicker Bootstrap and Centering Simple_Form_For

This is my form for now in my new_html_erb.
We are doing a promotion for the next two weeks
<%= simple_form_for #subscription, :html => { :class => 'form-horizontal'} do |f| %>
<%= f.input :manual_expiry_date %>
<%= f.association :user %>
<%= f.button :submit, :class => 'btn btn-primary'%>
<% end %>
I have used twitter bootstrap on and off. The form that I am working on for a rails application is using bootstrap.
How do I add bootstrap datepicker to manual_expiry_date ?
How do I center my simple_form_for?
I have tried putting the form in a container, then a row, then adding form control all in divs. There was no marked change. I have tried adding it as a class to simple_form but still the same.
Thank you for the help.
Twitter Bootstrap dont have in his core a date-picker, buy you can use "DatePicker For Bootstrap" www.eyecon.ro/bootstrap-datepicker/
You need to specify the class of the object
<%= f.input :manual_expiry_date, input_html: { class: 'date_picker' } %>
and then
$('.date_picker').datepicker()

Basic rails form, does not deselect alternative options - can't find solution for rails

I have excel, video, and multiple choice boolean columns in my Step model. I am trying to get it so the user has to choose one of the three, and when saved it passes back to the database as true. Right now my two problems are a)when I select one, it doesn't deselect the others and b) the radio button is on a different line than the text that labels it. Any help would be appreciated.
<fieldset class="stepCreator">
<%= "Step" %>
<%= f.label :description, "Description" %>
<%= f.text_field :description %>
<div>
<%= f.label :excel, "Excel" %>
<%= f.radio_button(:excel, true, :checked => true) %>
<%= f.label :video, "Video" %>
<%= f.radio_button(:video, true) %>
<%= f.label :multiple_choice, "Multiple Choice" %>
<%= f.radio_button(:multiple_choice, true) %>
</div>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "btn btn-danger btn-mini remove_fields "%>
</fieldset>
Actually you have 2 very different problems.
All the radio buttons (that you want to select one and automatically deselect the others) must have the same name, and different values, so it should be something like:
f.radio_button(:media, :excel, :checked => true)
f.radio_button(:media, :video)
For the labels to be at the same line as the checkbox, you just have to change the CSS.
From the documentation:
http://guides.rubyonrails.org/form_helpers.html
try:
<%= radio_button_tag(:age, "child") %>
<%= label_tag(:age_child, "I am younger than 21") %>
<%= radio_button_tag(:age, "adult") %>
<%= label_tag(:age_adult, "I'm over 21") %>
radio buttons have to select the same attribute in your model, in this example i selected the age attribute and i can give it 1 of two values, or he is a child or he is an adult
hope it helps

Spree User information

I'm using Spree as an eCommerce solution for a website that I am building and need to customize the layout, the deface option is great but I already have a layout for the rest of my application that I want to use so there is no change between the main part of my site and the store.
Having looked through the spree source code I have been able to transfer most of the infrastructure over to my own new template but I have hit one stumbling block. In the default template above the search box there are login/account and logout links which based on some digging through the code is rendered in the 'header' partial which itself renders the nav bar which renders the search bar partial, and the main nav bar which shows the links to home and the shopping cart, but nowhere mentions the user in/out stuff. Commenting out the rendering of the header partial removes all of this stuff but also the user information, which as far as I can tell isn't mentioned anywhere...
Does anyone know where in the default spree layout it implements this code for showing a login/logout account link, I could do this relatively simply with basic links to it but would rather understand how Spree implements this itself and I'm trying to keep my layout compatible with any updates to the core code.
Any help would be greatly appreciated, thanks!
Edited: For clarity
On version 3.0.1 of Spree and maybe on other older versions the user functionality is not handled by the core itself. Users are handled by the Spree Auth (Devise) Spree extension. This extension is included by default on new Spree apps. The extension uses a deface override to add this functionality. Interestingly enough the partial used by this override is not on the extension itself but rather on the Spree frontend files. I assume to make this component reusable should you wish to implement your custom user module. This is the code of the partial:
<% if spree_current_user %>
<li><%= link_to Spree.t(:my_account), spree.account_path %></li>
<li><%= link_to Spree.t(:logout), spree.logout_path %></li>
<% else %>
<li id="link-to-login"><%= link_to Spree.t(:login), spree.login_path %></li>
<% end %>
Found code in the RDR theme that explains this, still not sure why it doesn't show up in the default spree code, maybe a data hook?
The way RDR does it:
<% if current_user %>
<%= link_to t('logout'), destroy_user_session_path, :class => "cart" %>
<%= link_to t('my_account'), account_path, :class => "cart" %>
<% else %>
<%= link_to t('log_in'), login_path, :class => "cart" %>
<% end %>
override menu with
<% if current_user %>
<%= link_to t(:logout), spree.destroy_user_session_path %>
<%= link_to t(:my_account), spree.account_path %>
<% else %>
<%= link_to t(:login), spree.login_path %>
<% end %>
As of Spree 2.3.2 same thing can be done with:
<h2>Your Account</h2>
<ul>
<% if spree_current_user %>
<li><%= link_to t('My Account'), account_path, :class => "cart" %></li>
<% else %>
<li><%= link_to t('Log In'), login_path, :class => "cart" %></li>
<li><%= link_to t('Sign Up'), registration_path, :class => "cart" %></li>
<% end %>
</ul>