I can't save record in sqlite Rails - ruby-on-rails

I try save record on database, in pivot table but I have a error(screen) if I write #video.category_ids = 1 it works but category id = 1 in database
Below I put my code if you neeed more information please write.
videos_controller.rb
class VideosController < ApplicationController
layout 'master'
before_action :chceck_login
def index
#videos = Video.sortASC
end
def new
#categories = Category.all
end
def create
#video = Video.new(video_params)
#video.category_ids = [video_params]
if #video.save
flash[:notice] = 'The movie has been added successfuly'
redirect_to(:action => 'index')
else
#categories = Category.all
render('new')
end
end
def show
end
def edit
end
def delete
end
private
def video_params
params.require(:video).permit(:title, :url, :description, category_attributes: [:id]).merge(user_id: session[:user_id])
end
end
_form_html.erb
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-tag"></i></span>
</div>
<%= f.text_field :title, placeholder: 'Title', :class => 'form-control', :id => 'title' %>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-link"></i></span>
</div>
<%= f.text_field :url, placeholder: 'URL', :class => 'form-control', :id => 'url' %>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-edit"></i></span>
</div>
<%= f.text_area :description, placeholder: 'Description', rows: 5, :class => 'form-control', :id => 'description' %>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-sitemap"></i></span>
</div>
<%= f.select(:category_id, #categories.map{|t| [t.name, t.id]}, {}, :multiple => true, :class => 'custom-select') %>
</div>
video.rb
class Video < ApplicationRecord
belongs_to :user
has_and_belongs_to_many :categories
scope :sortASC, lambda{order("videos.created_at ASC")}
end
new.html.erb
<% #page_title = 'Add Film' %>
<div class='container'>
<div class='row'>
<div class='col s-6'>
<div class="jumbotron jumbotron-fluid bg-info">
<div class="container">
<h1 class="display-4 text-center">ADD NEW FILM <i class="fas fa-plus"></i></h1>
<p class="lead">This is a modified jumbotron that occupies the entire horizontal space of its parent.</p>
</div>
</div>
</div>
<div class='col s-6'>
<%= form_for(:video, :url => {:action => 'create'}) do |f| %>
<%= render(:partial => 'form', :locals => {:f => f}) %>
<%= submit_tag('Add Film', :class => 'btn btn-primary float-right') %>
<% end %>
</div>
</div>
</div>
Error
https://imgur.com/7P4WpMU
If you want I have put my project on Github.

I would suggest the following changes:
In _form.html.erb pluralize the name of the select, i.e. change
<%= f.select(:category_id, #categories.map{|t| [t.name, t.id]}, {}, :multiple => true, :class => 'custom-select') %>
to
<%= f.select(:category_ids, #categories.map{|t| [t.name, t.id]}, {}, :multiple => true, :class => 'custom-select') %>
In videos_controller.rb change
def video_params
params.require(:video).permit(:title, :url, :description, category_attributes: [:id]).merge(user_id: session[:user_id])
end
to
def video_params
params[:video][:category_ids].reject!(&:blank?) unless params[:video][:category_ids].nil?
params.require(:video).permit(:title, :url, :description, category_ids: []).merge(user_id: session[:user_id])
end
Also in videos_controller.rb remove
#video.category_ids = [video_params]
to leave things to the constructor
Regarding 2. I am not 100% sure about the exact solution, maybe you should also reject the empty id you have in your parameters (according to the provided screenshot, use .reject!(&:blank?) for that) - for analyzing further problems check the output of the video_params function before passing it to the constructor - it basically should look like a hash representation of the expected object.
edit Updated the change proposal for 2. regarding category_ids

Related

Type error on nested form

been banging my head on the table over this one. I have another nested form but this one is driving me crazy.
The error is:
TypeError in CompaniesController#create
no implicit conversion of Symbol into Integer
#company = Company.new(company_params)
The controller:
class CompaniesController < ApplicationController
layout 'welcome'
def new
#company = Company.new
end
def create
#company = Company.new(company_params)
if #company.save
flash[:notice] = "New company created successful."
redirect_to admin_accounts_path
else
flash.now[:alert] = "Creation failed, please try again"
render :new
end
end
private
def company_params
params.require(:company).permit(:name, :location, :users_attributes => [:email, :password])
end
end
On the new.html.erb:
<%= render partial: 'form', locals: { company: #company, users_attributes: :users_attributes } %>
This code looks exactly like another nested setup I have, but it works :p
I had read that sometimes changing the params from => to just having a semicolon works, but replacing the user_attributes => with a user_attributes: didn't change anything.
EDIT: form.html.erb
<%= form_for company, url: companies_path do |f| %>
<div class="col-12">
<div class="row">
<div class="col p-0 mr-3">
<div class="form-group">
Company <%= f.label :name %>
<%= f.text_field :name, :placeholder => 'Company name', class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :location %>
<%= f.text_field :location, :placeholder => 'Location', class: 'form-control' %>
</div>
</div>
<div class="col p-0">
<%= f.fields_for users_attributes do |user_f| %>
<div class="form-group">
<%= user_f.label :email %>
<%= user_f.text_field :email, :placeholder => 'Your Email Address', class: 'form-control' %>
</div>
<div class="form-group">
<%= user_f.label :password %>
<%= user_f.password_field :password, :placeholder => 'Password', class: 'form-control' %>
</div>
<% end %>
</div>
<div class="col-12 p-0">
<%= f.submit "Sign-Up", :class => 'btn btn-primary btn-block btn-lg' %>
</div>
</div>
</div>
<% end %>
Use users instead of users_attributes on the form.
Don't forget to define accepts_nested_attributes_for :users in model Company
## new.html.erb
<%= render partial: 'form', locals: { company: #company, users_attributes: :users } %>
I wonder why you didn't put :users into the form directly

using cocoon and checkboxes , I don't know how to recognize checked: true or false in edit

I am new to develop rails project, I have a nested form for submitting Event and Ticket by using gem 'cocoon'.And the form have some check_boxes .
When you submit new , it does not matter, but in edit, you can not put checked in my checkboxes.
How do i do that given my code below?
My error
undefined method `include?' for nil:NilClass
<%= f.check_box :payment_type, { multiple: true ,checked:f.object.payment_type.include?(pay), include_hidden: false }, pay %>
<%= f.label :payment_type, pay, :value => i %>
My Model
class Event < ActiveRecord::Base
has_many :tickets, inverse_of: :event
accepts_nested_attributes_for :tickets, allow_destroy: true
class Ticket < ActiveRecord::Base
belongs_to :event
My Controller
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
def new
#event = Event.new
end
def create
#event = Event.new(event_params)
if #event.save
flash[:notice] = 'wow'
redirect_to #event
else
flash[:notice] = 'oh..'
render 'new'
end
end
def edit
end
def update
if #event.update(event_params)
redirect_to #event
else
render 'edit'
end
end
My form
<div class="nested-fields">
<h3>tickets</h3>
<div class="row formsection text-center">
<div class="col-md-3">
<span class="label-title">fee</span>
</div>
<div class="col-md-9">
<div class='input-group'>
<%= f.text_field :fee, :placeholder => "fee" , :class => "form-control" %>
</div>
</div>
</div>
<div class="row formsection text-center">
<div class="col-md-3">
<span class="label-title">payment</span>
</div>
<div class="col-md-9">
<%= f.hidden_field :id %>
<% payments = { 'paypal' => 1, 'credit card' => 2 } %>
<% payments.each do |pay,i| %>
<%= f.check_box :payment_type, { multiple: true ,checked: f.object.payment_type.include?(i), include_hidden: false }, i %>
<%= f.label :payment_type, pay, :value => i %>
<br>
<% end %>
</div>
</div>
<%= link_to_remove_association 'remove this', f %>
</div>
Not sure why it works on new, but if payment_type is not yet set, and so is nil, this will give the error you showed.
So instead write something like:
<%- checked_value = f.object.payment_type.present? && f.object.payment_type.include?(i) %>
<%= f.check_box :payment_type, { multiple: true ,checked: checked_value, include_hidden: false }, i %>

Rails + Devise: Undefined Local Variable in UserRegistration#Edit

UPDATE:
I needed to put my controller code ABOVE super, like so and then I had access to the #company instance variable in the view:
User Registration Controller:
class UserRegistrationController < Devise::RegistrationsController
before_action :configure_permitted_parameters
skip_before_filter :load_customer_access, only: [:new, :create]
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
end
def edit
#customer = current_user.customer
#company = Company.find_by_domain(User.get_domain(#customer.email))
super
end
end
I want to pass through the Company ID of the currently logged-in user so I can then view all the users from the same company.
This page will be accessed via a button from the Edit Account page (automatically generated view with Devise). This is the page in which I get the below error:
Note: I only see this error with the button to 'Manage Users' page. If I comment out that line and look at my logs, I can see this fine:
User Model:
...
def self.get_domain(email_address)
email_address.gsub(/.+#([^.]+.+)/, '\1')
end
def confirm!
current_customer = Customer.where(email: self.email).first
super
end
def ensure_has_customer
customer = self.customer
company = Company.find_by_domain(User.get_domain(self.email))
end
...
User Registration Controller:
class UserRegistrationController < Devise::RegistrationsController
before_action :configure_permitted_parameters
skip_before_filter :load_customer_access, only: [:new, :create]
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :current_password, :password, :password_confirmation, :email) }
end
def edit
super
#customer = current_user.customer
#company = Company.find_by_domain(User.get_domain(#customer.email))
puts "HAPPY CUSTOMER"
puts #customer.email
puts "HAPPY COMPANY"
puts #company.id // See above screenshot for logs
end
end
Edit User Route:
match "company/:company_id/edit_users" => 'company#edit_users', via: [:get], :as => :edit_company_users
_edit_account.html.erb:
<%= form_for(current_user, :as => :user, :url => registration_path(:user), :html => { :method => :put }) do |f| %>
<div>
<h1 class="login-header">My Account</h1>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.text_field :first_name, :class => 'form-control custom-form-control', :placeholder => 'First name' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.text_field :last_name, :class => 'form-control custom-form-control', :placeholder => 'Last name' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.email_field :email, :autocomplete => "off", :class => 'form-control custom-form-control', :placeholder => 'Email' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.password_field :password, :autocomplete => "off", :class => 'form-control custom-form-control', :placeholder => 'New password' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.password_field :password_confirmation, :autocomplete => "off", :class => 'form-control custom-form-control', :placeholder => 'New password confirmation' %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="input-group col-centered input-group-lg">
<%= f.password_field :current_password, :class => 'form-control custom-form-control', :placeholder => 'Current password' %>
</div>
</div>
</div>
<br>
<div class="form-actions">
<button type="submit" class="btn-account-primary">Update Account</button>
</div>
<% end %>
<% if #customerAccess.admin_role %>
<hr>
<%= link_to 'Manage Users', edit_company_users_path(#company), :class => 'btn btn-sm btn-default' %><br>
<% end %>
The code for my Company controller and views can reach the edit_company_users_path ok, and the Company controller code is shown below if that's helpful:
Company Controller:
class CompanyController < ApplicationController
def index
#companies = Company.all
respond_to do |format|
format.html
format.json { render json: #companies }
end
end
def edit_users
#company = Company.find(params[:company_id])
#company_name = #company.name
#domain = #company.domain
#find_domain = "%" + #domain + "%"
#users = User.find(:all, :conditions => ["email LIKE ?", #find_domain])
end
end
Company Index View:
<div class="col-md-12">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="page-header">
<h1>Manage Companies</h1>
</div>
<table class = "table table-striped table-bordered">
<tr>
<th>Company Name</th>
<th>Domain</th>
<th>Action</th>
</tr>
<% #companies.each do |company| %>
<tr>
<td><%= company.name %></td>
<td><%= company.domain %></td>
<td>
<%= link_to 'Manage Company Access', edit_company_path(company), :class => 'btn btn-sm btn-default' %>
<%= link_to 'Manage Applications', edit_company_applications_path(company), :class => 'btn btn-sm btn-default' %>
<%= link_to 'Manage Users', edit_company_users_path(company), :class => 'btn btn-sm btn-default' %><br>
</td>
</tr>
<% end %>
</table>
</div>
</div>
</div>
What's interesting to note is that when I do <%= #company %> in the view, no name of the Company appears (though the ID is 2, and name is 'Ryan Drake'), and displays ok in the view. When I do <%= #company.id %>, it throws "undefined method 'id' for nil:class".
Any guidance about accessing the company ID from the Edit Account Devise view in the easiest way possible, so I can pass that through as a parameter and hit the method would be superb.
In the code you show, there is an end and a if, I think you are probably closing the "each" loop before the line of the error, so the "company" variable is not found.
EDIT:
I see two potential problems:
It is definitely #company in edit_company_users_path(#company). If
it doesn't work, does it show the same error? Please show that.
I think you might be messing with devise routes in the alias
"edit_company_users_path", but I don't know how are you organizing
your resources. If you are nesting companies under users, the
default route for that would be something like
edit_company_users_path(#company, current_user). Run rake routes if
you get a routing error.

How to implement upload photo feature in rails?

My repo: https://github.com/Gtar69/artstore_hw2
I want to implement upload more than 1 photo in certain product.
The basic idea is that administrator enter edit page of the product and can "upload new photos" for certain product.Im struggling in "how to create a photo object in edit page and add this photo object to product. !!! I can not successfully create a photo object in edit page .....
Very Thanks
in view/admin/products/edit.html.erb
<h2> 編輯 <%= #product.title %> </h2>
<%= simple_form_for [:admin, #product ] , :html => { :class => "form form-horizontal" } do |f| %>
<div class="form-group">
<div class="col-sm-10">
<%= f.input :title %>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<%= f.input :description, :as => :text %>
</div>
</div>
<div class="form-group">
<div class="col-sm-1">
<%= f.input :quantity %>
</div>
</div>
<div class="form-group">
<div class="col-sm-1">
<%= f.input :price %>
</div>
</div>
<!-- 20140623 upload more than one photo feature added -->
<div>
<%= simple_form_for [:admin, #product, #product.photos.build] do |f| %>
<%= f.input :image , :as => :file %>
<div class="form-group">
<%= f.submit "upload new photos", :class => "btn btn-default" , :disable_with => 'Submiting...' %>
</div>
<%end%>
</div>
<div class="form-group">
<%= f.submit "Submit", :class => "btn btn-default" , :disable_with => 'Submiting...' %>
</div>
<% end %>
in controllers/admin/products_controller.rb
def edit
#product = Product.find(params[:id])
##photo = #product.photos.build
##product.add_photo_to_product(#photo)
end
in controllers/admin/photos_controller.rb
def create
#binding.pry
#product = Product.find(params[:product_id])
#photo = #product.photos.create(photo_params)
#product.add_photo_to_product(#photo)
redirect_to admin_products_path
end
private
def photo_params
params.require(:photo).permit(:image)
end
in model/product.rb
class Product < ActiveRecord::Base
has_many :photos
accepts_nested_attributes_for :photos
validates :title , :presence => true
validates :quantity , :presence => true
def default_photo
photos.first
end
def add_photo_to_product(photo)
photos << photo
end
end
in photos.rb
class Photo < ActiveRecord::Base
belongs_to :product
mount_uploader :image, ImageUploader
end

Rails create two objects from one form

I have two models with a has_one relationship between them. The Event class which "has_one" Location. What I am trying to achieve is in the form where I create an event, I have a "Where?" field which is populated asynchronously through the Foursquare API.
Since I could not possibly migrate the whole of the Foursquare API to my own database, I want to generate a Location entry in my database the first time a location is mentioned in an event.
At the moment the result I am looking for is completed, but I think I am using to many hidden fields and a lot of javascript assignments. Is there a better practice than the one I am using to achieve the same result?
new.html.erb
<%= form_for :event, :url=> new_event_path, :method => :get, :remote => :true, :html => {:class => 'form-horizontal'} do |f| %>
<%= fields_for :location do |f1|%>
<%= f1.hidden_field :lat, :id=> 'foursquare-latitude' %>
<%= f1.hidden_field :lng, :id => 'foursquare-longitude' %>
<%= f1.hidden_field :foursquare_id, :id =>'foursquare-id' %>
<%= f1.hidden_field :name, :id =>'location-name-field' %>
<% end %>
<!-- TODO Encapsulate in location model -->
<%= f.hidden_field :latitude, :id=>'latitude-field' %>
<%= f.hidden_field :longitude, :id=>'longitude-field' %>
<div class="control-group">
<%= f.label :title , :class => "control-label" %>
<div class="controls">
<%= f.text_field :title %>
</div>
</div>
<div class="control-group">
<label class="control-label" for="location">Where?</label>
<div class="controls">
<input type="text" id="location" data-provide="typeahead">
<br/>
<img src="../img/fsq.png" alt="Powered By Foursquare" width="240px" />
</div>
</div>
<div class="control-group">
<%= f.label :body, :class => "control-label" %>
<div class="controls">
<%= f.text_area :body, :rows => 6 %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.submit %>
</div>
</div>
<% end %>
events controller
def new
#location = Location.new(params[:location])
if #location.save
#event = Event.new(params[:event])
#event.author = current_user
#event.location_id = #location.id
respond_to do |format|
if #event.save
format.js {render :action => "new"}
else
format.js {render :action => "new"}
end
end
end
end

Resources