Related
all!
I am following along with the video on GoRails on how to make a chat application using action cable. In the video, I believe Rails 5 is being used, however, I wanted to try it out with Rails 6.
Everything was going great so far. Installed Bootstrap and jQuery and properly configured my environments.js file. It was awesome. Then I get to the part right before we add action cable, and we make the chatroom form remote: true.
I cannot figure out why my form is trying to still submit as HTML instead of JS. On top of that, I am getting an ActionController::InvalidAuthenticityToken error.
Here's how I have my form set up:
<%= simple_form_for [#chatroom, Message.new], remote: true, html: { id: 'message-input' } do |f| %>
<%= f.input :body, label: false, input_html: { rows: 1, autofocus: true } %>
<% end %>
When I submit, I get this error in my rails server:
Started POST "/chatrooms/1/messages" for ::1 at 2019-10-16 21:02:01 -0500
Processing by MessagesController#create as HTML
Parameters: {"message"=>{"body"=>"test3"}, "chatroom_id"=>"1"}
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms | Allocations: 968)
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
Started POST "/__better_errors/cf3c4e5c6fa2d1b1/variables" for ::1 at 2019-10-16 21:02:01 -0500
Not sure what to do. I've googled and came across this but I'm not sure if it's entirely applicable: Using Rails-UJS in JS modules (Rails 6 with webpacker)
Any help would be greatly appreciated!
So, it turns out I had to use form_with instead of form_for in order to get it to process as JS. I guess form_for is slowing being deprecated?
My form now looks like this:
<%= form_with(model: [#chatroom, Message.new]) do |f| %>
<%= f.text_field :body %>
<% end %>
and the output is what I was expecting with out having to submit the CSRF token with JS or skipping the before action in the controller.
Started POST "/chatrooms/1/messages" for ::1 at 2019-10-18 20:15:14 -0500
Processing by MessagesController#create as JS
Parameters: {"authenticity_token"=>"xPBQ+4LOS5aMa7PQ3HEGXAMX6wIIEfQ0Izy/xUqUtleK4mB18IYxC4mOcIoiS5M+FGm6J/WEYMdbM4IVPojScw==", "message"=>{"body"=>"test 5"}, "chatroom_id"=>"1"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Chatroom Load (0.3ms) SELECT "chatrooms".* FROM "chatrooms" WHERE "chatrooms"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/messages_controller.rb:19:in `set_chatroom'
(0.1ms) BEGIN
↳ app/controllers/messages_controller.rb:12:in `create'
Message Create (0.7ms) INSERT INTO "messages" ("chatroom_id", "user_id", "body", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["chatroom_id", 1], ["user_id", 1], ["body", "test 5"], ["created_at", "2019-10-19 01:15:14.619853"], ["updated_at", "2019-10-19 01:15:14.619853"]]
↳ app/controllers/messages_controller.rb:12:in `create'
(5.6ms) COMMIT
↳ app/controllers/messages_controller.rb:12:in `create'
Redirected to http://localhost:3000/chatrooms/1
Completed 200 OK in 17ms (ActiveRecord: 7.6ms | Allocations: 4690)
When requesting POST requests rails does authenticity_token check.
In your request from javascript, you'll need to add a header: 'X-CSRF-Token': csrfToken.
You can get csrfToken with the following JS:
const csrfToken = document.querySelector('[name="csrf-token"]').getAttribute('content');
Another solution is to disable verify_authenticity_token, you can add this to your application_controller.rb to disable it or you can add it to controller specific:
skip_before_action :verify_authenticity_token
I have looked at previous SO solutions on accepts_nested_attributes here and here, but I am still getting the error. I am using React as front end and Rails back end. I am trying to create a request to be sent to schedules, and from there to populate to workers.
I am using Rails 5.0.2. I have a schedule, worker, roster models.
//Schedule
has_many :workers, through: :rosters, dependent: :destroy
has_many :rosters
accepts_nested_attributes_for :workers #implement accept_nested_attributes here
//Roster
belongs_to :schedule
belongs_to :worker
//Worker
has_many :schedules, through: :rosters
has_many :rosters
And here is my Schedule controller:
def create
#schedule = Schedule.new(schedule_params)
if #schedule.save
render json: #schedule
else
render json: #schedule, status: :unprocessable_entity
end
end
...
private
def schedule_params
params.permit(:date, :user_id, :workers_attributes => [:worker_id, :name, :phone])
end
Here is the error that I got:
app/controllers/schedules_controller.rb:13:in `create'
Started POST "/api/schedules" for 127.0.0.1 at 2017-05-23 10:30:38 -0700
Processing by SchedulesController#create as */*
Parameters: {"date"=>"2017-05-25T02:00:00.000Z", "user_id"=>1, "workers_attributes"=>{"name"=>"Iggy Test", "phone"=>"1
23-456-7890"}, "schedule"=>{"date"=>"2017-05-25T02:00:00.000Z", "user_id"=>1}}
Unpermitted parameter: schedule
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.0ms)
TypeError (no implicit conversion of Symbol into Integer):
app/controllers/schedules_controller.rb:13:in `create'
Why is my request shows Unpermitted parameter schedule? If I remove workers_attributes and only have params.permit(:date, :user_id), it works. I can't figure out why the error points to schedule. How can I make successful POST nested_attributes request to rails?
I am using fetch to do POST request from react side:
...
return fetch(`api/schedules`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
date: date,
user_id: 1,
workers_attributes: {name: "Iggy Test", phone: "123-456-7890"}
})
EDIT:
After following answer from #gabrielhilal, I added require(): params.require(:schedule).permit(:date, :user_id, :workers_attributes => [:id, :name, :phone]), and edited the fetch POST on React's end to have array of objects instead of plain objects: workers_attributes: [{name: "Iggy Test", phone: "123-456-7890"}]. It does not complain anymore, and it does register new schedule. However, new workers are all nil:
#Worker.last shows:
#<Worker id: 32, name: nil, phone: nil, created_at: "2017-05-23 19:36:09", updated_at: "2017-05-23 19:36:09">
Sorry, don't mean to create nested question, but does anyone know why it is nil?
EDIT 2:
I got it to work, sort of.
If I have
def create
#schedule = Schedule.new(schedule_params)
#workers = #schedule.rosters.build.build_worker
...
and
//schedule_params
params.permit(:date, :user_id, :workers_attributes => [:id, :name, :pho
ne])
I was able to have "Iggy Test" to display, but it immediately creates another nil worker.
Log:
Started POST "/api/schedules" for 127.0.0.1 at 2017-05-23 20:42:38 -0700
Processing by SchedulesController#create as */*
Parameters: {"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1, "workers_attributes"=>[{"name"=>"Iggy Test", "phone"=>"
123-456-7890"}], "schedule"=>{"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1}}
Unpermitted parameter: schedule
(0.1ms) begin transaction
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
SQL (1.1ms) INSERT INTO "schedules" ("date", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["date", 20
17-05-26 02:00:00 UTC], ["created_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC], ["user_id", 1]
]
SQL (0.2ms) INSERT INTO "workers" ("name", "phone", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Iggy
Test"], ["phone", "123-456-7890"], ["created_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.2ms) INSERT INTO "rosters" ("worker_id", "created_at", "updated_at") VALUES (?, ?, ?) [["worker_id", 56], ["c
reated_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.1ms) INSERT INTO "workers" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-05-24 03:42:38 UTC
], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.6ms) INSERT INTO "rosters" ("schedule_id", "worker_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["sc
hedule_id", 52], ["worker_id", 57], ["created_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.4ms) UPDATE "rosters" SET "schedule_id" = ?, "updated_at" = ? WHERE "rosters"."id" = ? [["schedule_id", 52],
["updated_at", 2017-05-24 03:42:38 UTC], ["id", 52]]
(5.6ms) commit transaction
Completed 200 OK in 417ms (Views: 6.9ms | ActiveRecord: 14.7ms)
If I modified params to have require(:schedule)
params.require(:schedule).permit(:date, :user_id, :workers_attributes => [:id, :name, :phone])
It creates a nil worker only.
Log:
Started POST "/api/schedules" for 127.0.0.1 at 2017-05-23 20:45:03 -0700
Processing by SchedulesController#create as */*
Parameters: {"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1, "workers_attributes"=>[{"name"=>"Iggy Test", "phone"=>"
123-456-7890"}], "schedule"=>{"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1}}
(0.1ms) begin transaction
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
SQL (0.4ms) INSERT INTO "schedules" ("date", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["date", 20
17-05-26 02:00:00 UTC], ["created_at", 2017-05-24 03:45:03 UTC], ["updated_at", 2017-05-24 03:45:03 UTC], ["user_id", 1]
]
SQL (0.2ms) INSERT INTO "workers" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-05-24 03:45:03 UTC
], ["updated_at", 2017-05-24 03:45:03 UTC]]
SQL (0.3ms) INSERT INTO "rosters" ("schedule_id", "worker_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["sc
hedule_id", 53], ["worker_id", 58], ["created_at", 2017-05-24 03:45:03 UTC], ["updated_at", 2017-05-24 03:45:03 UTC]]
(2.4ms) commit transaction
Completed 200 OK in 81ms (Views: 1.3ms | ActiveRecord: 8.3ms)
Your post:
{
"date"=>"2017-05-25T02:00:00.000Z",
"user_id"=>1,
"workers_attributes"=>{
"name"=>"Iggy Test",
"phone"=>"123-456-7890"
},
"schedule"=> {
"date"=>"2017-05-25T02:00:00.000Z",
"user_id"=>1
}
}
You have two issues:
schedule is not permitted (that's why you see the message in the logs), but it will be just ignored anyway (won't raise any error).
the workers_attributes should be a collection of workers and not a simple hash, so that's why you are having the error.
You should get something like the following in the post request:
{
"date"=>"2017-05-25T02:00:00.000Z",
"user_id"=>1,
"workers_attributes"=>{
"0" => {
"name"=>"Iggy Test",
"phone"=>"123-456-7890"
}
}
}
I'm using tinymce-rails-image-upload to upload images with paperclip (following this demo-app). When I try to upload an image I'm getting an 'umpermitted parameters' reminder and the image doesn't upload. The upload modal shows 'Got a bad response from server':
Processing by TinymceAssetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"auth token", "hint"=>"", "file"=>#<ActionDispatch::Http::UploadedFile:0x000001025a2780 #tempfile=# <File:/var/folders/t4/86vsrmds42j84r36kwpng7k00000gn/T/RackMultipart20150207- 12522-9rj6xq>, #original_filename="applecash.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"file\"; filename=\"applecash.jpg\"\r\nContent-Type: image/jpeg\r\n">, "alt"=>""}
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/t4/86vsrmds42j84r36kwpng7k00000gn/T/RackMultipart20150207-12522-9rj6xq[0]' 2>/dev/null
Unpermitted parameters: utf8, authenticity_token
(0.1ms) begin transaction
Question Load (0.5ms) SELECT "questions".* FROM "questions" WHERE (questions.position IS NOT NULL) AND (1 = 1) ORDER BY questions.position DESC LIMIT 1
Binary data inserted for `string` type on column `file_content_type`
SQL (0.8ms) INSERT INTO "questions" ("created_at", "file_content_type", "file_file_name", "file_file_size", "file_updated_at", "position", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Sun, 08 Feb 2015 18:35:07 UTC +00:00], ["file_content_type", "image/jpeg"], ["file_file_name", "timcook.jpg"], ["file_file_size", 120040], ["file_updated_at", Sun, 08 Feb 2015 18:35:07 UTC +00:00], ["position", 9], ["updated_at", Sun, 08 Feb 2015 18:35:07 UTC +00:00]]
(7.3ms) commit transaction
Completed 200 OK in 68ms (Views: 0.6ms | ActiveRecord: 8.7ms)
Here's the controller:
class TinymceAssetsController < ApplicationController
respond_to :json
def create
geometry = Paperclip::Geometry.from_file params[:file]
question = Question.create params.permit(:file, :alt, :hint)
render json: {
question: {
url: question.file.url,
height: geometry.height.to_i,
width: geometry.width.to_i
}
}, layout: false, content_type: "text/html"
end
end
and the question model:
class Question < ActiveRecord::Base
has_attached_file :file
end
and the view:
<%= simple_form_for [#comment, Question.new] do |f| %>
<%= f.text_area :body, :class => "tinymce", :rows => 10, :cols => 60 %>
<% end %>
<%= tinymce plugins: ["uploadimage"] %>
It's ok to not permit some of the parameters, so Unpermitted parameters: utf8, authenticity_token is a reminder, not an exception. I suggest you log what are question model instance errors:
question = Question.create params.permit(:file, :alt, :hint)
logger.debug question.errors.full_messages
May be wrong, but, you have the following:
question = Question.create params.permit(:file, :alt, :hint)
I think you'd just make that read like this and it would work:
question = Question.create params.permit(:file, :alt, :hint, :utf8, :authenticity_token)
The error is clear. You have not permitted those parameters. But they exist. You expressly permit the other three there so I presume you have to permit those two additional.
That's my guess and I'm sticking to it :).
I have a multiselect (using bootstrap-multiselect) in my #minisets new form that aims to associate #scales with the #miniset via the #sizes table.
The associations work fine. What I'm stuck on is how to loop through the multiple :scale_id submissions from the multiselect and create lines in the #sizes table for them all.
Following this answer I have been trying to use split and then loop the create but I think the fact that that answer pertains to a HABTM relationship and mine is has_many_through means I need a different solution?
In my minisets controller I have
def new
#miniset = Miniset.new
#miniset.sizes.build
end
def create
#miniset = Miniset.new(miniset_params)
if #miniset.save
params[:scale_id].split(',').each do |id|
#miniset.sizes.create(params[:sizes_attributes])
end
redirect_to #miniset
else
render 'new'
end
end
private
def miniset_params
params.require(:miniset).permit(:name, :release_date, :material, :pcode, :notes, :quantity, :random, productions_attributes: [:id, :manufacturer_id, :miniset_id], sizes_attributes: [:id, :scale_id, :miniset_id], sculptings_attributes: [:id, :sculptor_id, :miniset_id])
end
end
In my view I have
<%= f.fields_for :sizes do |size_fields| %>
<%= size_fields.label :scale_id, simple_pluralize(#miniset.scales.count, 'Scale') %>
<%= size_fields.select :scale_id,
options_from_collection_for_select(Scale.all, :id, :name, #miniset.scales.map(&:id)),
{},
{class: 'multiselect', multiple: true} %>
<% end %>
<script type="text/javascript">
$(document).ready(function() {
$('.multiselect').multiselect();
});
</script>
I'm currently getting error undefined methodsplit' for nil:NilClass` when I submit.
I think that may be because the log shows an empty scale_id passed before the two filled ones and split won't accept nil? Here is the log when submitting TWO scales.
Started POST "/minisets" for 127.0.0.1 at 2014-01-30 10:49:59 +0000
Processing by MinisetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R0RxDMwB5/ytSb5qgjIlVR5as0/DTkstgFMDXcefDnc=", "miniset"=>{"name"=>"Test for size", "quantity"=>"10", "random"=>"0", "material"=>"Hard Plastic", "sizes_attributes"=>{"0"=>{"scale_id"=>["", "1", "5"]}}, "pcode"=>"", "release_date(1i)"=>"", "release_date(2i)"=>"", "release_date(3i)"=>"", "notes"=>""}, "Set Scale"=>{"#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Size:0x007fcf643c29f0>"=>""}, "commit"=>"Add set"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'd59f28d384d62b71719dd845b4e5353cdd993016' LIMIT 1
Unpermitted parameters: scale_id
SQL (0.9ms) INSERT INTO "minisets" ("created_at", "material", "name", "notes", "pcode", "quantity", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00], ["material", "Hard Plastic"], ["name", "Test For Size"], ["notes", ""], ["pcode", ""], ["quantity", 10], ["updated_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00]]
SQL (0.6ms) INSERT INTO "sizes" ("created_at", "miniset_id", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00], ["miniset_id", 41], ["updated_at", Thu, 30 Jan 2014 10:49:59 UTC +00:00]]
(4.2ms) commit transaction
Completed 500 Internal Server Error in 153ms
NoMethodError (undefined method `split' for nil:NilClass):
app/controllers/minisets_controller.rb:19:in `create'
I'm sure what I have after the split is incorrect but I can't play with it until the split works. I can get rid of the error by adding to_s before the split but I get no better results.
Been making very slow progress on this multiselect for days now so any help very much appreciated.
Thanks to this fantastic youtube video I solved my problem.
My form:
<%= f.fields_for(#size) do |sf| %>
<%= sf.label simple_pluralize(#miniset.scales.count, 'Scale') %>
<%= collection_select( :scales, :id, #all_scales, :id, :name,
{},
{class: 'multiselect', multiple: true}) %>
<% end %>
In my minisets_controller I have the following new and create actions:
def new
#miniset = Miniset.new
#all_scales = Scale.all
#size = #miniset.sizes.build
end
def create
#miniset = Miniset.new(miniset_params)
params[:scales][:id].each do |scale|
if !scale.empty?
#miniset.sizes.build(:scale_id => scale)
end
end
if #miniset.save
redirect_to #miniset
else
render 'new'
end
end
It works perfectly. If anyone else is having the same problem, trying to get multiselects to work in rails with has_many_through, I recommend watching that video. So pleased.
I have a set of parameters below that are submitted through a form
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pyMkh1eJ7WxYC978XKjdsyGOeGDvi6RTIOSGb9KMqkc=", "link"=>{"category_id"=>"1", "comment"=>"", "url"=>"yahoo.com "}, "type"=>"html", "original_url"=>"http://yahoo.com", "url"=>"http://www.yahoo.com/", "title"=>"Yahoo!", "description"=>"Welcome to Yahoo!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and information.", "favicon_url"=>"http://www.yahoo.com/favicon.ico", "provider_url"=>"http://www.yahoo.com", "provider_display"=>"www.yahoo.com", "provider_name"=>"Yahoo", "safe"=>"true", "html"=>"", "thumbnail_url"=>"", "object_type"=>"link", "image_url"=>"", "category_id"=>"1"}
I want to create a new "links" record in the links model, which belongs to the categories model. My "create" action in the links controller looks like this
def create
#category = Category.find_by_id(params[:category_id])
#link = #category.links.build(params[:link])
#link.user_id = current_user.id
respond_to do |format|
if #link.save
links_attributes = params.slice(:original_url, :title, :description, :favicon_url, :provider_url, :provider_display, :thumbnail_url, :object_type)
#link.update_attributes(links_attributes)
else
end
end
end
If I just did #link.save without the update attributes, it only saves comment, url and category id. However, the above creates 2 records, one comment, url and category_id and another with all of the data.
How can I make sure this creates just one record, with all of the information?
UPDATE
If I could just create the record with the 3 parameters, then update it with the remaining parameters (links_attributes), I would be fine with that...just not sure how to do it.
This is the output I get when submitting:
Started POST "/categories/1/links" for 127.0.0.1 at 2013-01-11 12:43:44 -0500
Processing by LinksController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pyMkh1eJ7WxYC978XKjdsyGOeGDvi6RTIOSGb9KMqkc=", "link"=>{"category_id"=>"1", "comment"=>"", "url"=>"bloomberg.com "}, "type"=>"html", "original_url"=>"http://bloomberg.com", "url"=>"http://www.bloomberg.com/", "title"=>"Business, Financial & Economic News, Stock Quotes", "description"=>"Bloomberg is a premier site for business and financial market news. It delivers world economic news, stock futures, stock quotes, & personal finance advice.", "favicon_url"=>"http://www.bloomberg.com/favicon.ico", "provider_url"=>"http://www.bloomberg.com", "provider_display"=>"www.bloomberg.com", "provider_name"=>"Bloomberg", "safe"=>"true", "html"=>"", "thumbnail_url"=>"http://www.bloomberg.com/image/is2KySnyVWmA.jpg", "object_type"=>"link", "image_url"=>"", "category_id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "links" ("category_id", "comment", "created_at", "description", "favicon_url", "object_type", "original_url", "points", "profile_link", "provider_display", "provider_url", "thumbnail", "thumbnail_url", "title", "updated_at", "url", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["category_id", "1"], ["comment", ""], ["created_at", Fri, 11 Jan 2013 17:43:
Started POST "/categories/1/links" for 127.0.0.1 at 2013-01-11 12:43:44 -0500
Processing by LinksController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pyMkh1eJ7WxYC978XKjdsyGOeGDvi6RTIOSGb9KMqkc=", "link"=>{"category_id"=>"1", "comment"=>"", "url"=>""}, "commit"=>"Post", "category_id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.4ms) INSE44 UTC +00:00], ["description", "Bloomberg is a premier site for business and financial market news. It delivers world economic news, stock futures, stock quotes, & personal finance advice."], ["favicon_url", "http://www.bloomberg.com/favicon.ico"], ["object_type", "link"], ["original_url", "http://bloomberg.com"], ["points", nil], ["profile_link", nil], ["provider_display", "www.bloomberg.com"], ["provider_url", "http://www.bloomberg.com"], ["thumbnail", nil], ["thumbnail_url", "http://www.bloomberg.com/imRT INTO "links" ("category_id", "comment", "created_at", "description", "favicon_url", "object_type", "original_url", "points", "profile_link", "provider_display", "provider_url", "thumbnail", "thumbnail_url", "title", "updated_at", "url", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["category_id", "1"], ["comment", ""], ["created_at", Fri, 11 Jan 2013 17:43:44 UTC +00:00], ["description", nil], ["favicon_url", nil], ["object_type", nil], ["original_url", nil], ["points", nil], ["profile_link", nil], ["provider_display", nil], ["provider_url", nil], ["thumbnail", nil], ["thumbnail_url", nil], ["title", nil], ["updated_at", Fri, 11 Jan 2013 17:43:44 UTC +00:00], ["url", ""], ["user_id", 1]]
(1.1ms) commit transaction
Rendered links/create.js.erb (0.0ms)
Completed 200 OK in 8ms (Views: 3.9ms | ActiveRecord: 1.6ms)
age/is2KySnyVWmA.jpg"], ["title", "Business, Financial & Economic News, Stock Quotes"], ["updated_at", Fri, 11 Jan 2013 17:43:44 UTC +00:00], ["url", "bloomberg.com "], ["user_id", 1]]
(2.5ms) commit transaction
Rendered links/create.js.erb (0.0ms)
This is my form:
<%= form_for([#category, #category.links.build], :remote => true, :class => "form-horizontal") do |f| %>
<%= f.hidden_field :category_id, :value => params[:id] %>
Comment: <%= f.text_field :comment %><BR>
Link: <%= f.text_field :url %>
<%= f.submit "Post", :class => "btn", :disable_with => '...', :id => "new_link_button" %>
<% end %>
<div class="selector" style="width:350px;margin:-30px 0px 0px 0px;"></div>
<!-- Placeholder that tells Preview where to put the loading icon-->
<div class="loading">
<img src='http://embedly.github.com/jquery-preview/images/loading-rectangle.gif'>
</div>
<script>
$('#link_url').preview({ key:'60f1dcdf3258476794784148a6eb65e7', // Sign up for a key: http://embed.ly/pricing
selector : {type:'rich'},
preview : {
submit : function(e, data){
$.ajax({
dataType: 'script',
url: this.form.attr('action'),
type: 'POST',
data: data
});
},
},
autoplay : 0,
maxwidth : 350,
display : {display : 'rich'}
});
$('#new_link_button').click(function(e) {
e.preventdevault();
$('.new_link').submit();
return false;
});
</script>
This is my routes:
resources :categories, :only => [:new, :show, :create, :edit, :update] do
resources :links, :only => [:new, :show, :create, :edit, :update]
resources :industries, :only => [:new, :show, :create, :edit, :update]
resources :territories, :only => [:new, :show, :create, :edit, :update]
end
Try setting them before save.
def create
#link = Link.new(params[:link])
#link.category_id = params[:category_id]
#link.original_url = params[:original_url]
#link.title = params[:title]
#link.description = params[:description]
#link.user_id = current_user.id
# etc...
respond_to do |format|
if #link.save
# do things...
else
# do other things...
end
end
end
I think there's a problem with how your form is being constructed. As you can see in your posted parameters, params[:link] only includes a subset of the data you are trying to save on link:
"link"=>{"category_id"=>"1", "comment"=>"", "url"=>"yahoo.com "}
The rest is all at the top level of the params array:
Parameters: {... "original_url"=>"http://yahoo.com", "url"=>"http://www.yahoo.com/", "title"=>"Yahoo!", "description"=>"Welcome to Yahoo!..." ...}
You should look at how you're using form_for to construct your form, and make sure it's being used correctly for the additional parameters.
Edit:
If you can't edit the form, then you could do this in your controller:
#link.user_id = current_user.id
links_attributes = params.slice(:original_url, :title, :description, :favicon_url, :provider_url, :provider_display, :thumbnail_url, :object_type)
#link.attributes = links_attributes
respond_to do |format|
if #link.save
...
First of all, you don't need to call #link.save then #link.update_attributes, because #link.update_attributes would save #link in your db if it is not already there.
From the logs you posted, it seems your controller receives two post requests, this is why it creates two objects, I think the reason, is that two submits happen:
one by the form that is submitted and the other one by the ajax request inside your $('#link_url').preview(... code.