I've got a ruby 2.0.0 and rails 4.0.0 app that has a 'Guitar' and 'Photos' model. I've used paperclip to upload a single file in Rails3, but am new to uploading multiple files in Rails 4. I created the second model to hold said photos, and read up on strong parameters, etc. I'm getting an error when attempting to add 3 photos to a guitar. In the logs: "Unpermitted parameters: photos_attributes". I've tried adding photos_attributes to the whitelist and no joy. I'm pulling my hair out here - there's no error in the web view, but when I console in and type 'Photo.all', I get nothing. What am I doing wrong? I'm kind of a newbie, please be gentle.
guitar.rb
class Guitar < ActiveRecord::Base
belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb
class Photo < ActiveRecord::Base
belongs_to :guitar
has_attached_file :photo, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>',
large: '600x6003'
}
end
guitars_controller.rb
class GuitarsController < ApplicationController
before_action :set_guitar, only: [:show, :edit, :update, :destroy]
# GET /guitars
# GET /guitars.json
def index
#guitars = Guitar.all
end
# GET /guitars/1
# GET /guitars/1.json
def show
end
# GET /guitars/new
def new
#guitar = current_user.guitars.build
3.times {#guitar.photos.build}
end
# GET /guitars/1/edit
def edit
3.times {#guitar.photos.build}
end
# POST /guitars
# POST /guitars.json
def create
#guitar = current_user.guitars.build(guitar_params)
respond_to do |format|
if #guitar.save
format.html { redirect_to #guitar, notice: 'Guitar was successfully created.' }
format.json { render action: 'show', status: :created, location: #guitar }
else
format.html { render action: 'new' }
format.json { render json: #guitar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guitars/1
# PATCH/PUT /guitars/1.json
def update
respond_to do |format|
if #guitar.update(guitar_params)
format.html { redirect_to #guitar, notice: 'Guitar was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #guitar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guitars/1
# DELETE /guitars/1.json
def destroy
#guitar.destroy
respond_to do |format|
format.html { redirect_to guitars_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guitar
#guitar = Guitar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :oneowner, :user_id, :description, :photos_attributes)
end
end
views/guitar/_form.html.erb
<%= form_for #guitar, :html => { :multipart => true } do |f| %>
<% if #guitar.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#guitar.errors.count, "error") %> prohibited this guitar from being saved:</h2>
<ul>
<% #guitar.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :make %><br>
<%= f.text_field :make %>
</div>
<div class="field">
<%= f.label :model %><br>
<%= f.text_field :model %>
</div>
<div class="field">
<%= f.label :year %><br>
<%= f.text_field :year %>
</div>
<div class="field">
<%= f.label :color %><br>
<%= f.text_field :color %>
</div>
<div class="field">
<%= f.label :serial %><br>
<%= f.text_field :serial %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :condition %><br>
<%= f.number_field :condition %>
</div>
<div class="field">
<%= f.label :kind %><br>
<%= f.text_field :kind %>
</div>
<div class="field">
<%= f.label :bodykind %><br>
<%= f.text_field :bodykind %>
</div>
<div class="field">
<%= f.label :frets %><br>
<%= f.text_field :frets %>
</div>
<div class="field">
<%= f.label :oneowner %><br>
<%= f.check_box :oneowner %>
</div>
<div class="field">
<%= f.label :extended_description %><br>
<%= f.text_area :description %>
</div>
<%= f.fields_for :photos do |builder| %>
<%= builder.label :photo, "Image File" %>
<%= builder.file_field :photo %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
db/schema.rb
ActiveRecord::Schema.define(version: 20131014195859) do
create_table "guitars", force: true do |t|
t.string "make"
t.string "model"
t.string "year"
t.string "color"
t.string "serial"
t.string "price"
t.integer "condition"
t.string "kind"
t.string "bodykind"
t.string "frets"
t.boolean "oneowner"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "description"
end
add_index "guitars", ["user_id"], name: "index_guitars_on_user_id", using: :btree
create_table "photos", force: true do |t|
t.integer "guitar_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "city"
t.string "state"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
There was a typo.
:photos_attributes => [:photo]
Replace existing code with following and try to run the app.
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :oneowner, :user_id, :description, :photos_attributes [:photo])
end
Or you can check out the following link where i have created one sample app with nested attributes.
Nested Attributes Example
Related
After typing in topic and details in the appropriate text field and area, it still shows "validation error" "Topic cannot be empty". what is the problem? I cannot seem to figure it out.
This is the task partial
_task.html.erb:
<li id="task-<%= task.id %>">
<%= link_to gravatar_for(task.user, size: 50), task.user %>
<span class="task"><%= link_to task.user.name, task.user %></span>
<span class="topiclabel">Title:</span>
<span class="topic"style=color:black> <%= task.topic %></span>
<span class="detailslabel">Details: </span>
<span class="detail"><%= task.detail %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(task.created_at) %> ago.
</span>
</li>
Schema:
ActiveRecord::Schema.define(version: 20170526194708) do
create_table "stages", force: :cascade do |t|
t.text "topic"
t.datetime "duration"
t.text "detail"
t.integer "task_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["task_id"], name: "index_stages_on_task_id"
t.index ["user_id", "created_at"], name: "index_stages_on_user_id_and_created_at"
t.index ["user_id"], name: "index_stages_on_user_id"
end
create_table "tasks", force: :cascade do |t|
t.text "topic"
t.integer "member"
t.text "detail"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id", "created_at"], name: "index_tasks_on_user_id_and_created_at"
t.index ["user_id"], name: "index_tasks_on_user_id"
end
routes.rb:
resources :tasks, only: [:create, :destroy]
task.rb
class Task < ApplicationRecord
belongs_to :user
has_many :stages
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :topic, presence: true, length: { maximum: 140 }
validates :detail, presence: true, length: { maximum: 140 }
end
Project_pages_controller.rb:
def home
if logged_in?
#task = current_user.tasks.build
end
_task_form.html.erb:
<%= form_for(#task) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_field :topic, placeholder: "Compose new task..." %>
<%= f.text_area :detail, placeholder: "Write task details..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
This is the show page
show.html.erb
<div class="col-md-8">
<% if #user.tasks.any? %>
<% if #user.stages.any? %>
<h3>Tasks (<%= #user.tasks.count %>)</h3>
<ol class="tasks">
<%= render #tasks %>
<%= render #stages %>
</ol>
<%= will_paginate #tasks %>
<% end %>
<% end %>
</div>
This is my controller;
class TasksController < ApplicationController
include TasksHelper
before_action :logged_in_user, only: [:create, :destroy]
def create
#task = current_user.tasks.build(task_params)
if #task.save
flash[:success] = "Task created!"
redirect_to root_url
else
#feed_items = []
render 'workshareapp_pages/home'
end
end
def destroy
end
private
def task_params
params.require(:task).permit(:detail)
end
end
validates :topic, presence: true, length: { maximum: 140 }
This clause validates the topic attribute in your record to be present and within 140 char length. This is used to validate record before save. You may remove it if you really dont need it here. Another option you can create a form model which is used specifically for this form.
It's probably a validation in your model Task.
You are missing the :topic field in your permit, which is why it's not being processed.
def task_params
params.require(:task).permit(:detail, :topic)
end
I'm doing a parking permit website. The problem I met is that I'm not able to save my data to the PERMIT database which associated with the USER database. The problem i think is I didn't bring the user to the permit(Maybe i missed something). I found out the error when I trying to save from Permit.errors.full_messages is ["User must exist"]. Any help is appreciated, Thank you!
Schema.rb
ActiveRecord::Schema.define(version: 20160920143651) do
create_table "permits", force: :cascade do |t|
t.string "vehicle_type"
t.string "name"
t.string "studentid"
t.string "department"
t.string "carplate"
t.string "duration"
t.date "permitstart"
t.date "permitend"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_permits_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.integer "user_type"
end
end
Create_permit.rb
class CreatePermits < ActiveRecord::Migration[5.0]
def change
create_table :permits do |t|
t.string :vehicle_type
t.string :name
t.string :studentid
t.string :department
t.string :carplate
t.string :duration
t.date :permitstart
t.date :permitend
t.references :user, foreign_key: true
t.timestamps
end
add_index :permits, :user_id
end
end
Permit_controller
class PermitsController < ApplicationController
before_action :set_permit, only: [:show, :destroy]
def index
#permits = Permit.all
end
def new
#permits = Permit.new
end
def create
#permits = Permit.new(permit_params)
if #permits.save
redirect_to #permits
else
redirect_to contact_path
end
end
def destroy
Permit.destroy_all(user_id: 1)
respond_to do |format|
format.html { redirect_to users_url, notice: 'Permit was successfully canceled.' }
format.json { head :no_content }
end
end
def show
#permits = Permit.find(params[:id])
end
def update
respond_to do |format|
if #permits.update(user_params)
format.html { redirect_to #user, notice: 'Permit 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
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate, :duration, :permitstart, :permitend)
end
end
user.rb
class User < ApplicationRecord
has_many :permits
has_secure_password
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
end
permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#permits) do |f| %>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, class: 'form-control' %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, class: 'form-control' %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :permit_start %>
<%= f.date_select :permitstart, class: 'form-control' %>
<%= f.label :permit_end %>
<%= f.date_select :permitend, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
I guess you are using Rails 5. The problem is you have belongs_to association on permit that is belongs to user but while creating permit you are not associating any user with it and in Rails 5 it is mandatory to assign data to belongs_to association i.e you can not save permit when it don't have user_id so try to assign a user_id to permit. refer this for change to rails 5 belongs_to association
Where exactly is your error ? If in update action, you have to change your before_action. You must add there :update action.
before_action :set_permit, only: [:show, :destroy, :update]
I am trying to implement a Parking Permit application page using ROR. I couldn't get my data saved into the database. The permit database is associated with the user also. The program won't save the data and execute the else statement. There is no error generated, i think i have missed something but i don't know the exact problem. Any help is appreciated!
Permit_controller.rb
class PermitsController < ApplicationController
before_action :set_permit, only: [:show, :destroy]
def index
#permits = Permit.all
end
def new
#permits = Permit.new
end
def create
#permits = Permit.new(permit_params)
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
def destroy
end
def show
#permits = Permit.find(params[:id])
end
private
# Use callbacks to share common setup or constraints between actions.
def set_permit
#permits = Permit.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def permit_params
params.require(:permit).permit(:vehicle_type, :name, :studentid, :department, :carplate,:permitstart, :permitend)
end
end
Permit.rb
class Permit < ApplicationRecord
belongs_to :user
end
Create_permit.rb
class CreatePermits < ActiveRecord::Migration[5.0]
def change
create_table :permits do |t|
t.string :vehicle_type
t.string :name
t.string :studentid
t.string :department
t.string :carplate
t.date :permitstart
t.date :permitend
t.references :user, foreign_key: true
t.timestamps
end
add_foreign_key :permits, :user
add_index :permits, [:user_id, :created_at]
end
end
User.rb
class User < ApplicationRecord
has_secure_password
has_many :permits
end
#book pg 264 Validation
permit/new.html.erb
<% provide(:title, 'New Permit') %>
<h1>Permit Application</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#permits) do |f| %>
<%= f.label :"Vehicle" %>
<%= f.text_field :vehicle_type, class: 'form-control' %>
<%= f.label :"License Plate" %>
<%= f.text_field :carplate, class: 'form-control' %>
<%= f.label :"Student ID" %>
<%= f.text_field :studentid, class: 'form-control' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :"Department of applicant" %>
<%= f.text_field :department, class: 'form-control' %>
<%= f.label :permit_start %>
<%= f.date_select :permitstart, class: 'form-control' %>
<%= f.label :permit_end %>
<%= f.date_select :permitend, class: 'form-control' %>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
schema.rb
ActiveRecord::Schema.define(version: 20160921071908) do
create_table "permits", force: :cascade do |t|
t.string "vehicle_type"
t.string "name"
t.string "studentid"
t.string "department"
t.string "carplate"
t.date "permitstart"
t.date "permitend"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_permits_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.integer "user_type"
end
end
check with this #permits.save!.
it shows the exact error.
module ApplicationHelper
#for current user to use through out the app
def current_user
#current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
end
end
and
def create
#permits = Permit.new(permit_params)
#permits.user = current_user
if #permits.save
redirect_to root_path
else
redirect_to contact_path
end
end
test it
Or you can just say that a permit has a single user and avoid the confusion.
#models/permit.rb
class Permit < ApplicationRecord
has_one :user
end
#controllers/permit_controller.rb
def create
#user = User.find(session[:user_id]) #use your session variable
#permits = Permit.new(permit_params)
if #permits.save
#user.permits << #permits
redirect_to root_path
else
redirect_to contact_path
end
end
It will save permits for the logged in user.
Using Rails 4 and Ruby 2.2,
I have book as model which should have image upload functionality in the show page of book. So User can create the book first and from show page upload the multiple images for book. I have used carrierwave as gem and have separate model created for Image.
image.rb
class Image < ActiveRecord::Base
belongs_to :book
mount_uploader :avatar, AvatarUploader
end
book.rb
class Book < ActiveRecord::Base
belongs_to :subject, dependent: :destroy
belongs_to :user, dependent: :destroy
has_many :images, dependent: :destroy
end
books/show.html.erb
<%= form_for(Image.new, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
<%= hidden_field_tag "image[book_id]", #book.id %>
</div>
<%= f.fields_for :images do |p| %>
<div class="field">
<%= p.label :avatar %><br>
<%= p.file_field :avatar, :multiple => true, name: "images[avatar]" %>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
schema.rb
create_table "images", force: :cascade do |t|
t.datetime "avatar_updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "book_id", limit: 4
t.string "avatar", limit: 255
t.string "name", limit: 255
end
create_table "books", force: :cascade do |t|
t.string "title", limit: 255
t.integer "page_number", limit: 4
t.text "description", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id", limit: 4
t.integer "subject_id", limit: 4
t.boolean "active", default: false
end
Now I am kind of unable to proceed with this, can someone guide me on this because I am having form in books/show page and I need to show the image on the same page after successful updation of images.(Multiple images can be uploaded)
Thanks in advance
let me know if I need to provide any more information.
You can use the association itself and list all the images which have been uploaded.
<%= form_for(Image.new, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
<%= hidden_field_tag "image[book_id]", #book.id %>
</div>
<%= f.fields_for :images do |p| %>
<div class="field">
<%= p.label :avatar %><br>
<%= p.file_field :avatar, :multiple => true, name: "images[avatar]" %>
</div>
<% end %>
<!-- Listing all the images for a book -->
<% if #book.images.any? %>
<% #book.images.each do |img| %>
<%= image_tag(img.avatar.url) %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Hi I am trying to create a rails app,My app working fine locally but when I deployed it on Heroku its giving error.I have tried all other solutions on stackoverflow but not any working for me.
Rendered userdetails/new.html.erb within layouts/application (69.0ms)
2014-11-09T17:38:23.427665+00:00 app[web.1]: Started GET "/userdetails/new" for 113.193.105.11 at 2014-11-09 17:38:23 +0000
2014-11-09T17:38:23.511449+00:00 app[web.1]: Completed 500 Internal Server Error in 80ms
2014-11-09T17:38:23.513446+00:00 app[web.1]: app/views/userdetails/new.html.erb:4:in `_app_views_userdetails_new_html_erb__4090279191377710950_69952963663860'
2014-11-09T17:38:23.513443+00:00 app[web.1]: app/views/userdetails/_form.html.erb:24:in `block in _app_views_userdetails__form_html_erb___1876473002485377605_69952963666180'
2014-11-09T17:38:23.511258+00:00 app[web.1]: Rendered userdetails/_form.html.erb (67.7ms)
2014-11-09T17:38:23.430938+00:00 app[web.1]: Processing by UserdetailsController#new as HTML
2014-11-09T17:38:23.513448+00:00 app[web.1]:
2014-11-09T17:38:23.513426+00:00 app[web.1]:
2014-11-09T17:38:23.513429+00:00 app[web.1]: ActionView::Template::Error (undefined method `myuserid' for #<Userdetail:0x007f3e631929d8>):
2014-11-09T17:38:23.513430+00:00 app[web.1]: 21: </div>
2014-11-09T17:38:23.513433+00:00 app[web.1]: 22: <div class="field">
2014-11-09T17:38:23.513435+00:00 app[web.1]: 23: <%= f.label :myuserid , 'User ID'%><br>
2014-11-09T17:38:23.513436+00:00 app[web.1]: 24: <%= f.text_field :myuserid %>
2014-11-09T17:38:23.513438+00:00 app[web.1]: 25: </div>
2014-11-09T17:38:23.513439+00:00 app[web.1]: 26: <div class="field">
2014-11-09T17:38:23.513441+00:00 app[web.1]: 27: <%= f.label :birthday %><br>
2014-11-09T17:38:23.513445+00:00 app[web.1]: app/views/userdetails/_form.html.erb:1:in `_app_views_userdetails__form_html_erb___1876473002485377605_69952963666180'
My userdetail/new.html.erb looks like
<h1>New userdetail</h1>
<%= render 'form',userdetail: #userdetail %>
<%= link_to 'Back', userdetails_path %>
and My userdetail/_form.html looks like
<%= form_for(#userdetail) do |f| %>
<% if #userdetail.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#userdetail.errors.count, "error") %> prohibited this userdetail from being saved:</h2>
<ul>
<% #userdetail.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :myuserid , 'User ID'%><br>
<%= f.text_field :myuserid %>
</div>
<div class="field">
<%= f.label :birthday %><br>
<%= f.date_select :birthday %>
</div>
<div class="field">
<%= f.label :gender %><br>
<%= f.text_field :gender %>
</div>
<div class="field">
<%= f.label :contact_number %><br>
<%= f.text_field :contact_number %>
</div>
<div class="field">
<%= f.label :address %><br>
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.text_field :country %>
</div>
<div class="field">
<%= f.label :state %><br>
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :gre_date %><br>
<%= f.date_select :gre_date %>
</div>
<%= f.hidden_field :user_id %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My userdetail controller looks like
class UserdetailsController < ApplicationController
before_action :set_userdetail, only: [:show, :edit, :update, :destroy]
#User can only edit or destroy its own details
before_action :correct_user, only: [:edit, :update, :destroy]
# GET /userdetails
# GET /userdetails.json
def index
#userdetails = Userdetail.all
end
# GET /userdetails/1
# GET /userdetails/1.json
def show
end
# GET /userdetails/new
def new
#userdetail = Userdetail.new
end
# GET /userdetails/1/edit
def edit
end
# POST /userdetails
# POST /userdetails.json
def create
#userdetail = Userdetail.new(userdetail_params)
#userdetail.user_id = current_user.id
respond_to do |format|
if #userdetail.save
format.html { redirect_to #userdetail, notice: 'Userdetail was successfully created.' }
format.json { render :show, status: :created, location: #userdetail }
else
format.html { render :new }
format.json { render json: #userdetail.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /userdetails/1
# PATCH/PUT /userdetails/1.json
def update
respond_to do |format|
if #userdetail.update(userdetail_params)
format.html { redirect_to #userdetail, notice: 'Userdetail was successfully updated.' }
format.json { render :show, status: :ok, location: #userdetail }
else
format.html { render :edit }
format.json { render json: #userdetail.errors, status: :unprocessable_entity }
end
end
end
# DELETE /userdetails/1
# DELETE /userdetails/1.json
def destroy
#userdetail.destroy
respond_to do |format|
format.html { redirect_to userdetails_url, notice: 'Userdetail was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_userdetail
#userdetail = Userdetail.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def userdetail_params
params.require(:userdetail).permit(:first_name, :last_name, :myuserid, :birthday, :gender, :contact_number, :address, :country, :state, :city, :gre_date)
end
def correct_user
#user = #userdetail.user_id
redirect_to( userdetails_url) unless #user == current_user.id
end
end
My user detail model looks like
class Userdetail < ActiveRecord::Base
belongs_to :user
#validates that one user can create just one user details
validates_uniqueness_of :user_id, :message => "details already exist"
end
My db schema looks like
ActiveRecord::Schema.define(version: 20141108175213) do
create_table "posts", force: true do |t|
t.text "issue", default: "", null: false
t.text "description", default: "", null: false
t.integer "rating"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "topic_id"
end
create_table "topics", force: true do |t|
t.text "issue", default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "userdetails", force: true do |t|
t.string "first_name"
t.string "last_name"
t.date "birthday"
t.string "gender"
t.string "contact_number"
t.text "address"
t.string "country"
t.string "state"
t.string "city"
t.date "gre_date"
t.datetime "created_at"
t.datetime "updated_at"
t.string "myuserid"
t.integer "user_id"
end
add_index "userdetails", ["myuserid"], name: "index_userdetails_on_myuserid", unique: true
add_index "userdetails", ["user_id"], name: "index_userdetails_on_user_id"
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Please tell me answer. I generated app's view and model using command rails g scaffold userdetail but added 'myuserid' column later.Please see any if there is any other fault inn my code and suggest correction
Also my directory contain automatically generated duplicate files with ~ at the end of name, Any idea ?
I found the answer, when i ran rails console in heroku I found that myuserid column was not created in heroku. When i check my migration files I found that the migration file creating myuserid column was accidentally deleted. I recreated that file and my app start working.