What is the purpose of the 'hidden_field'? - ruby-on-rails

For example here's a snippit:
<%= form_for [#event, #event.comments.new], html: {class: 'form-horizontal', role: 'form'}, remote: true do |f| %>
<div><%= f.hidden_field :user_id, value: current_user.id %></div>
<div class="form-group">
What is the purpose of the f.hidden_field :user_id, ? Would the code work without it, or is it a necessity to include this line.
Cheers!

Without seeing the controller code which would process the form, the hidden field seems to be holding the id of the user currently logged in. I assume this would be used in further actions, for example, to attribute the event/comment being created against that user.
The view doesn't need the hidden field, but I would imagine the following controller action would fail if it were removed.
The hidden_field method simply creates <input ... type="hidden" /> in the html which means the value is included in the form parameters when the form submits, but the value is not visible to the user.

In Your Case,you are creating an Event with Comments for a particular User.So the user_id is passed as a hidden_field.Without it the value for user_id will be saved as nil,which would resulting an Error.

Related

Making a dropdown and getting the value in Rails

I have a many-to-many relation between my User model and my Project model for the purpose of assigning projects to users. I'm now trying to impement the feature for actual assignment using a dropdown containing the projects on the edit user page. My code looks like this, but I can't seem to figure out how send the selected value back to the controller once the button is clicked:
<div class="input-group my-2">
<%= collection_select('user', 'project', Project.all, :id, :name, include_blank: true) %>
<div class="input-group-append">
<%= link_to '<div class="btn btn-outline-primary">Toggle access to selected project</div>'.html_safe, toggle_project_access_user_url %>
</div>
</div>
I've tried using the suggestions for JS I've found, but I think I'm missing some knowlegde on how to implement the functionality completely with those.
Working with Javascript in Rails covers this.
Use form_with to set up a form with data-remote=true. Then proceed as normal.
form_with url: toggle_project_access_user_url, method: :patch do |f|
f.collection_select(:project_id, Project.all, :id, :name, include_blank: true)
f.submit("Toggle access to selected project")
end
This will send a POST to toggle_project_access_user_url with a hidden _method input indicating it's a PATCH. Use a hook on ajax:success to update the page.

How to use form field only as value in controller method with a corresponding column in the db?

I have the following simple form in a view:
<%= form_for(#invitation, method: :post, url: addinvitation_path) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<label for="email", title="email"></label>
<%= f.email_field :email, required: true %><br>
<%= f.submit "Submit" %>
<% end %>
Having added this form to a view, tests that use this view fail:
ActionView::Template::Error: undefined method `email' for #<Invitation:0x0000000baa4e88>
email is not a variable in the Invitation table, am I correct to assume that is the cause of the error? All I want is for the email field/value to be available in the controller method (where I will use it to find the right user). How can I do this? (at first sight it might look to make more sense to then use form_for(#user since the email field of that table but I don't want that since I will use the form to write to the Invitation table)
You can only use form_for object ('f' in your case) with column names present in the table. As email is not a column in your invitation table so you need to use direct field_tag.
<%= email_field_tag :email, required: true %

Simple_Form display validation error messages next to different input field

I have a form that allows the user to search for existing records to populate an association. Each "Booking" belongs to a "Customer". So the form allows you type the customer's name, it does a search automatically, and you click the customer you want. The input field you're typing into should display the customer's name, but for the form to work, I set the customer_id in a hidden input field.
I'm using the simple_form gem. Does anybody know if I can display the validation errors for the customer_id next to the text input field that displays the customer's name? The customer_id is required in the model, so I need the form to tell the user that if they leave it blank.
View code (simplified -- there's some JavaScript that handles searching when you type into the customer text box, and that sets the value in the hidden field to the customer's id when you make a selection):
<%= simple_form_for #booking do |f| %>
<%= f.hidden_field :customer_id, id: "customer_id" %>
<%= f.input :customer, required: true,
input_html: { value: #booking.customer_name } %>
<% end %>
I eventually found out about the append block in simple_form (buried in a pull request, not documented anywhere else that I could find). Basically, you can use that to append whatever markup you want to your f.input. I did the following, which is pretty hacky, but it does what I need it to do:
<%= f.input :customer, required: true,
wrapper_html: { class: ("error" if #booking.errors[:customer_id].any?) } do %>
<%= f.input_field :customer, value: #booking.customer_name %>
<%= f.error :customer_id %>
<% end %>
First, I had to conditionally add the class "error" to the wrapper if there were any errors on the related field, then I used the append block to render out the input field, followed by the errors for the related field from the model. Hope this helps someone in the future!

Passing arbitrary text value to ruby controller action

I know this is answered elsewhere, but I was looking and couldn't find what I needed.
I'm passing a value to a method that adds a value to a numeric attribute in the object.
I have a form, but I can't figure out how to give a textbox an arbitrary name and reference it in the view. If I pass the textbox an arbitrary symbol, it looks for that attribute in the object. I just need to name the textbox something random, and have the value pass to the controller action. The textbox value should not be connected to anything on the object, any help?
The controller action (does nothing right now, waiting to figure out how to pass values):
def addto(valu)
end
The view form:
<%= form_for(#cooler) do |f| %>
<div class="field">
<%= f.label "Increase/subtract from value:" %><br />
<%= f.text_field :dfdf%>
</div>
<p style="float:left;"><%= button_to "+", coolers_addto_path(:valu => 2)%></p>
<% end %>
You can use parameters in controller, like this:
def addto
# Here you can access your value with params[:val]
end

How do you create a checkbox that retains its value on a form render for a Ruby on Rails form?

What I am doing is pretty simple but the value is never retained on form being rendered because of failing validation. I basically have a Article model that has a published boolean field that can either be true or false.
If I change the value from say its initial render value of true by unchecking the checkbox, and then I fail validations and the form gets re-rendered, then it shows the original value of true in the database. How do I retain the recent change even on a validation issue and the form gets re-rendered like this? Thanks
form code:
= form_for #article, :url => article_path do |f|
= f.text_field :name
= f.text_field :description
= f.check_box :published
= f.submit "Update"
html code:
<input type="hidden" value="0" name="article[published]">
<input id="article_published" class="pull-left" type="checkbox" value="1" name="article[published]">
Adding published to attr_accessible fixed the issue.

Resources