How to add corrupted submit button? - ruby-on-rails

I want to have form for possible answers in poll and two submit buttons. First one passing all text fields, second one passing one of these fields as nil causing validation error. This is my _form.html.haml file:
= form_for [#poll, #question] do |f|
- if #question.errors.any?
#error_explanation
%h2= "#{pluralize(#question.errors.count, "error")} prohibited this question from being saved:"
%ul
- #question.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :title
= f.text_field :title
%p
= f.label :kind
.radio
-#kind_options.each do |option|
%label
=f.radio_button :kind, option[1]
=option[0]
=f.fields_for :possible_answers do |c|
%p
=c.text_field :title, class: "form-control", placeholder: "Enter a possible answer"
.actions
= f.submit 'Save', class: "btn btn-primary"
- x = f
= x.hidden_field :kind, :value => nil
= x.submit 'Corrupted Sumbit Button', class: "btn btn-primary"
It seems that there is no difference if I submit x or f, :kind is blank even I choose it in radio button and click on f.submit button. How to work this out?

Related

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/

Translation for form_tag submit button

In Rails, when you have a form like this:
<% form_for #post do |f| %>
<%= f.submit %>
<% end %>
You can have a specific translation like this for when the button is clicked:
en:
helpers:
submit:
create: "Create a {{model}}"
update: "Confirm changes to {{model}}"
But I have a form_tag, which submits a GET to an index path to set a filter:
= form_tag admin_dashboard_index_path, method: :get, class: "table_filter" do
= select_tag :company, options_for_select(#filter_companies.map{ |c| [c.name, c.id] }, params[:company]), include_blank: true, class: "selected"
= submit_tag :submit, value: I18n.t('.general.filter'), class: 'btn'
How can I get a specific translation for this situation?
Add this to the method so that the button label stays the same after pressing it.
= submit_tag :submit, value: t('.general.filter'), class: 'btn', data: { disable_with: t('.general.filter') }
submit_tag method

Undefined method `simple_field_for' for #<#<Class:0x007fa76a658768>:0x007fa76a962d50>

I'm attempting to use the cocoon gem to created some nested forms mixed with simple form and I get getting Undefined method simple_field_for when I implement the form. I'm fairly new to rails so I apologize in advance if this is a stupid fix but I've scanned Stack Overflow and Google long enough where I've decided to just post and ask. The tutorial I'm following uses haml in the views. Below is my code, please let me know if something else needs to be posted to assist with the error. Thank you!
Total Error Message:
undefined method `simple_field_for' for #<#<Class:0x007fa76a658768>:0x007fa76a962d50>
.row
.col-md-6
%h3 Ingredients
#ingredients
= f.simple_field_for :ingredients do |ingredient| #this is the line throwing the error
= render 'ingredients_fields', f: ingredient
.links
= link_to_add_association 'Add Ingredient', f, :ingredients, class: 'btn btn-default add-button'
Partial File Throwing The Error: _form.html.haml
= simple_form_for #recipe, html: { multipart: true } do |f|
- if #recipe.errors.any?
#errors
%p
= #recipe.errors.count
Prevented this recipe from saving.
%ul
- #recipe.errors.full_messages.each do |msg|
%li= msg
.panel-body
= f.input :title, input_html: { class: 'form-control' }
= f.input :description, input_html: { class: 'form-control' }
= f.input :image, input_html: { class: 'form-control' }
.row
.col-md-6
%h3 Ingredients
#ingredients
= f.simple_field_for :ingredients do |ingredient|
= render 'ingredients_fields', f: ingredient
.links
= link_to_add_association 'Add Ingredient', f, :ingredients, class: 'btn btn-default add-button'
= f.button :submit, class: 'btn btn-primary'
Ingredient Field Partial: _ingredients_fields.html.haml
.form-inline.clearfix
.nested-fields
= f.input :name, input_html: { class: 'form-input form-control' }
= link_to_remove_association "Remove", f, class: 'form-button btn btn-default'
Again, let me know if any other files need to be included. Thank you!
Its simple_fields_for, not simple_field_for. You are missing s.
Read Simple Form Documentation for more info.

Rails duplicate of same form in view leaves fields empty

I have a view associated with one model but there are multiple versions of the same form that are hidden until a jquery function shows them. When I try to submit one, all the fields are empty.
Here is the view in question:
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
.field
= f.label :svg_ref, "SVG PO Number"
= f.text_field :svg_ref
.field
= f.label :vendor_ref, "Vendor SO Number"
= f.text_field :vendor_ref
.field
= f.label :due
= f.text_field :due
= f.hidden_field :rfi_type, value:"order"
.actions
= f.submit
.rfi_type.rfi_type_quote
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
.field
= f.label :reference, "Quote number"
= f.text_field :reference
= f.hidden_field :rfi_type, value:"quote"
.field
= f.label :due
= f.text_field :due
.actions
= f.submit
This is the Jquery involved
$ ->
$(".rfi_type").hide()
$(".rfi_type_order").show()
$("input:radio[name=rfi_type]").change ->
$(".rfi_type").hide()
$(".rfi_type_"+$(this).val()).show()
return
return
I believe 'one' of the problems you may be facing is your use of the hidden_field with the same name. I would suggest you restructure both your haml and coffee-script to something like this
$ ->
$("#quote-fields").hide()
$("input:radio[name=rfi_type]").change ->
$(".show-hide-container").hide()
type = $(this).val()
$("#" + type + "-fields" ).show()
$("#rfi_type-field").val(type)
= form_for #rfi do |f|
- if #rfi.errors.any?
#error_explanation
h2 = "#{pluralize(#rfi.errors.count, "error")} prohibited this rfi from being saved:"
ul
- #rfi.errors.full_messages.each do |message|
li = message
#order-fields.show-hide-container
.field
= f.label :svg_ref, "SVG PO Number"
= f.text_field :svg_ref
.field
= f.label :vendor_ref, "Vendor SO Number"
= f.text_field :vendor_ref
#quote-fields.field.show-hide-container
= f.label :reference, "Quote number"
= f.text_field :reference
.field
= f.label :due
= f.text_field :due
= f.hidden_field :rfi_type, value: "order", id: "rfi_type-field"
.actions
= f.submit

Form not Rending Correctly in HAML

So I have this form written in haml
= form_tag assign_photographers_path, class: "col-md-12 assign-photographers-form-#{portfolio.id}" do
= hidden_field_tag :portfolio_id, portfolio.id
= hidden_field_tag :user_id_1, "", class: "user_id_1"
= hidden_field_tag :user_id_2, "", class: "user_id_2"
= label_tag :user, "Begin typing to assign the first photographer:"
%br
.col-md-8.col-md-offset-2
= text_field_tag :user, nil, class: "form-control photographer-search", data_id: portfolio.id
.clearfix
%div{class: "photographer-list#{portfolio.id} col-md-6 col-md-offset-3"}
%h3 Chosen photographers
.clearfix
%br
= submit_tag "Assign Photographers", :class => "btn btn-primary"
In the source code it shows
<form accept-charset="UTF-8" action="/photographers/assign" class="col-md-12 assign-photographers-form-19" method="post"></form>
The contents of the form are then printed below the form. I have no idea why it's printing outside of the form tags. What's wrong with the code and how can I fix it?

Resources