Everything is working fine in my rails project, except for one thing. I'm trying to be able to edit a boolean with check_box. But when I go to edit, whether or not the check_box is checked, the boolean stays false. Here is my relevant code:
class SubnetsController < ApplicationController
# GET /subnets
# GET /subnets.json
def index
#subnets = Subnet.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #subnets }
end
end
def normal
#subnets = Subnet.all
respond_to do |format|
format.html # normal.html.erb
format.json { render :json => #subnets }
end
end
# GET /subnets/1
# GET /subnets/1.json
def show
#subnet = Subnet.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #subnet }
end
end
# GET /subnets/new
# GET /subnets/new.json
def new
#subnet = Subnet.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #subnet }
end
end
# GET /subnets/1/edit
def edit
#subnet = Subnet.find(params[:id])
end
# POST /subnets
# POST /subnets.json
def create
#subnet = Subnet.new(params[:subnet])
respond_to do |format|
if #subnet.save
format.html { redirect_to #subnet, :notice => 'Subnet was successfully created.' }
format.json { render :json => #subnet, :status => :created, :location => #subnet }
else
format.html { render :action => "new" }
format.json { render :json => #subnet.errors, :status => :unprocessable_entity }
end
end
end
# PUT /subnets/1
# PUT /subnets/1.json
def update
#subnet = Subnet.find(params[:id])
respond_to do |format|
if #subnet.update_attributes(params[:subnet])
format.html { redirect_to #subnet, :notice => 'Subnet was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #subnet.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /subnets/1
# DELETE /subnets/1.json
def destroy
#subnet = Subnet.find(params[:id])
#subnet.destroy
respond_to do |format|
format.html { redirect_to subnets_url }
format.json { head :no_content }
end
end
end
View:
<%= form_for(#subnet) do |f| %>
<% if #subnet.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#subnet.errors.count, "error") %> prohibited this subnet from being saved:</h2>
<ul>
<% #subnet.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :SubnetName %><br />
<%= f.text_field :SubnetName %>
</div>
<div class="field">
<%= f.label :FW_Context_BridgeGroup %><br />
<%= f.number_field :FW_Context_BridgeGroup %>
</div>
<div class="field">
<%= f.label :VlanNumber %><br />
<%= f.number_field :VlanNumber %>
</div>
<div class="field">
<%= f.label :FwVlan %><br />
<%= f.number_field :FwVlan %>
</div>
<div class="field">
<%= f.label :NetAddress %><br />
<%= f.text_field :NetAddress %>
</div>
<div class="field">
<%= f.label :DefaultRouter %><br />
<%= f.text_field :DefaultRouter %>
</div>
<div class="field">
<%= f.label :Netmask %><br />
<%= f.text_field :Netmask %>
</div>
<div class="field">
<%= f.label :Broadcast %><br />
<%= f.text_field :Broadcast %>
</div>
<div class="field">
<%= f.label :Notes %><br />
<%= f.text_field :Notes %>
</div>
**<div class="field">
<%= f.label :multicast %><br />
<%= f.check_box :multicast %>
</div>**
<div class="actions">
<%= f.submit %>
<% end %>
Migration:
class AddMulticast < ActiveRecord::Migration
def up
add_column :subnets, :multicast, :boolean, :default => 0
end
def down
remove_column :subnets, :multicast
end
end
I know I'm probably missing some small piece of code. Any help would be appreciated!
I think the problem is here:
add_column :subnets, :multicast, :boolean, :default => 0
what you mean by default => 0?
Boolean can get two values: true and false... 0 and false are not the same...
You should remove the :default => 0 or use the below:
:multicast, :boolean, :default => false, :null => false
Related
I have a weird problem with "nested_form" in Rails. I made a model "evaluate" associated to other model "proyect", but when I try to show theres fields, on "proyects" form, just show fields from "proyects".
Here is my code:
Models:
proyect.erb
class Proyect < ActiveRecord::Base
belongs_to :user
has_many :vercions #I know is versions
has_many :evaluates #I know is evaluators
accepts_nested_attributes_for :evaluates, allow_destroy: true
validates :titulo,:presence => true,
:length => { :minimum => 3 }
validates :descripcion,:presence => true,
:length => { :minimum => 3 }
end
evaluate.erb
class Evaluate < ActiveRecord::Base
belongs_to :proyect
has_and_belongs_to_many :users
end
Controller
proyects_controller.erb
class ProyectsController < ApplicationController
before_action :set_proyect, only: [:show, :edit, :update, :destroy]
# GET /proyects
# GET /proyects.json
def index
if current_user.tipo == 'i'
#proyects = Proyect.where(:user_id => current_user.id)
else
#proyects = #Proyect.where(:id_user => current_user.id)
Proyect.all
end
end
# GET /proyects/1
# GET /proyects/1.json
def show
#vercion = Vercion.new
end
# GET /proyects/new
def new
#proyect = Proyect.new
#proyect.evaluates.build
end
# GET /proyects/1/edit
def edit
end
# POST /proyects
# POST /proyects.json
def create
#proyect = current_user.proyects.new(proyect_params)
respond_to do |format|
if #proyect.save
format.html { redirect_to #proyect, notice: 'Proyecto creado!.' }
format.json { render :show, status: :created, location: #proyect }
else
format.html { render :new }
format.json { render json: #proyect.errors, status: :unprocessable_entity }
end
# Llamamos al ActionMailer que creamos
Usermailer.bienvenido_email(current_user,#proyect).deliver
end
end
# PATCH/PUT /proyects/1
# PATCH/PUT /proyects/1.json
def update
respond_to do |format|
if #proyect.update(proyect_params)
format.html { redirect_to #proyect, notice: 'Proyect was successfully updated.' }
format.json { render :show, status: :ok, location: #proyect }
else
format.html { render :edit }
format.json { render json: #proyect.errors, status: :unprocessable_entity }
end
end
end
# DELETE /proyects/1
# DELETE /proyects/1.json
def destroy
#proyect.destroy
respond_to do |format|
format.html { redirect_to proyects_url, notice: 'Proyect was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_proyect
#proyect = Proyect.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def proyect_params
params.require(:proyect).permit(
:titulo, :descripcion,:evaluador, :id_user, :codigo, :user_assign,evaluates_attributes: [:id,:nombre, :prioridad, :_destroy, user_ids: [] ])
end
end
Views
_form.html.erb (Proyects)
<%= nested_form_for(#proyect) do |f| %>
<% if #proyect.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#proyect.errors.count, "error") %> prohibited this proyect from being saved:</h2>
<ul>
<% #proyect.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :titulo %><br>
<%= f.text_field :titulo %>
</div>
<div class="field">
<%= f.label :descripcion %><br>
<%= f.text_area :descripcion %>
</div>
<div class="field">
<%= f.hidden_field :id_user, :value => current_user.id %>
</div>
<!--Aqui aƱadi algo-->
<fieldset id="evaluates">
<%= f.fields_for :evaluates do |evaluates_form| %>
<div class="field">
<%= evaluates_form.label :status %><br>
<%= evaluates_form.text_field :status %>
</div>
<%= evaluates_form.link_to_remove "Eliminar esta tarea" %>
<% end %>
<p><%= f.link_to_add "Agregar una tarea", :evaluates %></p>
</fieldset>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_evaluate_fields.html.erb
<div class="field">
<%= f.label :status, 'Nombre de la tarea' %><br>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.collection_check_boxes :user_ids, User.where(:tipo => 'e'), :id, :cedula %>
</div>
<%= f.link_to_remove "Eliminar Evaluador" %>
I receive this error with my Book form in rails:
First argument in form cannot contain nil or be empty
Form
<%= form_for #book, html: { multipart: true } do |f| %>
<% if #book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#book.errors.count, "error") %> prohibited this book from being saved:</h2>
<ul>
<% #book.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :author %><br>
<%= f.text_field :author %>
</div>
<div class="field">
<%= f.label :language %><br>
<%= f.text_field :language %>
</div>
<div class="field">
<%= f.label :year %><br>
<%= f.text_field :year %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :total_pages %><br>
<%= f.text_field :total_pages %>
</div>
<div class="field">
<%= f.label :rating %><br>
<%= f.text_field :rating %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Controller
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def search
if params[:search].present?
#books = Book.search(params[:search])
else
#books = Book.all
end
end
def index
#books = Book.all
end
def show
#reviews = Review.where(book_id: #book.id).order("created_at DESC")
if #reviews.blank?
#avg_review = 0
else
#avg_review = #reviews.average(:rating).round(2)
end
end
end
def new
#book = current_user.books.build
end
def edit
end
def create
#book = current_user.books.build(book_params)
respond_to do |format|
if #book.save
format.html { redirect_to #book, notice: 'Book was successfully created.' }
format.json { render :show, status: :created, location: #book }
else
format.html { render :new }
format.json { render json: #book.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #book.update(book_params)
format.html { redirect_to #book, notice: 'Book was successfully updated.' }
format.json { render :show, status: :ok, location: #book }
else
format.html { render :edit }
format.json { render json: #book.errors, status: :unprocessable_entity }
end
end
end
def destroy
#book.destroy
respond_to do |format|
format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_book
#book = Book.find(params[:id])
end
def book_params
params.require(:book).permit(:title, :author, :language, :year, :description, :total_pages, :rating, :image)
end
I got related errors, but none of the solutions seems to fit my particular issue. Any clues to what could be causing this?
You're getting the error because #book is nil.
Make sure that you have initialized #book using #book = Book.new in the corresponding controller action so that #book is available to the view containing the form.
initialized #book = Book.new in the controller action
Not really any error messages, just no file uploaded!
Please help!!!!
everything get created and is fine apart from the file upload.
Models:
class Assets < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
attr_accessible :asset, :asset_file_name
end
class Video < ActiveRecord::Base
attr_accessible :body, :title, :url, :covers, :assets
has_many :covers, :as => :assetable, :class_name => "Video::Cover", :dependent => :destroy
accepts_nested_attributes_for :covers, :allow_destroy => true
class Video::Cover < Assets
has_attached_file :attachment, :styles => { :small => "300x0>", :large => "800x0>" }
end
end
video controller:
class VideosController < ApplicationController
#load_and_authorize_resource
# GET /videos
# GET /videos.xml
def index
#videos = Video.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #videos }
end
end
# GET /videos/1
# GET /videos/1.xml
def show
#video = Video.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #video }
end
end
# GET videos/new
# GET /videos/new.xml
def new
#video = Video.new
1.times do #video.covers.build end
#40.times do #video.images.build end
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #video }
end
end
# GET /collections/1/edit
def edit
#video = Video.find(params[:id])
1.times do #video.covers.build end
#1.times do #video.images.build end
end
# POST /videos
# POST /videos.xml
def create
#video = Video.new(params[:video])
respond_to do |format|
if #video.save
format.html { redirect_to(#video, :notice => 'Video was successfully created.') }
format.xml { render :xml => #video, :status => :created, :location => #video }
else
format.html { render :action => "new" }
format.xml { render :xml => #video.errors, :status => :unprocessable_entity }
end
end
end
# PUT /videos/1
# PUT /videos/1.xml
def update
#video = Video.find(params[:id])
respond_to do |format|
if #video.update_attributes(params[:video])
format.html { redirect_to(#video, :notice => 'Video was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #video.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /videos/1
# DELETE /videos/1.xml
def destroy
#video = Video.find(params[:id])
#video.destroy
respond_to do |format|
format.html { redirect_to(videos_url) }
format.xml { head :ok }
end
end
end
Views:
form
<div id="form_bk">
<div id="field_left">
<%= form_for #video, :html => {:multipart => true} do |f| %>
<% if #video.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#video.errors.count, "error") %> prohibited this video from being saved:</h2>
<ul>
<% #video.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="cover">
<br />
<h2>Cover Image</h2>
<%= f.fields_for :covers do |cover_image| %>
<% if cover_image.object.new_record? %>
<p><%= cover_image.file_field :attachment %></p>
<% end %>
<% end %>
<%= f.fields_for :covers do |cover_image| %>
<% unless cover_image.object.new_record? %>
<br />
<p>
<%=image_tag cover_image.object.url(:small) %><br />
<br /> Delete cover <%= cover_image.check_box :_destroy %><br /><br />
</p>
<% end %>
<% end %>
</div>
<div id="form_right">
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div></div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_area :url %>
</div></div>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<br />
</div>
Index view
<% #videos.each do |video| %>
<div class="video">
<%- video.covers.each do |cover| %>
<div class="video_cover"><%=link_to image_tag (cover.url(:small)), video %></div>
<% end %>
<div class="video_title">
<%=link_to video.title, video %>
<%= video.body %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
<%= link_to 'Destroy', video, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
<br /><br /><br />
<%= link_to 'New Video', new_video_path %>
<%= debug #videos %>
I need ruby to either take the value from an f.select or an f.text_field if 'other' is chosen in the select form. how is this possible?
in the view:
<div class="field">
Parent</br>
<% f.label :parent1 %>
<%= select("jobs","parent1_id",["None","8.5x11","shells"]) %>
Other:
<% f.label :parent2 %>
<%= text_field("jobs","parent2_id") %>
</div>
in the controller:
def create
#job = Job.new(params[:job])
if params[:parent1_id] == "None" #params[:option1_id].nil? #params.has_key?(:option1_id) #Take your pick
#job.parent = params[:parent2_id]
else
#job.parent = params[:parent1_id]
end
respond_to do |format|
if #job.save
format.html { redirect_to #job, :notice => 'Job was successfully created.' }
format.json { render :json => #job, :status => :created, :location => #job }
else
format.html { render :action => "new" }
format.json { render :json => #job.errors, :status => :unprocessable_entity }
end
end
end
The simplest way around the problem would be to use {:include_blank => "none"} in the select, and check if its parameter is null in the controller, if it is then use the other parameter.
How I personally used it:
<%= form_for #thing, :html => { :class => 'form-horizontal' } do |f| %>
.....
<div class="field">
<%= f.label :name %><br />
<%= select_tag "count", "<option>None</option><option>8.5x11</option><option>shells</option>".html_safe %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= text_field_tag "users" %>
</div>
........
<div class="form-actions">
<%= f.submit "Submit", :class => 'btn btn-primary' %>
.......
</div>
<% end %>
Note collection_select is a variation on select where I pass in another model's items.
In the controller:
def create
.......
if params[:option1_id] == "" #params[:option1_id].nil? #params.has_key?(:option1_id) #Take your pick
#thing.attribute = params[:option2_id]
else
#thing.attribute = params[:option1_id]
else
end
Couldn't find Submission without an ID
This problem has haunted me for last few days, and I can't seem to fix it.
This problem occurs when I try to create an Emailinterest object in the app.
I have two models.
Submission
Emailinterest
For every submission, there can be several emailinterest, but emailinterest cannot exist
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
submission_id actually a member of emailinterest object. submission_id is suppose to contain the ID value of submission object.
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\Rails\actuirl5\app\views\submissions\show.html.erb
....
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new(:submission_id=>#submission.id)} %>
C:\Rails\actuirl5\app\views\submissions
<%= form_for(emailinterest) do |f| %>
<%= hidden_field :submission_id, :value => #submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
FIX
C:\Rails\actuirl5\app\controllers\emailinterests_controller.rb
def create
#emailinterest = Emailinterest.new(params[:emailinterest])
#submission = Submission.find(params[:submission_id])
respond_to do |format|
if #emailinterest.save
Notifier.emailinterest_notification(#emailinterest, #submission).deliver
format.html { redirect_to(#emailinterest, :notice => 'Email was successfully sent!') }
format.xml { render :xml => #emailinterest, :status => :created, :location => #emailinterest }
else
format.html { render :action => "new" }
format.xml { render :xml => #emailinterest.errors, :status => :unprocessable_entity }
end
end
end
C:\Rails\actuirl5\app\views\submissions_form_new_emailinterest.html.erb
<%= form_for(emailinterest) do |f| %>
<%= hidden_field_tag :submission_id, value = #submission.id %>
<div class="field">
<%= f.label :sender_email %><br />
<%= f.text_field :sender_email %>
</div>
<div class="field">
<%= f.label :sender_email_content %><br />
<%= f.text_area :sender_email_content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
C:\Rails\actuirl5\app\views\submissions\show.html.erb
<%= render :partial=>"form_new_emailinterest", :locals=>{:emailinterest=>Emailinterest.new} %>
Considering you have set #submission in your show action this should work:
<%= hidden_field :submission_id, :value => emailinterest.submission_id %>
and then
#submission = Submission.find(params[:emailinterest][:submission_id])
You cannot access instance variables like #submission in a partials. This is why you have the option :locals.