Ruby on rails, radio button if - ruby-on-rails

I'm a little green with ruby but wanted to try something with it, so I want to ask you for help. I have a problem with radio buttons. I have succesfully created a radio buttons, like this:
<input id="user_options_true" name="user[options]" type="radio" value="true" />
<label class="collection_radio_buttons" for="user_options_true">Tak</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio_buttons" for="user_options_false">Nie</label>
But now I would like to do if statement on the other page that woudl work like this: if value true, Write text, if false, write different text. I tried doing it like this :
<% if user[options] == true %>
<b>Status:Ma doktora </b>
<% if user[options] == false %>
<b>Status:Nie ma doktora</b>
But I get error like this:
F:/p15/app/views/pacjencis/show.html.erb:47: syntax error, unexpected keyword_ensure, expecting keyword_end
F:/p15/app/views/pacjencis/show.html.erb:49: syntax error, unexpected $end, expecting keyword_end
My view:
<%= simple_form_for(#pacjenci, html: {class: "form-horizontal"}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :last_name %>
<%= f.input :adres %>
<%= f.input :pesel %>
<%= f.input :telefon %>
<%= f.input :email %>
Czy lekarz przyjmowaƂ
<input id="user_options_true" name="user[options]" type="radio" value="true" />
<label class="collection_radio_buttons" for="user_options_true">Tak</label>
<input id="user_options_false" name="user[options]" type="radio" value="false" />
<label class="collection_radio_buttons" for="user_options_false">Nie</label>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
My controler:
> class PacjencisController < ApplicationController
# GET /pacjencis
# GET /pacjencis.json
def index
#pacjencis = Pacjenci.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #pacjencis }
end
end
# GET /pacjencis/1
# GET /pacjencis/1.json
def show
#pacjenci = Pacjenci.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #pacjenci }
end
end
# GET /pacjencis/new
# GET /pacjencis/new.json
def new
#pacjenci = Pacjenci.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #pacjenci }
end
end
# GET /pacjencis/1/edit
def edit
#pacjenci = Pacjenci.find(params[:id])
end
# POST /pacjencis
# POST /pacjencis.json
def create
#pacjenci = Pacjenci.new(params[:pacjenci])
respond_to do |format|
if #pacjenci.save
format.html { redirect_to #pacjenci, notice: 'Pacjenci was successfully created.' }
format.json { render json: #pacjenci, status: :created, location: #pacjenci }
else
format.html { render action: "new" }
format.json { render json: #pacjenci.errors, status: :unprocessable_entity }
end
end
end
# PUT /pacjencis/1
# PUT /pacjencis/1.json
def update
#pacjenci = Pacjenci.find(params[:id])
respond_to do |format|
if #pacjenci.update_attributes(params[:pacjenci])
format.html { redirect_to #pacjenci, notice: 'Pacjenci was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #pacjenci.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pacjencis/1
# DELETE /pacjencis/1.json
def destroy
#pacjenci = Pacjenci.find(params[:id])
#pacjenci.destroy
respond_to do |format|
format.html { redirect_to pacjencis_url }
format.json { head :no_content }
end
end
end
Would really apreciate help.Thanks in advance.

<% if params[:user][:options] == 'true' %>
<b>Status:Ma doktora </b>
<% else %>
<b>Status:Nie ma doktora</b>
<% end %>
I'd recommend using <strong> tags instead of <b>.
For true or false values you'd usually use <% if params[:user][:options]? %> but since this is an actual string value and not a boolean, the answer above should work.
The params part is how you access POST and GET data passed from your forms.

You need to add elsif and end like below:
<% if user[options] == true %>
<b>Status:Ma doktora </b>
<% elsif user[options] == false %>
<b>Status:Nie ma doktora</b>
<% end %>

<b><%= params[:user][:options]? ? 'Status:Ma doktora' : 'Status:Nie ma doktora' %></b>
This solution uses a ternary operator: See How do I use the conditional operator (? :) in Ruby?

Related

Rails Remote True Ajax ActionController::UnknownFormat

I have a mini-update method in my model that only updates one field, set up in a chart (so I see the form for each row). It works properly, but want it to update using AJAX to be able to do multiple updates quickly without waiting for a full page reload.
The cell looks like this:
<td style="min-width: 150px" id="goody_cell<%= blog.id %>">
<%= render partial: "admin/goody_cell", locals: { blog: blog } %>
</td>
With this as the partial:
<% if blog.variation_id %>
<%= Variation.find(blog.variation_id).name %>
<% end %>
<%= simple_form_for(blog, url: update_goody_path(blog), remote: true, method: :patch, authenticity_token: true) do |f| %>
<% variation_collection = [] %>
<% Variation.all.each do |lm| %>
<% variation_collection << ["#{lm.name}", "#{lm.id}"] %>
<% end %>
<div class="row text-left">
<div class="form-group col-12 mb-0 pb-0">
<%= f.input :variation_id, label: false, prompt: "Choose a Related Goody", input_html: { id: "lmFor" + blog.id.to_s, class: 'browser-default', onchange: "this.form.submit();" }, collection: variation_collection, required: false %>
</div>
</div> <!-- row -->
<% end %>
My blogs/update_goody.js.erb looks like this:
$('#goody_cell<%= blog.id %>').html('<%= j render partial: "admin/goody_cell", locals: { blog: #blog } %>');
And my controller method is like this:
def update_goody
#blog = Blog.friendly.find(params[:id])
if #blog.update!(blog_params)
respond_to do |format|
format.js
end
else
format.html { render :edit }
format.json { render json: #blog.errors, status: :unprocessable_entity }
end
end
I have the route like this:
patch "blogs/:id/update_goody" => "blogs#update_goody", as: "update_goody"
When I try to update it, it does update the value, but before it renders the partial, I get an error saying:
ActionController::UnknownFormat - ActionController::UnknownFormat:
app/controllers/blogs_controller.rb:178:in `update_goody'
I've looked at SO posts like this but they all say that the error was fixed by adding remote: true, which I already have.
Can anyone help me get this working?
instead of remote: true, use local: false (Rails 6)
make sure you have both update_goody.html.erb and update_goody.js.erb in your views/blogs folder.
you dont have to add anything to update_goody.html.erb as its not rendering at all.
in your controller
def update_goody
#blog = Blog.friendly.find(params[:id])
if #blog.update!(blog_params)
respond_to do |format|
format.html
format.js
end
else
format.html { render :edit }
format.json { render json: #blog.errors, status: :unprocessable_entity }
end
end
note that format.html in respond_to block. it dosent matter you add it or not as you are set remote: true in your form
refer this
In my case i had to put redirect_to to render html
respond_to do |format|
format.js
format.html { redirect_to update_goody_path }
end

Remove Avatar isn't working for me

The Remove Avatar checkbox isn't doing anything for me on my edit form. Here is my code.
_form.html.erb
<%= simple_form_for(#child, html: { multipart: true}) do |f| %>
<% if child.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(child.errors.count, "error") %> prohibited this child from being saved:</h2>
<ul>
<% child.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :name %><br />
<div class="field"><%= f.text_field(:name) %> <br /></div>
<%= f.label :balance %><br />
<div class="field"> <%= f.text_field(:balance) %> <br /></div>
<%= f.label :parent_id %><b> ID</b><br />
<%= f.number_field :parent_id, :value => current_parent.id, :readonly => true %><br />
<%=f.label :avatar %><br />
<%= image_tag(#child.avatar_url) if #child.avatar? %>
<%= f.file_field(:avatar) %>
<%= f.hidden_field(:avatar_cache) %>
<br />
<label>
<%= f.check_box :remove_avatar %>
Remove Avatar
</label>
<div class="actions">
<% if #child.new_record? == true %>
<%= f.submit("Add Child") %>
<% else %>
<%= f.submit("Save Child") %>
<% end %>
</div>
<% end %>
Child Controller
class ChildrenController < ApplicationController
before_filter :authenticate_parent!
before_action :set_child, only: [:show, :edit, :update, :destroy]
# GET /children
# GET /children.json
def index
#children = Child.all
end
# GET /children/1
# GET /children/1.json
def show
end
# GET /children/new
def new
#child = Child.new
end
# GET /children/1/edit
def edit
end
# POST /children
# POST /children.json
def create
#child = Child.new(child_params)
if #child.avatar.file.nil?
img = LetterAvatar.generate(#child.name, 200)
File.open(img) do |f|
#child.avatar = f
end
end
respond_to do |format|
if #child.save
format.html { redirect_to #child, notice: 'Child was successfully created.' }
format.json { render :show, status: :created, location: #child }
else
format.html { render :new }
format.json { render json: #child.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /children/1
# PATCH/PUT /children/1.json
def update
if #child.avatar.file.nil?
img = LetterAvatar.generate(#child.name, 200)
File.open(img) do |f|
#child.avatar = f
end
end
respond_to do |format|
if #child.update(child_params)
format.html { redirect_to #child, notice: 'Child was successfully updated.' }
format.json { render :show, status: :ok, location: #child }
else
format.html { render :edit }
format.json { render json: #child.errors, status: :unprocessable_entity }
end
end
end
# DELETE /children/1
# DELETE /children/1.json
def destroy
#child.destroy
respond_to do |format|
format.html { redirect_to children_url, notice: 'Child was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_child
#child = Child.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def child_params
params.fetch(:child, {}).permit(:name, :balance, :parent_id, :avatar)
# params.fetch(:user, {}).permit(:first_name, :last_name, :email, :phone,
end
end
If I click save with the Remove Avatar box checked, it literally does nothing to the avatar, if I go back to any page that displayed the avatar, it is still there. What am I doing wrong? I am using Ruby on Rails 5.0.1
Side note: I am aware of some of the deprecated things that are being used here, such as before_filter. I am working with a large group of people so not all of this code is mine, fixing it is not priority at the moment.

Rails trying to save form with collection_select that is empty gives undefined method `map' for nil:NilClass

I have a form with a drop down generated from a collection_select. The collection_select starts off blank and then when the user selects a date from the date field, the collection_select is updated with values for the date that is chosen.
I'm trying to show a nice error message if the form is submitted without a value chosen in my dropdown. Currently i'm getting this error: undefined methodmap' for nil:NilClass`
How can i make it so that if the user doesn't select a value in the dropdown, I can show them a nice error message?
View
<%= form_for(#arrangement) do |f| %>
<div class="control-group">
<%= f.label :date, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :date, :class => 'input-large datepicker' %>
</div>
</div>
<div class="control-group">
<%= f.label :timeslot, :class => 'control-label' %>
<div class="controls">
<%= collection_select(:arrangement, :timeslot_id, #available_timeslots, :id, :timeslot_spelled) %>
</div>
</div>
<% end %>
Controller
# GET /arrangements/new
# GET /arrangements/new.json
def new
date = Time.now.to_date.to_s(:db)
#arrangement = Arrangement.new
#available_timeslots = Timeslot.where("location_id = ? AND date(timeslot) = ? AND arrangement_id is null", current_user.user_details.location_id, date).order('timeslot ASC')
respond_to do |format|
format.html # new.html.erb
format.json { render json: #arrangement }
end
end
# POST /arrangements
# POST /arrangements.json
def create
#arrangement = Arrangement.new(params[:arrangement])
respond_to do |format|
if #arrangement.save
# update the timslot record with the arrangement id
if #timeslot = Timeslot.update(#arrangement.timeslot_id, :arrangement_id => #arrangement.id)
format.html { redirect_to #arrangement, notice: 'Arrangement was successfully created.' }
format.json { render json: #arrangement, status: :created, location: #arrangement }
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
end
end
The error given is referring to #available_timeslots being empty when trying to save the form
I would try something like this.
def new
#available_timeslots = ...
if #available_timeslots.count > 0
flash.now[:error] = "nil errrraaarrr"
end
...
end
In view
<div class="controls">
<%- if #available_timeslots.count > 0 %>
<%= collection_select(:arrangement, :timeslot_id, #available_timeslots, :id, :timeslot_spelled) %>
<% else %>
<%= flash.now[:error] %>
<% end %>
</div>
#available_timeslots is nil. Make sure it's set with an array of available timeslots to avoid this error message.
The trick was to add #available_timeslots = [] in the else clause
def create
#arrangement = Arrangement.new(params[:arrangement])
respond_to do |format|
if #arrangement.save
# update the timslot record with the arrangement id
if #timeslot = Timeslot.update(#arrangement.timeslot_id, :arrangement_id => #arrangement.id)
format.html { redirect_to #arrangement, notice: 'Arrangement was successfully created.' }
format.json { render json: #arrangement, status: :created, location: #arrangement }
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
else
format.html { render action: "new" }
format.json { render json: #arrangement.errors, status: :unprocessable_entity }
end
end
end

undefined method `model_name' for NilClass:Class. The action 'create' could not be found for

When I go to http://localhost:3000/register_entries/new, I am getting this error: undefined method `model_name' for NilClass:Class
Below is my _checkoutform.html.erb. If I add this line <% #register_entry = RegisterEntry.new %> to the beginning of the controller then the form comes up but when I submit I get the following error The action 'create' could not be found for RegisterEntriesController:
<%= form_for (#register_entry) do |f| %>
<% if #register_entry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#register_entry.errors.count, "error") %> prohibited this register from being saved:</h2>
<ul>
<% #register_entry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :publisher %>
<%= f.collection_select :publisher_id, Publisher.order(:name), :id, :name %>
</div>
<div class="field">
<%= f.label :territory %>
<%= f.collection_select :territory_id, Territory.order(:last_worked), :id, :number %>
</div>
<div class="field">
<%= f.label "Checkout Date" %>
<%= f.date_select :checkout %>
</div>
<!-- <div class="field">
<%= f.label :checkin %><br />
<%= f.date_select :checkin %>
</div> -->
<!-- <div class="field">
<%= f.label :notes %><br />
<%= f.text_field :notes %>
</div> -->
<div class="actions">
<%= f.submit "Checkout Territory" %>
</div>
<% end %>
Here is my register_entries_controller.rb:
class RegisterEntriesController < ApplicationController
# GET /RegisterEntries
# GET /RegisterEntries.json
before_filter :authenticate_user!
helper_method :sort_column, :sort_direction
private
def sort_column
#register_entry.column_names.include?(params[:sort]) ? params[:sort] : "checkout"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
def index
authorize! :index, #user, :message => 'Not authorized'
#register_entries = RegisterEntry.order(sort_column + ' ' + sort_direction) #pluralized #register_entry
respond_to do |format|
format.html # index.html.erb
format.json { render json: #register_entries }
end
end
# GET /RegisterEntries/1
# GET /RegisterEntries/1.json
def show
#register_entry = RegisterEntry.find(params[:id])
#publishers = Publisher.all
respond_to do |format|
format.html # show.html.erb
format.json { render json: #register_entry }
end
end
# GET /RegisterEntries/new
# GET /RegisterEntries/new.json
def new
#register_entry = RegisterEntry.new
authorize! :index, #user, :message => 'Not authorized'
respond_to do |format|
format.html # new.html.erb
format.json { render json: #register_entry }
end
end
# GET /RegisterEntries/1/edit
def edit
authorize! :index, #user, :message => 'Not authorized'
#register_entry = RegisterEntry.find(params[:id])
end
# POST /RegisterEntries
# POST /RegisterEntries.json
def create
authorize! :index, #user, :message => 'Not authorized'
#register_entry = RegisterEntry.new(params[:register_entry])
respond_to do |format|
if #register_entry.save
format.html { redirect_to #register_entry, notice: 'Register Entry was successfully created.' }
format.json { render json: #register_entry, status: :created, location: #register_entry }
else
format.html { render action: "new" }
format.json { render json: #register_entry.errors, status: :unprocessable_entity }
end
end
end
# PUT /RegisterEntries/1
# PUT /RegisterEntries/1.json
def update
authorize! :index, #user, :message => 'Not authorized'
#register_entry = RegisterEntry.find(params[:id])
respond_to do |format|
if #register_entry.update_attributes(params[:register_entry])
format.html { redirect_to #register_entry, notice: 'Register Entry was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #register_entry.errors, status: :unprocessable_entity }
end
end
end
# DELETE /RegisterEntries/1
# DELETE /RegisterEntries/1.json
def destroy
authorize! :index, #user, :message => 'Not authorized'
#register_entry = RegisterEntry.find(params[:id])
#register_entry.destroy
respond_to do |format|
format.html { redirect_to register_entries_url }
format.json { head :no_content }
end
end
end
Been fighting with this for a few hours. Any ideas?
You are making all of your controller action methods private which means they can't be called as actions. See the end of the Methods and Actions section of the ActionController Rails Guide.

Rails 3.2 - Nested Resource Passing ID

Okay so my associations are:
Outlet -> has_many :monitorings
Monitoring -> belongs_to :outlet
My Routes:
resources :outlets do
resources :monitorings
end
View:
<%= link_to new_outlet_monitoring_path(#outlet) %>
When I click the link, the logs show that the outlet_id is passed as a parameter to the new page correctly.
But when saving the monitoring record, the outlet_id becomes nil.
Any help?
UPDATE:
# views/monitorings/_form.html.erb
<%= form_for(#monitoring) do |f| %>
<h2>Type of Monitoring</h2>
<fieldset data-role="controlgroup" >
<div class="radio-group">
<%= f.radio_button :mtype, "Full" %><%= f.label :mtype, "Full", value: "Full" %>
<%= f.radio_button :mtype, "Partial" %><%= f.label :mtype, "Partial", value: "Partial" %>
<%= f.radio_button :mtype, "None" %><%= f.label :mtype, "None", value: "None" %>
</div>
</fieldset>
<hr>
<%= f.submit "Next Step" %>
<% end %>
And the controller:
# controllers/monitoring_controller.rb
def new
#monitoring = Monitoring.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #monitoring }
end
end
def create
#monitoring = Monitoring.new(params[:monitoring])
respond_to do |format|
if #monitoring.save
format.html { redirect_to #monitoring, notice: 'Monitoring was successfully created.' }
format.json { render json: #monitoring, status: :created, location: #monitoring }
else
format.html { render action: "new" }
format.json { render json: #monitoring.errors, status: :unprocessable_entity }
end
end
end
This is most likely an issue with the way you are creating the new monitoring record. Can we see your form and your create controller action?

Resources