I'm writing a CMS and I faced a problem while making a role management for users. I have a boolean field :admin in my User model, and I've made a checkbox in my form to set created user as an administrator. Here is the users_controller:
def create
#user = User.create(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to users_path }
format.json { head :no_content }
else
format.html { render :new }
format.json { render #user.errors, status: :unprocessable_entity }
end
end
end
def edit
end
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to users_path }
format.json { head :no_content }
else
format.html { render :edit }
format.json { render #user.errors, status: :unprocessable_entity }
end
end
end
and this is my form :
<%= form_for #user do |f| %>
# Here go fields for username, email and password
<p>
<%= f.label "Set as administrator" %> <br />
<%= f.hidden_field :admin, '' %>
# I also tried with <%= f.hidden_field :admin, false %>
<%= f.check_box :admin, checked = true %>
# Or <%= f.check_box :admin, data: { switch: true } %>
</p>
<% end %>
But any of these options returns me the following:
NoMethodError in Multiflora::Users#edit
undefined method `merge' for "":String
What have I done wrong?
Take hidden field as
<%= f.hidden_field :admin, value: '' %>, or simply
<%= f.hidden_field :admin %>
and checkbox as <%= f.check_box :admin, :checked => true %>
<%= form_for #user do |f| %>
# Here go fields for username, email and password
<p>
<%= f.label "Set as administrator" %> <br />
<%= f.hidden_field :admin, value: '' %>/<%= f.hidden_field :admin %>
<%= f.check_box :admin, :checked => true %>
</p>
<% end %>
Related
I am working on authentication part. Also, I am new to rails view part.
Here, I am trying to signup with required field but I am getting error message which is due to association between user and emergency contact. Can anyone suggest how to resolve it? Thanks in advance.
user.rb
class User < ApplicationRecord
has_one :emergency_contact, foreign_key: 'user_id'
end
emergency_contact.rb
class EmergencyContact < ApplicationRecord
belongs_to :user
end
registrations/new.html.erb
<h2>Sign up form</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<h3>Emergency Contact</h3>
<div>
<%= f.fields_for :emergency_contact, EmergencyContact.new do |emergency_contact| %>
<div>
<%= emergency_contact.label :full_name %> <br />
<%= emergency_contact.text_field :er_full_name %> <br />
</div>
<% end %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
users_controller.rb
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
#users = User.all
end
def show
end
def new
#user = User.new
end
def edit
end
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: #user }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #user.update(user_params)
format.html { redirect_to #user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: #user }
else
format.html { render :edit }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
def destroy
#user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_user
#user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:email, :password,emergency_contact_attributes: [:er_full_name] )
end
end
Error message:
1 error prohibited this user from being saved:
Emergency contact user must exist
controller:
def new
#user = User.new
#emergency_contact = #user.build_emergency_contact
end
view:
<%= form.fields_for #emergency_contact do |emergency_contact_fields| %>
<div>
<%= emergency_contact_fields.label :full_name %> <br />
<%= emergency_contact_fields.text_field :er_full_name %> <br />
</div>
<% end %>
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.
I'm new to rails and I'm trying to create a form in two parts. First, you fill in the user form. Then you choose to become a prestataire or a employeur. And by clicking on the respective button, the user form will be saved, and you will be redirected either to the prestataire's form or the employeur's with the recently created user params.
Until now, I didn't manage to send the recently created user params, only saving and redirecting. But then I get this error in the prestataire_controller, since prestataire belong to user. The error is similar when I click on the employeur button:
undefined local variable or method 'user' for #<UsersController:0x0000000570eef0>
format.json { render action: 'show', status: :created, location: #user }
else
format.html { redirect_to new_user_prestataire_path(user_id: user), notice: "Renseignez vos informations de prestataire" } #This is sublined
format.json { render action: 'show', status: :created, location: #user }
end
Here is the code I've written so far:
User Form:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :civility, 'Titre de civilité: ' %><br>
<%= f.text_field :civility %>
</div>
<div class="field">
<%= f.label :forename, 'Prénom: ' %><br>
<%= f.text_field :forename %>
</div>
<div class="field">
<%= f.label :surname, 'Nom de famille: ' %><br>
<%= f.text_field :surname %>
</div>
<div class="field">
<%= f.label :email, 'Email: ' %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password, 'Mot de passe: ' %><br>
<%= f.password_field :password, size: 40 %>
</div>
<div class="field">
<%= f.label :password_confirmation, 'Confirmation de mot de passe: ' %><br>
<%= f.password_field :password_confirmation, size: 40 %>
</div>
<div class="field">
<%= f.label :phone, 'Numéro de téléphone: ' %><br>
<%= f.text_field :phone %>
</div>
<div class="actions">
<%= f.submit 'Employeur', name: 'employeur' %>
<%= f.submit 'Prestataire', name: 'prestataire' %>
</div>
<% end %>
User controller:
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
if params[:commit] == 'employeur'
format.html { redirect_to new_user_employeur_path, notice: "Renseignez vos informations d'employeur" }
format.json { render action: 'show', status: :created, location: #user }
else
format.html { redirect_to new_user_prestataire_path, notice: "Renseignez vos informations de prestataire" }
format.json { render action: 'show', status: :created, location: #user }
end
else
format.html { render action: 'new' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if #user.update(user_params)
if params[:commit] == 'employeur'
format.html { redirect_to new_user_employeur_path, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { redirect_to new_user_prestataire_path, notice: "User was successfully updated." }
format.json { head :no_content }
end
else
format.html { render action: 'edit' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :surname, :forename, :civility, :phone, :employeur)
end
User model:
class User < ActiveRecord::Base
has_one :prestataire
has_one :employeur
validates :email, presence: true, uniqueness: true
validates :password, :forename, :surname, :phone, :civility, presence: true
validates :password, confirmation: true
has_secure_password
end
Routes file:
Workplace::Application.routes.draw do
resources :users do
resources :prestataires
resources :employeurs
end
resources :projets do
resources :feedbacks
resources :offres
end
root 'projets#index'
I was thinking about adding an onclik option on the f.submit and adding an adequate method. But I read somewhere that f.submit was not the best option for this kind of request, but no other suggestion was made. Thanks in advance for your help.
As I said,the error is because it should be #user in this line of your create action
format.html { redirect_to new_user_prestataire_path(user_id: #user), notice: "Renseignez vos informations de prestataire" }
And also,you haven't initialized #user in your update action which may rise in another error in future.Add this line at the beginning of your update action
#user = User.find(params[:id])
You are not defining your #user object on update. Try:
def update
#user = User.find(params[:id])
respond_to do |format|
if #user.update(user_params)
if params[:commit] == 'employeur'
format.html { redirect_to new_user_employeur_path, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { redirect_to new_user_prestataire_path, notice: "User was successfully updated." }
format.json { head :no_content }
end
else
format.html { render action: 'edit' }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
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.
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?