This might be a really basic question but how do I create default values in forms?
I'm trying to put the <%= params[:id] %> of the page in as a default hidden value in this form.
`<% form_for(#revision) do |f| %>
<%= f.label :background_title %><br />
<%= f.text_field :background_title %><%= params[:id] %>
<%= f.label :title %><br />
<%= f.text_field :title %>
<%= f.submit 'Create' %>
<% end %>`
Thanks.
The form is linked to the object you pass to form_for, so set the value on the object before you start the form. For example, in the controller:
#revision.id = params[:id]
then in the form:
<%= f.hidden_field :id %>
However, I hope this is an example and you're not actually setting the ID (primary key) of an object based on a URL parameter...
If you are trying to create a new object, you can set the default values when you first instantiate a new object
def new
#revision = Revision.new(
:background_title => "Some Background Title",
:title => "Some Title"
)
end
then automatically the value of the fields will be set accordingly =) it's just that simple ;-)
Related
I have a form created using form_with. What I need is to retain the values submitted using that form after page reload. I am able to save the value of text_field but not the value of check_box. What should I change in my code so that I can achieve the same?
html.erb
<%= form_with url: search_path,
id: :search_by_filter,
method: :get, local: true do |f| %>
<div>
<p><strong>Search by Name</strong></p>
<%= f.label 'Name' %>
<%= f.text_field :name, value: params[:name] %>
</div>
<br>
<div>
<%= label_tag do %>
<%= f.check_box :only_students, checked: params[:only_students] %>
Show only students
<% end %>
</div>
<br/>
<div class="submit_button">
<%= f.submit :Search %>
</div>
<% end %>
controller.rb
def get_desired_people(params)
people = Person.includes(:country, :state, :university).order(id: :desc)
people = people.where(is_student: params[:only_students]) if params[:only_students]
people = people.where(name: params[:name]) if params[:name].present?
people
end
Here I am able to retain the value of params[:name] but not the value of params[:only_students]. It always remains unchecked after form submission. How can I retain the checked and unchecked value?
f.check_box check_box_tag is expecting checked to by boolean value, and every param is a string (string is always evaluated to true if exists) so you should do:
checked: params[:only_students].present?
you don't have to worry about a value of param, as unchecked params are not send while posting.
EDIT:
above works for check_box_tag.
f.check_box is tricky, you should carefully read description: https://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box
The behaviour you described seems pretty correct, you can deal with it or switch to check_box_tag as a better option when not updating model attributes
All the solutions above did not work for me. Try this:
<%= check_box_tag :only_students, true, params[:only_students] %>
I'm trying to build a form that has an input for a question, and five inputs, each of which represents an answer choice.
My problem is that in my /show.html.erb file, it throws an error which I can't figure out.
For instance:
QUESTION
ANS1
ANS2
ANS3
ANS4
ANS5
Here's what I have so far:
# app/views/mcqs/new.html.erb
<%= form_for :mcq, url: mcqs_path do |f| %>
<%= f.label :title %><br>
<%= f.text_field :q %>
<%= f.label :ans1 %><br>
<%= f.text_field :ans1 %>
<%= f.label :ans2 %><br>
<%= f.text_field :ans2 %>
<%= f.label :ans3 %><br>
<%= f.text_field :ans3 %>
<%= f.label :ans4 %><br>
<%= f.text_field :ans4 %>
<%= f.label :ans5 %><br>
<%= f.text_field :ans5 %>
<%= f.label :category_id %><br>
<%= f.number_field :category_id %>
<%= f.label :tags %><br>
<%= collection_check_boxes(:mcq, :tag_ids, Tag.all, :id, :name) %>
<%= f.submit %>
<% end %>
The controller:
# app/controllers/mcqs_controller.rb
class McqsController < ApplicationController
def new
#Mcq = Mcq.new
end
def index
#questions = Mcq.all
end
def show
#Mcq = Mcq.find(params[:id])
end
def create
#Mcq = Mcq.new(params[:mcqs])
# #Mcq.save returns a boolean indicating whether the article was saved or not.
if #Mcq.save
redirect_to #Mcq
else
render 'new'
end
end
end
The view:
# app/news/mcqs/show.html.erb
<strong>MCQ Title:</strong><br>
<%= mcq.q %>
<strong>Question Text:</strong><Br>
<%= mcq.ans1 %>
...<strong>MCQ Title:</strong><br>
<%= mcq.q %>
<strong>Question Text:</strong><Br>
<%= mcq.ans1 %>
...
The error:
NameError in Mcqs#show
Showing app/views/mcqs/show.html.erb where line #4 raised:
undefined local variable or method `mcq' for #<#
<Class:0x007fad811936e8>:0x007fad838a47a0>
<strong>MCQ Title:</strong><br>
<%= mcq.q %>
</p><p>
How can I display the input of the new.html.erb on the show.html.erb without this error?. <%= #mcq.q %> doesn't work.
In your show method you create #Mcq as Mcq.find(params[:id]), but then in your show view you want to access to it as mcq, so you need to access it the same way (name) as you declared it within the controller.
Try with:
<strong>MCQ Title:</strong><br>
<%= #Mcq.q %>
If you create a #mcq on the controller (show method), and then you want to access to it in the view which responds to that method using #Mcq, then you will receive an object with NilClass, that's to say, if the names don't match, they won't work.
Also if you use #mcq on the controller, and then you want to access as mcq, that won't work neither, the one on your controller is an instance variable, available to be used within your views coming from your controllers, the second one is a local variable, and most probably it'll raise an undefined local variable or method 'variable' error.
I can quote to #Anhubaw with:
The main difference between local and instance variable is that local
variable is only available in controller, where as instance variable
is available in corresponding views also. The controller and views do
not share local variables
Change all #Mcq in controller to #mcp, and change all mcq in view to #mcq, also change :mcq in new view to #mcq.
The variable prefix with # means it is an instance variable, it can be accessible in the view, while the normal variable doesn't start with # is just a local variable, it just can be used in the controller action method.
I would really like to add a form field "invite_code" to the user sign up form, but I don't know how to add the invite_code to the controller so that the application knows how to look for it?
The form in the sign up on the template would read:
<% form_for User.new do |f| %>
<span>Email:</span> <% f.email %><br>
<span>Name:</span> <% f.name %><br>
<span>Invite Code:</span> <% f.invite_code %><br>
<% end %>
The "invite_code" isn't part of the database or anything, but in the user registration model, I want to put a:
before_save :invite_promo
def invite_promo
if #invite_code.present? && #invite_code == "special_person"
self.special_key = true
end
end
Is there an easy way to look for form fields in the template using the model or controller?
So sorry...I'm new to Rails. Thank you so much in advance!
You need to define a virtual attribute invite_code in User model:
attr_accessor :invite_code
Your form should look as follows:
<%= form_for User.new do |f| %>
<span>Email:</span> <%= f.email_field :email %><br>
<span>Name:</span> <%= f.text_field :name %><br>
<span>Invite Code:</span> <%= f.text_field :invite_code %><br>
<% end %>
if you don't want to create a field inside your table, you can use hidden field in view like, <%= hidden_field_tag :invite_code, 'code' %>
I'm building a page that contains a html table, where the user can choose the name in the table header and the content in the table row.
The user will enter the information to a form, which will be parsed into a hash and saved into the DB.
Here is an example where :key will be a key of the hash (table header text) and :value will be a value in the hash (table column content).
<%= form_for([#page]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :key1 %>
<%= f.text_field :value1 %>
<%= f.text_field :key2 %>
<%= f.text_field :value2 %>
<%= f.text_field :key3 %>
<%= f.text_field :value3 %>
<%= f.text_field :key4 %>
<%= f.text_field :value4 %>
<%= f.submit "Save" %>
<% end %>
I could make a new DB table with a has_many association to the page DB and have the info stored in that table. Then that data can be rebuilt into a hash when I need to render the view.
But if I could save the entire hash into a column in the page DB, then it would save me from having to rebuild the hash every time someone visits the page.
I'm struggling to come up with how to do this in form_for.
The first issue is that it requires key and value columns in the page table. A simple fix would be change all inputs into <%= f.text_field :html_table %> (for html_table column in the page table). But then it won't be able to distinguish between a key and a value in the hash. And I also don't have any way of telling it to put the info together into a hash that is then stored into the page table's html_table column.
Not sure if this is even a possible thing but it doesn't hurt to ask before I go ahead with building the has_many association table and setting it up that way.
I'm rather confused about your question.
Can't you build a hash like this in your controller action?
html_table = {
params[:key1] => params[:value1],
params[:key2] => params[:value2],
params[:key3] => params[:value3],
params[:key4] => params[:value4]
}
Then save it to #page.html_table attribute.
EDIT:
In this case, the form is not about attirbutes of a particular object. So instead of using form_for (Link), you should be using form_tag (Link).
Example:
<%= form_tag "/pages_controller/build_html_table" do %>
<%= text_field_tag "key1" %>
<%= text_field_tag "value1" %>
<%= submit_tag "Save" %>
<% end %>
I have what I hope to be a simple question. I need to display the value for an attribute on the Edit page, while keeping the input field for the same attribute. How might this be accomplished?
Well generally you can just use the original object, like you'll have an #foo that you'll have used in your form_for statement, so you can just use that directly: = #foo.the_attribute
If you're within a partial, or elsewhere where you have only the form builder instance, then you can refer to the underlying object with the .object method, eg.:
= form_for #foo do |f|
# in here, f.object == #foo
In my case, I'm working with accepts_nested_attributes_for in two models. Event accept nested objects from Speaker. And Speaker has a perfil_id attribute which could be ['Maker', 'Developer', 'Entrepreneur', ...].
The Speaker's form is a partial rendered from the principal form, Event's form:
<%= form_for(event) do |f| %>
<%= f.text_field :title %>
<%= f.label :title, 'Event name' %>
<%= f.fields_for :speakers do |builder| %>
<%= render 'events/partials/speaker_fields', f: builder %>
<% end %>
<%= f.submit %>
<% end %>
Partial
<%= builder.number_field :name %>
<%= builder.label :name %>
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.member_profile_id ) %>
<%= select_tag "event[speakers_attributes][profile_id]", options, prompt: 'Select a Profile' %>
When editing Event's Speakers I wanted a select_tag to select the profile name for the actual Speaker.
I could not use an input field for this. So I need to get the correct values from the builder object and I get what I need by doing this:
f.object.profile_id
Passing it as a fourth param to the select options I get this working:
<% options = options_from_collection_for_select(#profiles, 'id', 'name', f.object.profile_id ) %>
I hope it could be useful for you too!