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 %>
Related
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] %>
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.
This is my login form
<%= form_for Customer.new, url: {action: :login} do |f| %>
<%= f.text_field :username,placeholder: 'Username or Email' %>
<%= f.password_field :password,placeholder: 'Password' %>
<%= f.submit 'Login' %>
<% end %>
This is my controller
def login
username = params[:username]
password = params[:password]
unless username.blank? && password.blank?
#My code doesn't entering to this block
end
end
If i submit form it is just submitted it doesn't perform any action
Edit 1
This is what my console return when form submitted
Started POST "/auth/login" for 127.0.0.1 at 2013-09-08 08:23:05 +0530
Processing by AuthController#login as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"9f7TKlIspKQwX7jMSzI7XGrabgJKvnzj8Ip0OLTDtW4=", "customer"=>{"username"=>"xxx", "password"=>"[FILTERED]"}, "commit"=>"Login"}
Rendered auth/login.html.erb within layouts/application (3.5ms)
Rendered layouts/_header.html.erb (0.4ms)
Completed 200 OK in 21ms (Views: 19.3ms | ActiveRecord: 0.0ms)
If you check your params hash you'll see that those keys don't exist. Instead, you'll see that params[:custpmer] does and that it contains those fields. This is just how rails builds the params in a form_for style. So try this instead:
username = params[:customer][:username]
password = params[:customer][:password]
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:)
I'm creating a profile that is associated with a member id based on Devise authentication.
When creating the profile, the values aren't inserted into the database.
Routes file.
resources :troopers do
resource :trooper_profile
end
Trooper model
has_one :trooper_profile
Trooper Profile model
belongs_to :trooper
Trooper Controller #create action
def create
#trooper = current_trooper
#profile = #trooper.build_trooper_profile(params[:profile])
respond_to do |format|
if #profile.save
format.html { redirect_to(trooper_trooper_profile_path, :notice => 'Profile was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
Profile Form
<%= form_for #profile, :url => trooper_trooper_profile_path(#trooper) do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :first_name %><br /><br />
<%= f.label :last_name %><br />
<%= f.text_field :last_name %><br /><br />
<p><%= submit_tag "Create Profile" %></p>
<% end %>
Server output
Started POST "/troopers/1/trooper_profile" for 127.0.0.1 at 2011-05-20 13:04:01 +1000
Processing by TrooperProfilesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LhT6b4xu5bIJ5kwhS74L7dpaGbuR5BTdirh9AziD+Ew=", "trooper_profile"=>{"first_name"=>"Robert", "last_name"=>"", "commit"=>"Create Profile", "trooper_id"=>"1"}
Trooper Load (0.5ms) SELECT "troopers".* FROM "troopers" WHERE ("troopers"."id" = 1) LIMIT 1
TrooperProfile Load (0.3ms) SELECT "trooper_profiles".* FROM "trooper_profiles" WHERE ("trooper_profiles".trooper_id = 1) LIMIT 1
Rendered trooper_profiles/_form.html.erb (11.7ms)
Rendered shared/_head.html.erb (1.6ms)
Rendered shared/_menutop.html.erb (1.2ms)
Rendered trooper_profiles/new.html.erb within layouts/application (18.5ms)
Completed 200 OK in 109ms (Views: 22.5ms | ActiveRecord: 0.8ms)
I've got this same setup in another application and can't see why this isn't working.
Change the following...
#profile = #trooper.build_trooper_profile(params[:trooper_profile])
It appears the parameters being passed are :trooper_profile
trooper_profile"=>{"first_name"=>"Robert", "last_name"=>"", "commit"=>"Create Profile", "trooper_id"=>"1"}
However, you're referencing :profile
If it's rendering the new view, then the profile isn't saving - perhaps it's a validation error? Does TrooperProfile require a last name (blank in your example)?