using an instance of appointment in the appointment model - ruby-on-rails

I've created a form with some radio buttons and had to interpolate the appointment.id into the radio button and the label. However, Rails is throwing the error:
undefined method cancel_3 for Appointment.
So I have tried to make a method in the Appointment model to solve this by using define_method (see below). However as this is in the model, I'm unable to use the instance of appointment here.
Is there any way I can make this work?
define_method "cancel_#{appointment.id}" do
# ...
end
<%= f.radio_button "cancel_#{appointment.id}", :true %>
<%= f.label "cancel_#{appointment.id}_true", "Yes", class: "modal-options cancel" %>
<%= simple_form_for :appointment, url: delete_admin_appointment_path(appointment) do |f| %>

update your form with
<%= simple_form_for #appointment, url: delete_admin_appointment_path(#appointment) do |f| %>
and in your controller
def new
#apointment = Apointment.new
end
and radio button with
<%= f.radio_button "cancel_#{#appointment.id}", :true %>
<%= f.label "cancel_#{#appointment.id}_true", "Yes", class: "modal-options cancel" %>

Related

how to get session variable from params in rails (undefined method '*' for nil class

In the first controller, I set my session variables:
def show
#item = Item.find(params[:id])
session[:item_id] = #item.id
session[:amount] = params[:amount]
end
My view sets the amount with a form_tag:
<%= form_tag checkout_transaction_path, method: :get do %>
<%= label_tag :amount %>
<%= text_field :amount, placeholder: "Total bid amount", autofocus: true %>
<%= submit_tag "submit" %>
<% end %>
The parameters that get sent with this form look like this:
Parameters: {"utf8"=>"✓", "amount"=>{"{:placeholder=>\"Total bid amount\", :autofocus=>true}"=>"1111"}, "commit"=>"Confirm offer", "id"=>"1"}
The second controller tries to assign the session variable to an instance variable.
def checkout
#item = session[:item]
#amount = session[:amount]
end
However, only #item is working. I try to multiply #amount * 0.10 but get this error: undefined method '*' for nil:NilClass
What causes that error? The submit params says it's being submitted, but maybe something is up with the way I try to retrieve it? session[:item_id] goes through perfectly.
You have issue here:
Parameters: {"utf8"=>"✓", "amount"=>{"{:placeholder=>\"Total bid amount\", :autofocus=>true}"=>"1111"}, "commit"=>"Confirm offer", "id"=>"sam-lipp-abandonment"}
In your parameters, you send amount parameters but in amount, you send HTML form "amount"=>{"{:placeholder=>\"Total bid amount\", :autofocus=>true}"=>"1111"}, but should send only value what you set in the amount form, and your amount parameters should looks like this example! "amount"=>"1"
So this means that your form doesn't work correctly!
Try please replace your form on this one, and in a controller, you will get amount!
<%= form_for #new_item, url: checkout_transaction_path do |f| %>
<%= f.label :amount %>
<%= f.text_field :amount, placeholder: "Total bid amount", autofocus: true %>
<%= submit_tag "submit" %>
<% end %>
OR the same code for form_tag
<%= form_tag checkout_transaction_path do %>
<%= label_tag :amount %>
<%= text_field_tag :amount, placeholder: "Total bid amount", autofocus: true %>
<%= submit_tag "Submit Post" %>
<% end %>
The problem in your form can be here:
you wrote text_field instead text_field_tag
Also for form_for form, you need to add in the controller in a method with variable what you will use in your form, for example, I'm using variable #new_item where from you call this form, something like this
def new
#new_item = Item.new
end

Ruby on Rails, method undefined

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).

Given a Model enum, how to create a radio button group

I have a model like:
class User < ActiveRecord::Base
enum :status [:banned, :registered, :trial, :pending]
end
On my edit page, I want to show a list of 4 radio buttons and pre-select the radio button that is currently set for a user.
How can I do this?
<%= form_for #user do |f| %>
<%= f.collection_radio_buttons :status, User.statuses, :first, :first %>
<%= f.submit %>
<% end %>
Ref
Rails creates a class method using the pluralized attribute name when you use enum. The method returns a key value pair of strings you've defined and what integers they map to. So, you could do something like this:
<% User.status.keys.each do |status| %>
<%= f.radio_button :status, status %>
<%= f.label status.to_sym %>
<% end %>

Adding or updating record with user id

Ruby on Rails 3, I have an edit page with two select_tag submissions. One has users that have the attribute :attend = "Yes" and the other has users that have the attribute :attend = "No" or nil. I am trying to get the select_tag submissions to create or update the Certificate table for the user. As of now I know it is POSTing the user id and name. I do not know how to get the controller or model to take the user id and create or update with the :attend attribute value of "Yes" or "No". The app will create a new record with the correct user id but not with :attend since it is not in my select_tag submission.
Here is my edit:
<%= form_for #untrained do |f| %>
<p> Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(#current_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Un-Train", class: "btn btn-large btn-primary" %>
<% end %>
<%= form_for #trained do |f| %>
<p> Non-Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(#non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Trained", class: "btn btn-large btn-primary" %>
<% end %>
Here is my controller:
#trained = Certificate.new(params[:certificate])
#untrained = Certificate.update(params[:user_id])
Here is my model:
attr_accessible :attend, :pass, :user_id
belongs_to :user
validates :user_id, presence: true
How do you set the controller to create or update a new record with :attend as "Yes"?
How do you update the record with :attend as "No"?
Thank you
Have u tried in the controller after
if #trained.save
put
#trained.update_attributes(attend: "yes")
or no depending on the type u want.
Also have a look at this for the create/update method in rails.
create_or_update method in rails

Why is my one form submitting both the forms on this page?

I have two forms that I want to be submitted from new.html.erb, which is served by status_updates Controller. The init form, and the the stat form.
When I submit the stat form, everything works perfectly.
The init form has a different controller it answers to. The problem is that when I submit this form it immediately tries to find the object for the stat form within the controller for the init form.
If I go to that controller and provide the an instance of the object, it'll submit both forms.
Here's the error when I submit the init form.
undefined method `model_name' for NilClass:Class
<div id='stat'>
<%= form_for(#status_update) do |f| %>
The stat form submits fine. When the init form is submitted it tries to process the stat form as well. If I provide the model #status_update within init's action controller it will submit both forms. If not, it throws the above error.
Here's the code for init form
<div id='init'>
<%= form_for(current_user, method: :put ) do |f| %>
<%= f.label :target_bf_percent %>
<%= f.text_field :target_bf_pct %>
<%= f.label :Deficit_percent %>
<%= f.text_field :deficit_pct %>
<%= f.label :activity_factor %>
<%= f.select( :activity_factor, options_for_select(
[['Pick One', nil],
['Sedentary (Desk Job)', 1.2],
['Light Activity (1-3 day a week)', 1.35],
['Moderate Activity (3-5 days a week)', 1.55],
['Very Active ( 6-7 days a week)', 1.75],
['Extremely Active (Atheletic Endurance)', 1.95]]) )
%>
<%= f.submit "Post", class:"btn btn-large btn-primary" %>
<% end %>
</div>
And the code for the stat form
<div id='stat'>
<%= form_for(#status_update) do |f| %>
<%= f.label :current_weight %>
<%= f.text_field :current_weight %>
<%= f.label :current_bf_pct %>
<%= f.text_field :current_bf_pct %>
<%= f.submit "Post", class:"btn btn-large btn-primary" %>
<% end %>
The status_update controller new action:
def new
#status_update = current_user.status_update.build if user_signed_in?
end
The users controller update action:
def update
if current_user.update_attributes!(params[:user])
flash[:success] = "Your personal settings have been saved!"
render new_status_update_path
else
flash[:error] = "Whoops! There was an error saving your personal settings. Please try again."
render new_status_update_path
end
end
Please let me know of anything I can do to improve this question.
It doesn't matter if you render one or more forms on the same page.
Your problem is caused by #status_update because it is nil, but you already know that.
Double check if it is initialized in your controller.

Resources