I am trying to create User using
User.create ({email:"ayzabc66#gmail.com", password:"password",password_confirmation:"password"})
but I keep on getting below error:
/.rvm/gems/ruby-3.0.0/gems/activemodel-7.0.3/lib/active_model/attribute_assignment.rb:51:in `_assign_attribute': unknown attribute 'password' for User. (ActiveModel::UnknownAttributeError)
I have already uncommented gem "bcrypt", "~> 3.1.7" and ran bundle.
This is what my user.rb looks like:
class User < ApplicationRecord
#email :string
#password_digest:string
#password:string
#password_confirmation:string virtual
has_secure_password
end
And my db>migrate looks like:
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :email
t.string :password_digest
t.timestamps
end
end
end
If you run the following in Rails Console, do you get the list of attributes that you would expect?
=> User
If so, I would try uncommenting the lines that you have in your User model and see if that helps, so it would look like this:
class User < ApplicationRecord
email :string
password_digest:string
password:string
password_confirmation:string virtual
has_secure_password
end
I have a rSpec Controller test which fails
the index route will have an :id which does not exist in an index route.
I've clean routes:
resources :products
This is the controller,
class ProductsController < ApplicationController
before_action :set_product, only: [:show]
skip_before_action :authorize_request, only: [:show, :index]
# GET /products
def index
# params here {"controller"=>"products", "action"=>"index", "product"=>{}}
#products = Product.all
render json: #products
end
ActionController::UrlGenerationError: No route matches {
:action=>"show",
:controller=>"products"},
missing required keys: [:id]
Why is "show" called? I did not passed any params to the controller:
This is the spec:
RSpec.describe "/products", type: :request do
describe " GET /index" do
it " renders a successful response " do
Fabricate.times(3, :product)
get "/products", headers: valid_headers
expect(response).to be_successful
end
end
end
When I change the routes from get "/products", to: 'products#index' and comment out resources :products then it passes the test
EDIT / PROBLEM FOUND :
I use include Rails.application.routes.url_helpers in my Product model, which caused this issue. I need it to generate URLs to retrieve my attachments. How else can I get the URLs of ActiveStorage ?
I solved the problem: I had include Rails.application.routes.url_helpers in my Product model which caused the problem:
class Product < ApplicationRecord
# include Rails.application.routes.url_helpers
end
commented out, all specs are passing now
But I still don't understand why I can't use it in my Model.
I wanna have it included to retrieve urls of my attachments:
def main_image_thumb_url
rails_blob_url(main_image, host: "localhost:3000")
end
I'm trying to use a partial via ActionCable as follows:
#app/channels/comments_channel.rb
class CommentsChannel < ApplicationCable::Channel
def self.broadcast(comment)
broadcast_to comment.post, comment:
CommentsController.render(partial: 'comments/comment', locals: { comment: comment})
end
def subscribed
stream_for Post.last
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
here is the JS part:
#app/javascript/comments_channel.js
import consumer from "./consumer"
consumer.subscriptions.create("CommentsChannel", {
connected() {
// Called when the subscription is ready for use on the server
},
disconnected() {
// Called when the subscription has been terminated by the server
},
received(data) {
const commentsId = document.getElementById('comments');
commentsId.append(data.comment);
}
});
callin it in the CommentsController:
#app/controllers/comments_controller.rb
...
def create
comment = #post.comments.create! comments_params
CommentsMailer.submitted(comment).deliver_later
CommentsChannel.broadcast(comment)
redirect_to #post
end
and finally, the comments/_comment partial:
#app/views/comments/_comment.html.erb
<p><%= comment.body%>--<%= comment.created_at.to_s(:long)%></p>
It works but displays HTML tags when posting a new comment. If I reload the page, no HTML tags displayed:
What am I missing?
The solution that worked for me was to use insertAdjacentHTML as follows in the comments_channel.js:
#app/javascripts/channels/comments_channel.js
import consumer from "./consumer"
consumer.subscriptions.create("CommentsChannel", {
...
received(data) {
const commentsId = document.getElementById('comments');
commentsId.insertAdjacentHTML('beforeend', data.comment);
}
Hope this helps.
I'm trying to upload multiple files on a form. I followed the rail casts for paperclip and nested attributes, and also this tutorial http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/ but I can't seem to get it working...
I've also searched here in stack overflow, looked at all paperclip and nested attributes posts, but I can't seem to find my answer, it seems that I'm doing everything right...
What happens is that when I submit the form, it creates the ad (it's an ad app), it says everything is ok, but it doesn't write the image data to the database and also it doesn't upload the files...
So I have the Classified model:
class Classified < ActiveRecord::Base
has_many :classified_images, :dependent => :destroy
accepts_nested_attributes_for :classified_images, :reject_if => lambda { |t| t['classified_image'].blank? }
attr_accessible :classified_images_attributes, :access, :contact, :price, :bizType
end
Then, the Classified_Image model:
class ClassifiedImage < ActiveRecord::Base
belongs_to :classified
has_attached_file :photo, :styles => {:small => "150x150>", :large => "320x240>"},
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/classifieds/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
attr_accessible :caption, :photo
end
On the Classified controller, on the "new" part, I have:
def new
#classified = Classified.new
3.times { #classified.classified_images.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: #classified }
end
end
On the "_form" I have:
<%= form_for #classified, :html => { :multipart => true } do |f| %>
...
<%= f.fields_for :classified_images do |builder| %>
<%= render 'image_fields', :f => builder %>
<% end %>
On the "image_fields" partial I have:
<% if f.object.new_record? %>
<li>
<%= f.label :caption %>
<%= f.text_field :caption %>
<%= f.label :photo %>
<%= f.file_field :photo %>
</li>
<% end %>
On the migration files I have:
class AddAttachmentPhotoToClassifiedImages < ActiveRecord::Migration
def self.up
add_attachment :caption, :classified_id, :photo
end
def self.down
drop_attached_file :caption, :classified_id, :photo
end
end
class CreateClassifiedImages < ActiveRecord::Migration
def change
create_table :classified_images do |t|
t.string :caption
t.integer :classified_id
t.timestamps
end
end
end
On the "development.rb" file I have:
Paperclip.options[:command_path] = "/usr/local/bin/"
Paperclip.options[:log] = true
Here's an example of the log when I commit the form:
Started POST "/classifieds" for 127.0.0.1 at 2013-05-19 23:39:43 +0100
Processing by ClassifiedsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"978KGJSUlmMEvr6Tysg5xYIEQzNLn5vod07g+Z7njkU=", "classified"=>{"contact"=>"918218338", "price"=>"1500", "access"=>"bons", "classified_images_attributes"=>{"0"=>{"caption"=>"teste", "photo"=>##original_filename="064_dont-count-the-days.jpg", #content_type="image/jpeg", >#headers="Content-Disposition: form-data; name=\"classified[classified_images_attributes][0][photo]\"; filename=\"064_dont-count-the-days.jpg\"\r\nContent-Type: image/jpeg\r\n", >#tempfile=#3954-11t04t>>}, "1"=>{"caption"=>""}, "2"=>{"caption"=>""}}}, "commit"=>"Criar novo >Classificado"}
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 19 May 2013 22:39:43 UTC >+00:00]]
(0.8ms) commit transaction
Redirected to localhost:3000/classifieds/8
Completed 302 Found in 5ms (ActiveRecord: 1.4ms)
As you can see, it inserts into the "classifieds" table but not into the "classifieds_image" table, and also, I don't get any info from paperclip...
Sorry for all the code but this should be really something simple that i'm not seeing and as more information you've got, the better you can help me... Please let me know if you need any more code or info...
We spent days chasing a similar problem. In the end it was the :reject_if lambda for the accepts_nested_attributes_for call in the model that triggered in the wrong situations.
Now that I revisit the question, it seems that you have the same issue. Instead of:
:reject_if => lambda { |t| t['classified_image'].blank? }
you should probably have:
:reject_if => lambda { |t| t['photo'].blank? }
i.e. the name of the paperclip attribute instead of the nesting model.
It is a frustrating thing to get wrong since it fails silently, t['classified_image'] will be nil all the time and your attributes will be rejected as specified. :) At least we learned to be more careful with :reject_if...
I am trying to set up a custom crop action with Rails Admin.
I have the action set up, but am having an issue passing the cropping coordinates to save the cropped image. I want to pass the 'crop_x', etc. attributes in the form, but for some reason they are not being passed. Perhaps my form is set up wrong?
Here is my form code:
= form_for #object, method: :put, :url => crop_path(:model_name => #abstract_model.to_param, :id => #object.id) do |f|
- %w[x y w h].each do |attribute|
= f.text_field "crop_#{attribute}", id: "crop_#{attribute}"
.form-actions
= f.submit 'Crop Image', class: 'btn btn-primary'
Here is my rails admin custom cropping action:
require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'
module RailsAdminCrop
end
module RailsAdmin
module Config
module Actions
class Crop < RailsAdmin::Config::Actions::Base
RailsAdmin::Config::Actions.register(self)
register_instance_option :member? do
true
end
register_instance_option :http_methods do
[:get, :put]
end
register_instance_option :visible? do
authorized? && (['Painting'].include? bindings[:abstract_model].model.to_s)
end
register_instance_option :bulkable? do
false
end
register_instance_option :pjax? do
true
end
register_instance_option :controller do
Proc.new do
if request.get? # show crop page
respond_to do |format|
format.html { render #action.template_name }
format.js { render #action.template_name, :layout => false }
end
elsif request.put? # Submit
Rails.logger.warn "\n=========================\n"
Rails.logger.warn #object.inspect
#object.image.recreate_versions!
flash[:notice] = "#{#object.title} successfully cropped."
redirect_to "/admin/#{#abstract_model.to_s.downcase}"
end
end
end
register_instance_option :link_icon do
'icon-th-large'
end
end
end
end
end
Here are my rails admin routes (they don't seem to add routes to any of my existing actions, so these are just the default routes):
Routes for RailsAdmin::Engine:
dashboard GET / rails_admin/main#dashboard
index GET|POST /:model_name(.:format) rails_admin/main#index
new GET|POST /:model_name/new(.:format) rails_admin/main#new
export GET|POST /:model_name/export(.:format) rails_admin/main#export
bulk_delete POST|DELETE /:model_name/bulk_delete(.:format) rails_admin/main#bulk_delete
history_index GET /:model_name/history(.:format) rails_admin/main#history_index
bulk_action POST /:model_name/bulk_action(.:format) rails_admin/main#bulk_action
show GET /:model_name/:id(.:format) rails_admin/main#show
edit GET|PUT /:model_name/:id/edit(.:format) rails_admin/main#edit
delete GET|DELETE /:model_name/:id/delete(.:format) rails_admin/main#delete
history_show GET /:model_name/:id/history(.:format) rails_admin/main#history_show
show_in_app GET /:model_name/:id/show_in_app(.:format) rails_admin/main#show_in_app