Form submit button not showing - ruby-on-rails

I'm working on a project where I'm trying to implement a voting system, but I'm having some trouble, I've tried creating a form with a hidden field:
<% form_for :vote, url: votes_path do |f| %>
<%= f.hidden_field :rating, value: '1' %></td>
<%= form_submit_button("vote 1") %>
<% end %>
But when I run that the submit button does not appear, I'm not really sure what I'm doing wrong.

form_for generates <FORM> tag, so you need to include its output with <%=:
<%= form_for :vote, url: votes_path do |f| %>

Use this code:
<%= form_for :vote, url: votes_path do |f| %>
<%= f.hidden_field :rating, value: '1' %></td>
<%= form_submit_button("vote 1") %>
<% end %>
= is usually shows the form in view page, if you remove it then any input tag will not be displayed.

Related

Change parameter name FORM_WITH VS FORM_FOR

I am using rails 5 and the form_with helper. My code works when using Form_for, but not Form_with. Is the :as parameter not valid with Form_with?
View
<%= form_with(model: #user_wizard , :as => :user_wizard, url: validate_step_wizard_path, local: true, builder: BsFormBuilder) do |f| %>
Model
def user_wizard_params
params.require(:user_wizard).permit( :name )
end
However, every time I try to submit the form, the parameter submits:
{"utf8"=>"✓",
"authenticity_token"=>"LvRDsdfsdfsd3V0l4NLg14q2JWBdwkDPqUIu2l7SXDiioCtvMwW6Bv3ss/LPSS9+bdxiPIzjg==",
"current_step"=>"step1",
"wizard_user_step1"=>
{"name"=>"Name"}
}
I have been following this.. https://medium.com/#nicolasblanco/developing-a-wizard-or-multi-steps-forms-in-rails-d2f3b7c692ce
However, my code I use Form_with and they use Form_for. I don't know what the difference is.
Rails form_with replaced the as option with scope, which has the same functionality. Your example should look like this:
<%= form_with(model: #wizard , scope: :user_wizard) do |f| %>
Now your inputs should have names like user_wizard[attribute].
form_for using underlying model instance for example here #post as a model
<%= form_for #post do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
form_tag is form helper but don't have underlying model and you should use text_field_tag instead of text_field
<%= form_tag “/posts” do %>
<%= text_field_tag “post[author]” %>
<%= submit_tag “Create” %>
<% end %>
form_with will combine form_for and form_tag and later these two will be unify with form_with
here implementation of form_for and form_tag using form_with, as you can see below the difference,
if you passing form_with with a model it will work as form_for but if you passing it url then it will work as form_tag
<%= form_with model: #post do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
<%= form_with url: “/posts” do |form| %>
<%= form.text_field :author %>
<%= form.submit “Create” %>
<% end %>
meanwhile for your problem there is no options as, you can check from this link for more detail options

Displaying hstore saved values in form for input when editing

I got a problem with displaying saved values in form_for for hstore data (filters).
It's saved in database but when I'm going back to this view to edit something I can't see actual values in input form fields like in normal form_for without hashes.
This is shorcut of me view code
<%= form_for #lesson, url: { action: "step3" } do |f| %>
<%= f.fields_for :filters do |d| %>
<%= #lesson.filters["age_from"] %> # like this value would be displayed
<%= d.text_field :age_from %>
<%= d.text_field :age_to %>
<%= d.text_field :name %>
<%= link_to "Back", step1_lesson_path(#lesson) %>
<%= submit_tag "Next" %>
<%end%>
<%end%>
Thanks in advence
Can you try with changing the view a bit, passing #lesson.filters in fields_for as following
....
<%= f.fields_for :filters, OpenStruct.new(#lesson.filters) do |d| %>
#your code goes here ...
<% end %>
....
Solution without modifying controller action:
<%= f.fields_for :filters, OpenStruct.new(#lesson.filters) do |d| %>

Ruby Rails is it possible to check before displaying form?

Is it possible to check submission before showing this simple form:
<%= form_for #article, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, size: "60x12" %>
<%= f.submit "Create" %>
<% end %>
like if its already submitted first time show other html content instead ?
Test the object. If you're using ActiveRecord/Mongoid:
<% if #article.new_record? %>
... display form ...
<% else %>
... do something different ...
<% end %>

Rails checkboxes and serialized data

I'm trying to figure out whats the best way to get checkboxes to properly show their current state. This is what I have in my form
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= f.fields_for :notification_preferences, #user.notification_preferences do |p| %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_post %>
<%= p.check_box :notify_on_friend_request %>
<%= p.check_box :notify_on_friend_comment %>
<% end %>
<%= f.submit %>
<% end %>
notification_preferences is a serialized hash on my user model
class User < ActiveRecord::Base
serialize :notification_preferences, Hash
My issue that is no matter what I try, I can not get the check boxes to reflect the existing state of the hash values. IE, if the hash already contains :notify_on_friend_post => 1, then the check box for that value should be checked.
The form posts the data fine, and I'm able to update my model as well.
Update
using check_box_tag I can get this to work
<%= p.hidden_field :notify_on_friend_post, :value => "0" %>
<%= check_box_tag "user[notification_preferences][notify_on_friend_post]", "1", #user.notification_preferences[:notify_on_friend_post] == "1" ? true : false %>
ugly but working, still hoping I'm missing something very obvious
I ran into this problem and solved it in a simpler way.
<%= form_for #user do |f| %>
<%= f.fields_for :notifications, #user.notifications do |n| %>
<%= n.check_box :new_task, checked: #user.notifications[:new_task] == "1" %>
<% end %>
<%= f.submit %>
<% end %>
In this way you let the magic of check_box to the work and don't need to have a hidden_field because Rails will provide one for you. Still unsure why you need a "checked" field, but it seemed to not work without one.
Try something like this:
<%= form_for #user, :url => user_notification_preferences_path(#user), :method => :put do |f| %>
<%= check_box_tag "user[notification_preferences][]", :notify_on_friend_post, #user.notification_preferences.try(notify_on_friend_post) %>
<%= f.submit %>
<% end %>

Hiding Checkbox & Assigning Value - Ruby on Rails - Easy Question

I am trying to hide a checkbox and assign a default value of 1 such that the submit button only shows. Here is my form. Just wondering as the proper format as I am new to rails. I think you can do this with helpers but was wondering if I can just include it in the form. Here is the form:
<% remote_form_for [#post, Vote.new] do |f| %>
<p>
<%= f.label :vote %>
<%= f.check_box :vote %>
</p>
<%= f.submit "Vote" %>
You can certainly do this, but if all you want is to set a parameter without displaying a field, what you probably want instead is a hidden field:
<%= f.hidden_field :vote, :value => '1' %>
If you really do want a hidden checkbox (maybe so you can optionally display it later using javascript?), you can do it like this:
<%= f.check_box :vote, :checked => true, :style => 'visibility: hidden' %>
You could use CSS to hide the checkbox:
<%= f.check_box_tag :vote, 1, true, :style => "display: none;" %>
But if you just want to pass a value you can just use a hidden field:
<%= f.hidden_field_tag, :vote, 1 %>
If you just want to pass the value along, use a hidden field
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field_tag 'vote', '1' %>
<%= f.submit "Vote" %>
<% end %>

Resources