When I try and edit the Contact to add a DataType, I receive a: 'Unpermitted parameter: data_format' error. I have tried following similar associations I have found online, but can't figure out what I am missing. I apologize if this is redundant.
Here are the relevant pieces of information:
Models:
# == Schema Information
#
# Table name: contacts
#
# id :integer not null, primary key
# name :string
# email :string
# phone :string
# mobile :string
# created_at :datetime not null
# updated_at :datetime not null
# supplier_id :integer
#
class Contact < ActiveRecord::Base
has_one :data_type
accepts_nested_attributes_for :data_type, allow_destroy: true
end
# == Schema Information
#
# Table name: data_formats
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# contact_id :integer
#
class DataFormat < ActiveRecord::Base
belongs_to :contact
end
Contact Controller:
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
respond_to do |format|
if #contact.save
format.html { redirect_to #contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: #contact }
else
format.html { render :new }
format.json { render json: #contact.errors, status: :unprocessable_entity }
end
end
end
private
def set_contact
#contact = Contact.find(params[:id])
end
def contact_params
params.require(:contact).permit(:name, :email, :phone, :mobile, :data_format,
data_format_attributes: [ :id, :name, :contact_id, :_destroy ] )
end
end
Edit View:
<%= simple_form_for #contact do |f| %>
<div class="form-group">
<div class="col-lg-6">
<%= f.input :name %>
</div>
<div class="col-lg-6">
<%= f.simple_fields_for :data_format do |t| %>
<%= t.input :id, label: 'Date Format:', :collection => DataFormat.order(:name) %>
<% end %>
</div>
<div class="col-lg-6">
<%= f.label "Submit", class: 'control-label' %>
<%= f.button :submit, class: 'btn btn-primary form-control' %>
</div>
</div>
<% end %>
The DataType(s) are appearing in the form and I can select them. So, I know the issue must be within the controller.
Any help would be greatly appreciated!
Your contact.rb is accepting nested attributes name and permitted parameters name mismatch. contact.rb mentions: accepts_nested_attributes_for :data_type, allow_destroy: true .
contact.rb
class Contact < ActiveRecord::Base
has_one :data_format
accepts_nested_attributes_for :data_format, allow_destroy: true
end
contacts_controller.rb
class ContactsController < ApplicationController
before_action :set_contact, only: [:show, :edit, :update, :destroy]
def new
#contact = Contact.new
#contact.data_format = DataFormat.new
end
...
private
def set_contact
#contact = Contact.find(params[:id])
end
def contact_params
params.require(:contact).permit(:name, :email, :phone, :mobile, :data_format,
data_format_attributes: [ :id, :name, :contact_id, :_destroy ] )
end
form
<%= simple_form_for #contact do |f| %>
<div class="form-group">
<div class="col-lg-6">
<%= f.input :name %>
</div>
<div class="col-lg-6">
<%= f.simple_fields_for :data_format, #contact.data_format do |t|%>
<%= t.input :id, label: 'Date Format:', :collection => DataFormat.order(:name) %>
<% end %>
</div>
<div class="col-lg-6">
<%= f.label "Submit", class: 'control-label' %>
<%= f.button :submit, class: 'btn btn-primary form-control' %>
</div>
</div>
<% end %>
Related
While i was trying to submit the form, following error occured: Validation failed: Images imageable must exist and render the same new.html.erb view.
If i comment the file field in new.html.erb. Product is being created successfully.
ProductsController:
def new
#product = Product.new
end
def create
#product = Product.create!(product_params)
if #product.save
redirect_to products_path, notice: "Product Created Successfully"
else
render "new"
end
end
def product_params
params.require(:product).permit(:name, :quantity, :price, images_attributes: [:id, :photo, :_destroy])
end
new.html.erb:
<%= nested_form_for #product, html: { multipart: true } do |f|%>
<h2>New</h2>
<P> <%= f.label :name %> <%= f.text_field :name %> </P>
<P> <%= f.label :quantity %> <%= f.text_field :quantity %> </P>
<P> <%= f.label :price %> <%= f.text_field :price %> </P>
<%= f.fields_for :images do |p| %>
<p> <%= p.label :photo %> <%= p.file_field :photo %> </p>
<%= p.link_to_remove "Remove Image" %>
<% end %>
<%= f.link_to_add "Add Image", :images %>
<%= f.submit "Add Product" %>
<% end %>
20160725102038_add_image_columns_to_imageable.rb:
class AddImageColumnsToImageable < ActiveRecord::Migration[5.0]
def up
add_attachment :images, :photo
end
def down
remove_attachment :images, :photo
end
end
Model:product.rb
class Product < ApplicationRecord
has_one :variant
has_many :images, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true
end
Model:image.rb
class Image < ApplicationRecord
belongs_to :imageable, polymorphic: true
has_attached_file :photo, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment :photo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end
In rails 5, belongs_to makes sure that the associated model must exist.
E.g In this polymorphic association, Image model has belongs_to :imageable and Product model has has_many :images.
So here in new.html.erb we are creating an image, but respective product not exist, so that's why error Image imageable must exist .
Solution
Add optional: true while making an association of belong_to in Image model.
Image Model now looks like:
class Image < ApplicationRecord
belongs_to :imageable, polymorphic: true, optional: true
has_attached_file :photo, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment :photo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end
I have two models:
class Tool < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :tools, dependent: :destroy
end
And I created a migration to provide the foreign key of the model:
class AddUserIdToTool < ActiveRecord::Migration
def change
add_column :tools, :user_id, :integer
end
end
I have the following form:
<%= form_for #tool, :html => { :multipart => true } do |f| %>
<% if #tool.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#tool.errors.count, "error") %> prohibited this tool from being saved:</h2>
<ul>
<% #tool.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.file_field :tool_image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Whenever I submit the form the foreign key is not updated(user_id). I see the following while in my console:
irb(main):002:0> Tool.last
Tool Load (0.0ms) SELECT "tools".* FROM "tools" ORDER BY "tools"."id" DESC LIMIT 1
=> #<Tool id: 6, name: "wrench2", description: "another wrench", created_at: "2015-04-09 21:38:47", updated_at: "2015-04
-09 21:38:47", tool_image_file_name: "wrench.jpg", tool_image_content_type: "image/jpeg", tool_image_file_size: 3424, to
ol_image_updated_at: "2015-04-09 21:38:43", user_id: nil>
irb(main):003:0>
As you can see the user_id is nil
Here is my controller:
class ToolsController < ApplicationController
before_action :authenticate_user!, only: [:new, :destroy, :edit, :update], notice: 'you must sign in first!'
before_action :set_tool, only: [:show, :edit, :update, :destroy]
# GET /tools
# GET /tools.json
def index
#tools = Tool.all
end
# GET /tools/1
# GET /tools/1.json
def show
end
# GET /tools/new
def new
#tool = Tool.new
end
# GET /tools/1/edit
def edit
end
# POST /tools
# POST /tools.json
def create
#tool = Tool.new(tool_params)
respond_to do |format|
if #tool.save
format.html { redirect_to #tool, notice: 'Tool was successfully created.' }
format.json { render :show, status: :created, location: #tool }
else
format.html { render :new }
format.json { render json: #tool.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tools/1
# PATCH/PUT /tools/1.json
def update
respond_to do |format|
if #tool.update(tool_params)
format.html { redirect_to #tool, notice: 'Tool was successfully updated.' }
format.json { render :show, status: :ok, location: #tool }
else
format.html { render :edit }
format.json { render json: #tool.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tools/1
# DELETE /tools/1.json
def destroy
#tool.destroy
respond_to do |format|
format.html { redirect_to tools_url, notice: 'Tool was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tool
#tool = Tool.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tool_params
params.require(:tool).permit(:name, :description, :tool_image, :user_id)
end
end
How would I get the user_id updated with the id of the logged in user? Do I have to include a hidden field in my form or something like that?
Also do I have to permit the user_id variable within my strong parameters in my controller? I have tried this but it has not solved the problem...
How does this work?
Currently you are building a tool on its own. To associate it with the user model you need to create a tool model in the following way:
#user.tools.build
It will create a new ActiveRecord tool model with proper user_id of a belonged model. But to get parent you need to fetch its id from your params. It could be done in a such way, in your action:
#user = User.find(params[:user_id])
Note: to make it possible your routes should be nested correspondingly (read more here: http://guides.rubyonrails.org/routing.html)
So, all in all, your new action should look like:
def new
#user = User.find(params[:user_id])
#tool = #user.tools.build
end
And in your routes.rb
resources :users do
resources :tools
end
I am getting the following error while trying to do reset password by sending sending the link to email and open that link from that email.
Error:
NoMethodError in Admins#editpass
Showing C:/Site/swargadwar_admin/app/views/admins/editpass.html.erb where line #16 raised:
undefined method `Password_field' for #<ActionView::Helpers::FormBuilder:0x21c0108>
Extracted source (around line #16):
13: <% end %>
14: <p>
15: <label for "new_pass">New Password :</label>
16: <%= f.Password_field :password,placeholder:"Enter your new password" %>
17: </p>
18: <p>
19: <label for "new_pass">Confirm New Password :</label>
Rails.root: C:/Site/swargadwar_admin
Application Trace | Framework Trace | Full Trace
app/views/admins/editpass.html.erb:16:in `block in _app_views_admins_editpass_html_erb___904659562_17338176'
app/views/admins/editpass.html.erb:2:in `_app_views_admins_editpass_html_erb___904659562_17338176'
Please check my below codes and let me to know where i did the mistake as well as try to help me to resolve this.
views/admins/editpass.html.erb
<center>
<%= form_for :admin,:url => {:action => "setpass",:id => params[:id] } do |f| %>
<% if #admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<label for "new_pass">New Password :</label>
<%= f.Password_field :password,placeholder:"Enter your new password" %>
</p>
<p>
<label for "new_pass">Confirm New Password :</label>
<%= f.Password_field :password_confirmation,placeholder:"confirm your new password" %>
</p>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
</center>
controller/admins_controller.rb
class AdminsController < ApplicationController
def create_registration
#admin=Admin.new(params[:admin])
if #admin.save
flash[:notice]="User has created successfully"
flash[:color]="valid"
redirect_to :action => "index" , :controller => 'homes'
else
flash[:alert]="User could not created"
flash[:color]="invalid"
render 'homes/index'
end
end
def forget
#admin=Admin.new
end
def resetpass
#admin=Admin.find_by_email(params[:admin][:email])
if #admin.email==params[:admin][:email]
UserMailer.registration_confirmation(#admin).deliver
flash[:notice]="Check your email to reset the password"
flash[:color]="valid"
redirect_to :action => "index" , :controller => 'homes'
else
flash[:alert]="Invalid email id"
flash[:color]="invalid"
render 'homes/index'
end
end
def editpass
#admin=Admin.new
end
def setpass
#admin=Admin.find(params[:id])
if #admin.update_attributes(params[:admin])
flash[:notice]="Your password has updated successfully"
flash[:color]="valid"
redirect_to :action => "index" , :controller => 'homes'
else
flash[:alert]="Your password could not updated"
flash[:color]="invalid"
render 'homes/index'
end
end
end
model/admin.rb
class Admin < ActiveRecord::Base
attr_accessible :email, :password_hash, :password_salt, :picture, :user_name,:password_confirmation,:password, :remember_me
attr_accessor :password
attr_accessor :remember_token
before_save :encrypt_password
mount_uploader :picture, PictureUploader
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :user_name, :presence => true, :length => {:in => 3..10}
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
has_secure_password
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def Admin.digest(string)
cost = 10
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def Admin.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = Admin.new_token
update_attribute(:remember_digest, Admin.digest(remember_token))
end
def forget
update_attribute(:remember_digest, nil)
end
end
Please help me.
You just have a typo: The method is named password_field, not Password_field. Just change the two method calls in app/views/admins/editpass.html.erb and you are done.
I have a Section model, where a Section can be a parent of another Section (subsection).
Here is my model:
class Section < ActiveRecord::Base
has_many :exercises
has_one :parent_link,
:foreign_key => 'subsection_id',
:class_name => 'SectionLink',
:dependent => :destroy
has_one :parent, :through => :parent_link
has_many :subsection_links,
:foreign_key => 'parent_id',
:class_name => 'SectionLink',
:dependent => :destroy
has_many :subsections, :through => :subsection_links
attr_accessor :parent_id
def to_param
"#{id}-#{description.parameterize}"
end
def self.search(search)
if search
find(:all, :conditions => ['description LIKE ?', "%#{search}%"])
else
find(:all)
end
end
end
And the association model:
class SectionLink < ActiveRecord::Base
belongs_to :parent, :class_name => 'Section'
belongs_to :subsection, :class_name => 'Section'
end
My controller:
class SectionsController < ApplicationController
# GET /sections
# GET /sections.json
def index
#sections = Section.order("subsections_count DESC").search(params[:search])
respond_to do |format|
format.html # index.html.erb
end
end
# GET /sections/1
# GET /sections/1.json
def show
#section = Section.find(params[:id])
#subsections = #section.subsections
#exercises = #section.exercises
respond_to do |format|
format.html # show.html.erb
format.json { render json: #section }
end
end
# GET /sections/new
# GET /sections/new.json
def new
#section = Section.new
#section.parent_id = params[:parent]
respond_to do |format|
format.html # new.html.erb
format.json { render json: #section }
end
end
# GET /sections/1/edit
def edit
#section = Section.find(params[:id])
end
# POST /sections
# POST /sections.json
def create
#section = Section.new(params[:section])
#parent = #section.build_parent(:parent_id => #section.parent_id) unless #section.parent_id.empty?
respond_to do |format|
if #section.save
format.html { redirect_to #section, notice: 'Section was successfully created.' }
format.json { render json: #section, status: :created, location: #section }
else
format.html { render action: "new" }
format.json { render json: #section.errors, status: :unprocessable_entity }
end
end
end
# PUT /sections/1
# PUT /sections/1.json
def update
#section = Section.find(params[:id])
respond_to do |format|
if #section.update_attributes(params[:section])
format.html { redirect_to #section, notice: 'Section was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #section.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sections/1
# DELETE /sections/1.json
def destroy
#section = Section.find(params[:id])
#section.destroy
respond_to do |format|
format.html { redirect_to sections_url }
format.json { head :no_content }
end
end
end
The parent ID is fed in via a hidden field in the form:
<%= form_for(#section) do |f| %>
<% if #section.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#section.errors.count, "error") %> prohibited this section from being saved:</h2>
<ul>
<% #section.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :description %><br />
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<%= f.hidden_field :parent_id, :value => #section.parent_id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I'm getting
undefined method 'build_parent' for #<Section:0xb4f1764c>
Is there a better way of modelling this association? Why is build_parent undefined?
UPDATE:
Now works with following controller code:
#section = Section.new(params[:section])
unless #section.parent_id.empty?
#parent = Section.find(#section.parent_id)
#section.parent = #parent
end
Looking for any suggestions on how it can be improved, and why it did not work before...
You're looking to do a self join.
class Section < ActiveRecord::Base
has_many :subsections, :class_name => "Section"
belongs_to :parent_section, :class_name => "Section",
:foreign_key => "parent_id"
end
I use accepts_nested_attributes_for in a model but my child form isn't saved in the database?
I am building a nested form almost in the same way as in episode 196 / 197 from Ryan Bates railscasts. I have a parent question form and as a child the answer form:
models/question.rb
class Question < ActiveRecord::Base
belongs_to :user
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:text].blank? },
:allow_destroy => true
validates :content, :presence => true
end
models/answer.rb
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
controllers/questions_controller.rb
class QuestionsController < ApplicationController
before_filter :authenticate_user!
def index
setup_questions
end
def create
#question = Question.new(params[:question])
#this is to get every id_key from the user into the params of the answer
params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
#question.user_id = current_user.id
if #question.save
redirect_to questions_path, :notice => "Successfully created question."
else
setup_questions
render :index
end
end
def edit
#question = Question.find(params[:id])
end
def update
#question = Question.find(params[:id])
respond_to do |format|
if #question.update_attributes(params[:question])
format.html { redirect_to(#question, :notice => 'Question was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #question.errors, :status => :unprocessable_entity }
end
end
end
def show
#question = Question.find(params[:id])
end
def destroy
#question = Question.find(params[:id])
#authorize! :destroy, #question
#question.destroy
redirect_to questions_path, :notice => "Successfully deleted question: #{#question.content}."
end
private
def setup_questions
#questions = Question.all
#question ||= Question.new
#question.answers.build #to build the answers form
end
end
views/question/_form.html.erb
<%= form_for(#question) do |f| %>
<!-- =================== -->
<!-- = Error handeling = -->
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<!-- =================== -->
<!-- ================= -->
<!-- = Question form = -->
<div class="field">
<%= f.label :content, "Question" %><br />
<%= f.text_field :content, :placeholder => "type your question here.." %>
</div>
<!-- ====================== -->
<!-- = Nested Answer form = -->
<div class="answer-field">
<%= f.fields_for :answers do |builder| %>
<%= builder.label :content, "Possible answer" %><br />
<%= builder.text_field :content, :placeholder => "type an optional answer.." %>
<% end %>
</div>
<!-- ====================== -->
<div class="actions">
<%= f.submit %>
</div>
<!-- ================= -->
<% end %>
So now the big question... Why doesn't it save anything from the answer field to the database? I thought one create action at the parent (question) should be enough and that the "accepts_nested_attributes_for" should take care of it's child(s).
Regards,
Thijs
1 Remove this line from controller
params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
2 add this into your view
<!-- = Nested Answer form = -->
<div class="answer-field">
<%= f.fields_for :answers do |builder| %>
<% builder.object.user = current_user %>
<%= builder.label :content, "Possible answer" %><br />
<%= builder.text_field :content, :placeholder => "type an optional answer.." %>
<%= builder.hidden_field, :user_id %>
<% end %>
</div>
<!-- ====================== -->