So I've been at this a while and have tried a bunch of different methods. Essentially I have an over-arching property model and an associated deed model. From the properties#show view I have it displaying all associated deeds. I have a button in my property view that displays a modal (using AJAX) to create a deed associated to said model. All the steps work (even 'edit' and 'update' using AJAX from the properties view) except when trying to save the new deed I get an error message:
This form contains 1 error. Property must exist
I've tried passing the #properties.id through in different ways and I just can't seem to get it. I'll list all the relevant info I have and if more is needed just let me know.
Property Model
class Property < ApplicationRecord
has_many :deeds, dependent: :destroy
accepts_nested_attributes_for :deeds
end
Deeds Model
class Deed < ApplicationRecord
belongs_to :property
accepts_nested_attributes_for :property
end
Deeds Controller (Included edit and update actions)
def new
#deeds = Deed.new
#deeds.build_property
respond_to do |format|
format.html { render 'new'}
format.js
end
end
def create
#properties = Property.find(params[:deed][:property_id])
#deeds = #properties.deeds.new(deed_params)
if #deeds.save
flash[:success] = "Deed created successfully!"
respond_to do |format|
format.html { redirect_to deeds_path }
format.js
end
else
render 'new'
end
end
def edit
#deeds = Deed.find(params[:id])
respond_to do |format|
format.html { #deeds.save }
format.js
end
end
def update
#deeds = Deed.find(params[:id])
#properties = Property.find(#deeds.property.id)
#deeds.update_attributes(deed_params)
respond_to do |format|
format.html { redirect_to deeds_path }
format.js
end
end
private
def deed_params
params.require(:deed).permit(:property_id, :deed_number, :deed_context, :consideration, :recorded_date, :grantor, :grantee, :trustee)
end
new deeds link from properties#show view
<%= link_to fa_icon("plus", text: "Add Deed"), new_property_deed_path(#properties.id), remote: true, class: "btn btn-primary btn-large btn-ouline pull-right", locals: { property_id: #properties } %>
new.js.erb
$('#deeds-modal').modal("show");
$('#deeds-modal').html('<%= j render partial: "deeds/deeds_modal", locals: { property_id: #properties } %>');
_deeds_modal.html.erb partial Form
<div class="modal-body">
<%= bootstrap_form_for(#deeds, layout: :horizontal, remote: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :property_id, value: property_id %>
<%= f.text_field :deed_number, class: 'form-control' %>
<%= f.text_field :deed_context, class: 'form-control' %>
<%= f.text_field :consideration, class: 'form-control' %>
<%= f.text_field :recorded_date, class: 'form-control' %>
<%= f.text_field :grantor, class: 'form-control' %>
<%= f.text_field :grantee, class: 'form-control' %>
<%= f.text_field :trustee, class: 'form-control' %>
<%= f.form_group do %>
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
<% end %>
</div>
server response upon deeds_modal form POST
Started GET "/properties/99/deeds/new" for 127.0.0.1 at 2017-05-30
23:24:48 -0400
Processing by DeedsController#new as JS
Parameters: {"property_id"=>"99"}
Rendering deeds/new.js.erb
Rendered shared/_error_messages.html.erb (0.4ms)
Rendered deeds/_deeds_modal.html.erb (5.6ms)
Rendered deeds/new.js.erb (7.3ms)
Completed 200 OK in 14ms (Views: 10.6ms | ActiveRecord: 0.0ms)
Started POST "/deeds" for 127.0.0.1 at 2017-05-30 23:24:55 -0400
Processing by DeedsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eaj9K...==", "deed"=>{"property_id"=>"", "deed_number"=>"69696969", "deed_context"=>"Bagel slaps", "consideration"=>"Considered", "recorded_date"=>"2017-05-31", "grantor"=>"", "grantee"=>"NipLips", "trustee"=>""}, "commit"=>"Save"}
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Rendering deeds/new.js.erb
Rendered shared/_error_messages.html.erb (0.7ms)
Rendered deeds/_deeds_modal.html.erb (12.1ms)
Rendered deeds/new.js.erb (20.4ms)
Completed 200 OK in 32ms (Views: 30.6ms | ActiveRecord: 0.2ms)
So as you can see I've tried to pass through a local variable from the new link -> new.js.erb -> deeds modal form but I can't seem to pull the property_id into the new deeds object.
Is this even the correct approach for what I'm trying to do or am I just missing something? Hope this wasn't too long of a read and that I provided enough info... lol.
*Upon writing this post I clicked through some of the 'similar questions' on SO, which is where I got the #properties = Property.find(params[:deed][:property_id]) line from but I still get ActiveRecord::RecordNotFound (Couldn't find Property with 'id'=): upon submission. Also, thank you to anyone who takes the time to read this.
rails routes Update (only included relevant routes for brevity)
deeds GET /deeds(.:format) deeds#index
POST /deeds(.:format) deeds#create
new_deed GET /deeds/new(.:format) deeds#new
edit_deed GET /deeds/:id/edit(.:format) deeds#edit
deed GET /deeds/:id(.:format) deeds#show
PATCH /deeds/:id(.:format) deeds#update
PUT /deeds/:id(.:format) deeds#update
DELETE /deeds/:id(.:format) deeds#destroy
...
property_deeds GET /properties/:property_id/deeds(.:format) deeds#index
POST /properties/:property_id/deeds(.:format) deeds#create
new_property_deed GET /properties/:property_id/deeds/new(.:format) deeds#new
edit_property_deed GET /properties/:property_id/deeds/:id/edit(.:format) deeds#edit
property_deed GET /properties/:property_id/deeds/:id(.:format) deeds#show
PATCH /properties/:property_id/deeds/:id(.:format) deeds#update
PUT /properties/:property_id/deeds/:id(.:format) deeds#update
DELETE /properties/:property_id/deeds/:id(.:format) deeds#destroy
This should be you new action
def new
#deeds = Deed.new
#deeds.build_property
#properties = Property.find_by_id(params[:property_id]) # add this line
respond_to do |format|
format.html { render 'new'}
format.js
end
end
Update:
If you are not using the associated property anywhere else, you don't need to pass the locals and I don't suggest hidden_field as it can be easily modified, alternately you can do..
def new
#deeds = Property.find_by_id(params[:property_id]).deeds.new # add this line
#deeds.build_property
respond_to do |format|
format.html { render 'new'}
format.js
end
end
As the error says Property must exist, so, your intention is to print the property_id for a new record, but for some reason that's not happening.
As that value goes from your form_for directly to create a new "deed", then in must be setted on it, you do it, but you don't check if it's printing something or not, and it get's you a nil value that's not allowed because of the relationship between Deeds and Properties.
When you send the form, you can see the value is empty:
"deed"=>{"property_id"=>""}
How do you define which property_id must a new record have? You can create a variable within your new method and set an object to print within your form_for, it'll give you a value to proceed with a new record.
When you make a request to the new method you pass through the params the value for property_id:
Processing by DeedsController#new as JS
Parameters: {"property_id"=>"99"}
So I suppose you could use it instead setting a local variable which hasn't been initialized, maybe you can try with:
<%= f.hidden_field :property_id, value: params[:property_id] %>
Related
I have a 'parent_id' which is successfully passed as parameter into a form page ("Create Child") here :
<li><%= link_to "Create Child", new_block_path(:parent_id => #block.id)%></li>
Logging the parameter gives :
Started GET "/blocks/new?parent_id=7" for ::1 at 2022-10-31 22:01:05 +0000
Processing by BlocksController#new as HTML
Parameters: {"parent_id"=>"7"}
I then have a form which calls a create method here :
def create
#block = Block.new(block_params)
if #block.save
redirect_to #block
else
render :new, status: :unprocessable_entity
end
end
using these block_params
private
def block_params
params.require(:block).permit(:title, :body, :parent_id)
end
end
But when I call the create function only :title and :body are present.
Printing the parameters shows :
Started POST "/blocks" for ::1 at 2022-10-31 22:01:17 +0000
Processing by BlocksController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]",
"block"=>{"title"=>"this is a child of block 7",
"body"=>"this is a child of block 7"}, "commit"=>"Create Block"}
Filtered shows :title and :body are permitted correctly, but the URL parameter of parent_id has just vanished.
The rails guide states : "Submitted form data is put into the params Hash, alongside captured route parameters" - I just can't work this out. The parent_id parameter is there as it should be when the form is loaded, but when submitted it vanishes.
There are two actions, new and create, you could think they are separate.
So params from new won't be available in create automatically (in your case parent_id), you have to pass the params explicitly.
You could just add a hidden input in your form to pass parent_id:
<%= form_with model: #block do |f| %>
<%= f.hidden_field :paren_id, value: params[:parent_id] %>
<%= f.text_field :title %>
<%= f.text_field :body %>
<% end %>
I have an app running rails 6.1.3.2. I'm starting to migrate to use hotwire instead of ujs. My app uses an admin namespace to allow a user to make edits, create items via this. Example route below -
resources :event_attachments
namespace :admin do
resources :event_attachments
end
When making an update via the admin view to an event_attachment, the turbo_stream is not processing the partial in the admin view, instead it's looking in EventAttachmentsController. I'm not sure why it's doing this - any suggestions would be appreciated.
Admin:EventAttachmentsController -
def update
if #event_attachment.update(event_attachment_params)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.update("flash", partial: "shared/flash", locals: { notice: "Event image updated" }),
turbo_stream.replace(:event_attachments, partial: 'admin/pages/event_attachment', locals: { event_attachment: #event_attachment })
]
end
format.html do
redirect_to edit_admin_event_attachment_path(#event_attachment), notice: 'Event image updated'
end
end
else
render :edit, status: :unprocessable_entity
end
end
Here's a sample of my form in admin/event_attachments -
<%= form_for([:admin, event_attachment], :id => dom_id(event_attachment)) do |f| %>
<%= f.text_field :title, :placeholder => "Event image name *" %>
<% end %>
Rails Server Log (as a test, I created a partial in the main event_attachments view) -
Started PATCH "/event_attachments/25" for ::1 at 2022-01-03 22:18:53 -0500
Processing by EventAttachmentsController#update as TURBO_STREAM
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "event_attachment"=>{"title"=>"Test Name", "summary"=>"caption"}, "commit"=>"Save Changes", "id"=>"25"}
EventAttachment Load (1.1ms) SELECT `event_attachments`.* FROM `event_attachments` WHERE `event_attachments`.`id` = 25 LIMIT 1
↳ app/controllers/event_attachments_controller.rb:51:in `set_event_attachment'
Rendered event_attachments/_event_attachment.html.erb (Duration: 0.1ms | Allocations: 22)
[ActionCable] Broadcasting to event_attachments: "<turbo-stream action=\"update\" target=\"event_attachment_25\"><template><li id=\"event_attachment_25\">\n hello\n</li>\n</template></turbo-stream>"
Redirected to http://localhost:3000/event_attachments/25
Completed 302 Found in 4ms (ActiveRecord: 1.1ms | Allocations: 1640)
It looks like the problem here is in the url that your form is sending the request to. If you look in the logs you will see it says processing by EventAttachmentsController#update this is because in your form the URL is wrong. My thoughts is you should change from using form_for and the [] to use form_with
<%= form_with(url: admin_event_attachments_path, :id => dom_id(event_attachment)) do |f| %>
<%= f.text_field :title, :placeholder => "Event image name *" %>
<% end %>
I have a the following form I'm trying to submit (from the show method view):
<%= form_for(Photo.new, :remote => true, html: {multipart: :true}) do |f| %>
<%= f.label :title, 'Title' %>
<%= f.text_field :title %>
<%= f.label :image, 'Choose Image' %>
<%= f.file_field :image %>
<%= f.hidden_field :item_id, :value => #item.id %>
<%= f.submit 'Add' %>
<% end %>
And I'm trying to use the following create method:
def show
#item = Item.find(params[:id])
end
def create
#item = Item.find(params[:item_id])
#photo = Photo.new(photo_params)
redirect_to edit_photos_url, notice: 'Photo uploaded' if #photo.save
end
This is what the logs generate:
Started POST "/photos" for ::1 at 2015-07-03 21:17:10 -0400
Processing by PhotosController#create as HTML
Parameters: {"utf8"=>"✓", "photo"=>{"title"=>"sdb", "image"=># <ActionDispatch::Http::UploadedFile:0x007fe301a2ebf8 #tempfile=#<Tempfile:/var/folders/d8/hx2wgfwx7m77c6mjwx2pffcc0000gq/T/RackMultipart20150703-35083-rt86ai.png>, #original_filename="Screen Shot 2015-06-28 at 9.23.31 PM.png", #content_type="image/png", #headers="Content-Disposition: form-data; name=\"photo[image]\"; filename=\"Screen Shot 2015-06-28 at 9.23.31 PM.png\"\r\nContent-Type: image/png\r\n">, "item_id"=>"27"}, "commit"=>"Add"}
Can't verify CSRF token authenticity
Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound (Couldn't find Item without an ID):
app/controllers/photos_controller.rb:13:in `create'
The item_id seems to be submitted, yet it doesn't work ?
I've tried also implementing sessions, which would be more ideal. I've done the following in the create method of the items controller:
remember_item #item
Calling the method in the helper:
def remember_item(item)
cookies.permanent.signed[:item_id] = item.id
session[:item_id] = item.id
end
I've checked the session variables, and the :item_id did get passed correctly in the session. I've tried the following:
def create
#item = Item.find(session[:item_id])
#photo = #item.photos.build(photo_params)
redirect_to edit_photos_url, notice: 'Photo uploaded' if #photo.save
end
And this also doesn't work, giving the following error:
Couldn't find Item without an ID
I'd really like to get the latter to work. Suggestions?
The item_id is included in the photo params
#item = Item.find params[:photo][:item_id]
Should give you what you want.
If you look closely, what you are submitting is actually
params
=> {photo: {item_id: 1, **other_attrs}}
params[:item_id]
=> nil
params[:photo][:item_id]
=> 1
That is because item_id is in your form as a helper and the form helper thinks it is a property of photo.
I am trying to create lists and each has 2 attributes name and description.Database does create it and save it when using console but not using the website form.When checking the log file I found that website form does not post instead uses gets and is redirected to itself, How do I make the website form POST instead of GET so it gets stored in database.
Log file:
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-09-18 12:35:14 -0400
Served asset /jquery_ujs.js - 304 Not Modified (0ms)
Here is list controller:
def create
#list = Lists.new(params[:lists])
if #list.save
redirect_to #list
else
render 'new'
end
end
def update
if #list.update_attributes(params[:lists])
flash[:success] = "lists updated"
redirect_to #list
else
render 'edit'
end
end
def new
#list = Lists.new
end
This is the form for users to create list
<%= form_for #list, url: newlist_path(#list), html: { method: :put } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
</br>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.submit "Create the List" %>
<% end %>
Your form_for helper is routing to the incorrect action. Try routing to the create action instead:
<%= form_for #list, url: {action: "create"} do |f| %>
I don't know if your controller's code excerpt you've pasted is complete, but you might missed to initialize list object for update. In your update action you have only
if #list.update_attributes(params[:lists])
but you are not initializing #list variable before. So you probably need something like
#list = Lists.find(params[:id])
You can also inspect your log file and verify what parameters are sent to controller.
My form
<%= form_tag load_link_path, :remote => true, :html => { :'data-type' => 'html', :id => 'load_link_form' } do %>
<fieldset>
<%= url_field_tag(:url) %>
<div class="validation-error"></div>
<%= submit_tag %>
</fieldset>
<% end %>
My action
def load
format.js
end
My js view(load.js.erb)
alert(0);
When submit the form, alert isn't firing. The console outputs:
Processing by LinksController#load as JS
Parameters: {"utf8"=>"✓", "commit"=>"Save changes", "url"=>"http://www.alfajango.com/blog/rails-3-remote-links-and-forms/", "authenticity_token"=>"KWwfZH+sivCrWdlVBUPM0MBRPHzcDhmbB96ckLQ82Dg="}
Rendered links/load.js.erb (0.4ms)
Completed 200 OK in 2822ms (Views: 9.9ms | ActiveRecord: 0.0ms)
Try adding respond_to at the top of your controller and then wrapping your format.js call in a respond_with block
class LinksController < ApplicationController
respond_to :html, :js
def load
respond_with do |format|
format.js
end
end
end
I'd recommend reading this article, it's the best guide to ajax with rails3 imo.
UPDATE:
I got it working after making a few small changes:
The default method for an ajax call in rails is "GET" so if you want to do a "POST", you need to include this with :data-method=>"post". I also removed the html hash from your form tag and set the data-type to script, see below:
<%= form_tag load_link_path, :remote => true, :id => 'create_link_form', :data => {:type=>"script", :"method"=>"post"} do %>
Also, you need to uncomment the alert from your js.erb file, so change from:
//alert(0);
$('#message').val('ok');
to:
alert(0);
//$('#message').val('ok');
Hope this helps, let me know if it doesn't and i'll push my code to github:)