Ruby on Rails, method undefined - ruby-on-rails

I'm very new to ruby on rails. I'm trying to make a text field to assign one of my variables (end_date), but I keep getting this error:
undefined method `end_date' for #<Quiz:0x007fccd1e0f9c0>
Here's my code:
<%# Main Canvas where cardes places %>
<div class="column large-11" id="main">
<%= form_for #quiz do |q| %>
<%= q.label :quiz_name %>
<%= q.text_field :quiz_name %>
<%= q.label :end_date %>
<%= q.text_field :end_date %>
<%= hidden_field_tag 'selected', 'none' %>
<%= q.hidden_field :classroom_id, value: #classroom_id%>
<%= q.submit "Create Quiz", class: "expanded button" %>
<% end %>
<%= form_tag("/quiz/#{#classroom_id}/copy", method: "get") do %>
<%= label :id, "ID" %>
<%= text_field_tag "id", "" %>
<%= submit_tag "Copy Quiz By ID", class: "expanded button" %>
<% end %>
</div>

Let me break down how these different pieces relate to one-another, which hopefully will make this easier for you to troubleshoot.
<%= form_for #quiz do |q| %>
Here you are invoking form_for to create a form bound to the #quiz object. It yields a form builder object as the argument q.
<%= q.text_field :quiz_name %>
Here you are calling the text_field method on the form builder with the field named quiz_name. This means it will generate a text input, and call the quiz_name method on #quiz to find the current value.
So given that background, it should be clear why you are seeing this error:
<%= q.text_field :end_date %>
You are telling the form builder to call #quiz.end_date for the value of this field, but that method does not exist.
You have not given enough code samples for us to determine why you expect this method to exist. Perhaps this is a field you've added to the quizzes table, but haven't yet run the migration? Is this supposed to be a virtual attribute on Quiz? Or perhaps you just want to send a field that isn't connected to the Quiz model inside this form. (You can do that with a separate set of helpers, in this case text_field_tag, that give you more flexibility in where the data comes from).

Related

How to create a simple form without a model?

How would I go about creating a form that takes what user input as a value and just passes it to the controller without being connected to any model?
Something simple like i.e. calculating tax based on input salary, or other calculation like that, when I show the user a form, let them fill it, and when submitting it would go to the results
<%= form_with url: 'calculator#result' do |form| %>
<%= form.number_field :value, in: 1000.0..20000.0, step: 0.5 %>
<%= form.submit %>
<% end %>
i expected something like this to pass 'value' and redirect to calculator#result when submitting, but the button doesn't really do anything. whereas a form connected to a model seems pretty smart and does it
The form_tag Helper method is usually used for forms that are not linked to a model.
I think this should work:
<%= form_tag("/calculator/result", :method => "get") do %>
<%= label_tag(:value, "Value:") %>
<%= text_field_tag(:value) %>
<%= submit_tag("Submit") %>
<% end %>

Rails simple form doen't pass params from input

I want to display a list of InvestorTypes (as a radio button) but before each type I should be able to add an explanation of that type. Here is what I've got:
<%= simple_form_for(resource, as: resource_name, url: users_user_experience_level_path(resource_name), html: { method: :put }) do |f| %>
<div class="form-inputs">
<% User::USER_EXPERIENCE_LEVEL.each do |level| %>
<b>Investor type <%= level %></b>
<%= t("user.description.#{level}") %>
<%= f.input :experience_level, collection: [level], as: :radio_buttons, label: false %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit, 'Submit' %>
</div>
<% end %>
Which gives me expected view:
Investor type Beginner
Some explanation of what is going on
[checkobox] type Beginner
Investor type Expert
Some clarification of who is an expert and what ever you want to display here
[checkbox] type Expert
Investor type Institutional
Some clarification of who is an institutional client and some legal stuff
[checkbox] type Institutional
But when Submit button is pressed it doesn't pass input value (radio box selection which user chose) into the params:
=> #<ActionController::Parameters {"experience_level"=>""} permitted: true>
[EDIT]
class User < ApplicationRecord
USER_EXPERIENCE_LEVEL = %w[institutional beginner expert].freeze
end
It looks to me like you're using simple form wrong. The "collection" input in Simple Form is expecting to get an entire list of options, not just one option.
Looping in the way you're doing it is creating one group for each experience level, and each group only has one button in it. So it might visually look correct, but it's not functioning the way you intended. Instead you want to create one group of radio buttons for experience level such that each button changes the value of experience level.
Because you're doing this with significant customization around the appearance, it's probably not a good use of Simple Form, and instead you should fall back to the normal Rails form helpers.
You want to pass a block to f.input to get the simple form wrapper and then use the lower level rails helpers:
<%= simple_form_for(resource, as: resource_name, url: users_user_experience_level_path(resource_name), html: { method: :put }) do |f| %>
<div class="form-inputs">
# adds the simple form wrapper
<%= f.input :experience_level do %>
# iterates across the options and yields a input builder to each iteration
<%= f.collection_checkboxes(:experience_level, User::USER_EXPERIENCE_LEVEL, :value_method, :label_method) do |cb| %>
# There are three special methods available:
# object, text and value,
# which are the current item being rendered, its text and value methods, respectively.
<%= t("user.description.#{cb.text}") %>
<%= cb.check_box %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit, 'Submit' %>
</div>
<% end %>
If you don't actually have a model you can use #itself to iterate across a flat array:
<%= f.collection_checkboxes(:experience_level, User::USER_EXPERIENCE_LEVEL, :itself, :itself) do |cb| %>
Or an array of pairs:
<%= f.collection_checkboxes(:experience_level, User::USER_EXPERIENCE_LEVEL.zip(User::USER_EXPERIENCE_LEVEL), :first, :last) do |cb| %>
Or even a struct.

Get data attribute value from the Rails form builder without using input field

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!

How to send one or the other attribute in the form

I have a form for which I am using checkboxes (Not using radio buttons for my purpose). The problem I run into is when I submit a form, I get an error saying params is missing or value is empty:checkup. I am trying to use hidden filed but get the same error. HOw to have an option of sending only one if selected?
def checkup_params
params.require(:checkup).permit(:eye, :heart)
end
my form:
<%= form_for(#checkup) do |f| %>
<%= hidden_field_tag "checkup[eye]", nil %>
<%= check_box_tag :eye, "eye" %>
<%= hidden_field_tag "checkup[heart]", nil %>
<%= check_box_tag :heart, "heart" %>
<%= f.submit %>
<% end %>
I suggest reading this guide http://guides.rubyonrails.org/form_helpers.html#dealing-with-model-objects
Most specifically section 2.2. You really shouldn't be using tag helpers here.

Passing variable from view to controller Rails 4

I want to pass one variable which is introduced by the user in the front-end. I dont use any model (i dont need it, because im working with JSON data all in memory). I have looked many tutoriasl but almost all of them are focused on filling out a form. My application does not have any form nor tables. Any idea?
Thank you.
{<div class="module1">
<p>Mein Lastprofil berechnen</p>
<div class="boxed">
Jahreshausverbrauch (kWh)
<%= text_field_tag "input", nil, placeholder: "3500" %>
<%= button_to "Senden", root_path, :method => :get %>
</div>
</div>}
I want to save/pass the variable introduced when clickling the button.
By having a text field you implicitly already have a form.
Make it explicit by wrapping the text field and button:
<%= form_tag root_path, method: :get do %>
<%= text_field_tag :input, placeholder: "3500" %>
<%= submit_tag "Senden" %>
<% end %>
Then you can access the value as params[:input] in the controller.

Resources