I have asked a similar question but I had no luck in finding an answer. I think I provided too much information, so this time it's going to be short and sweet.
Description
I have a reply form I want to render inside a paginated item. The paginated item is defined as #share_item therefore when I write codes inside that partial they will look like this:
<% if registered_member?(share_item.user) %>
Problem
The problem occurs when I want to render the form partial. With some code omitted, it looks something
<%= form_for(#blob) do |f| %>
<%= f.text_area :content, rows: "2", placeholder: "Reply to ##{share_item.user.name}" %>
#blob is the main model controller where everything is defined, where #share_item is a paginated item of #blob. I get the error share_item is an undefined model or method.
What Works
If I add the form's html directly inside the partial, I can add something like this:
<textarea id="blob_content" name="blob[content]" placeholder="Reply to #<%= share_item.user.name %>">#<%= share_item.user.name %> </textarea>
and I will receive no errors.
What I want
I want to be able to render the form rather than adding the form directly inside the partial (it's annoying, uncanny, and space-consuming). I have other codes that are similar, and I am basically just looking for a solution on how I can get the same results from rendering a form as adding the HTML to the partial under circumstances as these.
Try:
<%= form_for(#blob) do |f| %>
<%= f.text_area :content, rows: "2", placeholder: "Reply to #<%= #share_item.user.name%>" %>
Related
Noob question! :)
I have a form, that has basically no point other than call some_action. The reason I use a form for this, is because we have a specific styling for this in our large website.
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= f.save_button %>
<% end %>
I thought, since it's a form, I should be able to put a checkbox in there. It should have no other goal than confirming the user wants to do this action indeed. E.g. "Yes, I want to do some_action to the user model".
How would I make a checkbox that does not really change any attribute or affect anything - Other than that it should be checked for the form to submit?
This is probably dead simple, but according to the documentation and various error messages I should provide arguments such an attribute (which I don't want...)
form_for is meant to work on attributes of a model, which is what all the documentation you are reading is telling you. So if your model had a boolean column you could easily attach a check box to it.
If you ever want a form (or specific tag) that does not follow this, you can use the _tag version of these methods. For example, form_tag or, in your particular case, check_box_tag.
Example:
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= check_box_tag "do_some_method" %>
<%= f.save_button %>
<% end %>
NOTE: You will only get a param entry for :do_some_method if it is checked off. If you want to get a param regardless, you have to add a hidden_field_tag before it.
<%= hidden_field_tag "do_some_method", "no_dont_do_it" %>
<%= check_box_tag "do_some_method", "yes_do_it" %>
Now if the checkbox is selected you'll get params[:do_some_method] set to "yes_do_it"; if it's not checked off, instead of getting no entry, you'll get params[:do_some_method] set to "no_dont_do_it".
This is really strange, but when I use a fields_for helper in order to give users the ability to submit multiple instances of my model, the view displays the instance data on the page like so:
Embiggened: http://i.stack.imgur.com/Omjt0.png
The code I have which generates the form is like so:
<%= form_for :metric_request, url: '/metric_request', :html => {:multipart => true} do |metric_request| %>
<%= #metric_requests.each do |m| %>
<%= metric_request.fields_for m, index: m.id do |f| %>
It works fine in generating multiple copies of my form, but whenever it's included, the view page spits out the model as in the image above. What gives? When I remove the fields_for helper, the output model instance text disappears... ?!
Edit: as pointed out by Joel Brewer, this was caused by my stupid copy/paste fail. Didn't pay attention and included the <%= instead of <%
Try:
<% #metric_requests.each do |m| %>
instead of:
<%= #metric_requests.each do |m| %>
I want to highlight my input field when error happens, I exactly know how to do it in form_for(rails do it). And i search it on google, it just tell me the field_error_proc things, but it seems that function doesn't work in form_tag and this is my code:
<%= form_tag({controller: :user, action: :change_password, id: current_user.id}, {method: :patch}) do %>
<%= render 'shared/error_messages', object: #user %>
<h5>Old password:</h5>
<%= password_field_tag :password %>
<% end %>
Errors
We achieved this using a somewhat hacky effort here (click "register" at top & try submitting without data):
#app/views/controller/your_form.erb
<%= form_tag ...... do %>
<%= content_tag :div, #user.errors[:attribute], class: "error" if #user.errors[:attribute].present? %>
<% end %>
Every time you return an object to a view which has gone through validations, it should have an errors object attached.
I'm not sure if this will work directly with form_tag, but I do know it works with form_for (something you may wish to look into). You should read up on the ActiveModel::Errors object which is attached to model objects when they come back from validation
--
Implementation
If you use the code I created above, it will basically allow you to show any errors that are assigned to your attributes, when they are present.
Most people will refer you to the #object.errors.full_messages method - which basically displays the entire messages for the errors you've received. These full_messages basically give you attribute | message - which means if you loop through the errors object, it should allow you to refer each attribute individually
As mentioned, you can see a demonstration of this idea in my link above!
This has been driving me nuts because it doesnt seem to make any sense.
I want to do something relatively simple.
Display an edit form in a modal on the index page.
I have the following code looping through a collection of sites
<%= render(#sites) %>
<%= will_paginate #sites %>
Within the sites partial i have the following form hidden away
<%= simple_form_for site, remote: true do |f| %>
<%= f.input :name %>
<%= f.input :matter %>
<%= f.submit "Save", :class => "button gr thirt", id: "site_save" %>
<% end %>
instead of generating the expected HTML i get the following, linking to the show action, am I missing something fundamental here?
<form accept-charset="UTF-8" action="/sites/1" class="simple_form edit_site" data-remote="true" method="post" novalidate="novalidate">
</form>
I was looping through a collection of #sites, a results returned by a call to Site.all
so the object being served to the above form is one of the |site|'s contained within #sites
If you serve a form_for form with a an object retrieved from the database or a 'new record' object like Site.new, it will automatically differentiate and modify the route etc accordingly between the create and the update action.
The site object contained in the #sites block was not recognizable by the form_for. So a quick re factor to request an edit from via ajax, and provide the form with the instance variable created by the edit action (#site = Site.find(params[:id]) ) was recognizable by the form_for helper and meant that the submit action, accordingly adjusted to the correct route.
I want to reload a part of a form generated with the form_for-helper via AJAX.
After reloading the part I still want to have access to the form object.
How can I do this?
Best regards
I am not sure if your are using different terminology than I've heard, but what do you mean "still want to have access to the form object"?
Do you mean access to it in JavaScript? That should still work as long as you don't overwrite the form tags.
Do you mean in the html.erb code generating the partial? That doesn't really make sense, because that form_for object has already generated its html tags and gone out of scope. You need to use to the regular form of the helpers that takes the name of the object as the first parameter. There is no problem with this working with the tags generated by the form_for version of the helpers.
So, in your main page:
<%= form_for :person, #person, :url => { :action => "create" } do |f| %>
<%= f.text_field :name %>
<div id="reloadable">
</div>
<% end %>
And in your partial that fills that div:
<%= text_field :person, :name %>
No step 3.