I have a question with turning this html into erb.
<button name="support" type="button" value="Get Support" class="brightOrange">
<span>Get Support</span>
</button>
I've been trying to do something like this in rails.
<% form_tag get_support_path do %>
<%= text_field_tag :email, "Email:" %>
<% submit_tag "Join", :class=>"brightOrange" %>
<% end %>
Thanks for the help in advance. Not quite sure how to do this.
Try this:
button_to "Get Support", {:action => 'support/get'}.
you can give HTML options also.
See following links for more details
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
http://snipplr.com/view/866/ruby-on-rails-form-helper-with-image-submit-button/
http://blog.moertel.com/articles/2005/05/08/taking-the-unsafe-gets-out-of-rails
Related
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
Well my problem is when i call my model Question and show in a .each do, my view show the ActiveRecord but i don't now how to hide this or the right way to show my questions without the ActiveRecord.
In my view the content show like this:
<%= #questions = Question.all.order(:id).reverse_order %>
<% #questions.each do |question| %>
<% if #course.id == 1 %>
<h5><%= link_to question.title, question , class: 'reply text-light text-decoration-none' %></h5>
<% end %>
<% end %>
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?
You could use <% %> instead of <%= %>:
<% #questions = Question.all.order(id: :desc) %>
And it's better to put that to controller:
class QuestionsController
def index
#questions = Question.all.order(id: :desc)
end
end
I found a solution if i want to call a Model in view, just create a input type="hidden" like this:
<input type="hidden" value="<%= #questions = Question.all.order(:id).reverse_order %>">
You should use url_helpers provided by Rails. See Examples provided under link_to docs.
Also I would suggest to follow the recommendation at https://stackoverflow.com/a/66470651/936494 and in your view change following
<h5><%= link_to question.title, question , class: 'reply text-light text-decoration-none' %></h5>
to
<h5><%= link_to question.title, question_path(question), class: 'reply text-light text-decoration-none' %></h5>
And if you don't have url_helpers available, then the 2nd argument should be a URL to a resource in your application.
Hope that helps. Thanks.
This is a simple question but I can't seem to find a solid answer. I simply want to know if this is valid? it's a basic form_for with in input at the bottom.
## Form
<%= form_for #snitch, html: { class: "form-actions", method: 'delete' } do |form| %>
<span class="button-text"><%= link_to 'NO WAY!', home_base_url_or_default(root_path), rel: "modal:close" %></span>
<button type="submit" class="button button--modal delete-snitch" data-snitch-id="<% #snitch.token %>" value="Yes, delete it.">
<% end %>
Is the third line valid? specifically where it says data-snitch-id="<% #snitch.token %>"? if it is not. can someone help me figure out how I can do something like that?
HTML data attributes are perfectly valid and widely supported. They're used to store custom data in an element. You can create elements with those attributes in rails helpers as well.
<%= button_tag "Yes, delete it.", type: :submit,
data: {"snitch-id" => #snitch.token},
class: 'button button--modal delete-snitch' %>
The only problem with your example is that you're not printing the value of #snitch.token. You should be using <%= #snitch.token %> instead of <% #snitch.token %>
Use #{ } expression instead.
data-snitch-id="#{#snitch.token}"
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
I know that if you just type something like <button>Something</button> outside a form_for in rails, it will create a useless button.
But I want to create buttons within this form_for to be handled by JavaScript.
Is there a way to create it?
This will create useless buttons that can be handled by JavaScript.
Plain HTML:
<input type="button" onclick="alert('Hello.')" value="Click Here" />
Rails:
<%= submit_tag "Click Here", :type => 'button', :onclick => 'alert("Hello.")' %>
If you're not looking for Rails to use it, why not just use the plain html inside the form_for?
<%= form_for #record do |f| %>
## inputs ##
<button>Something</button>
<%= f.submit %>
<% end %>
Check out this answer: How do I create multiple submit buttons for the same form in Rails?
<% form_for(something) do |f| %>
..
<%= f.submit 'A' %>
<%= f.submit 'B' %>
..
<% end %>