Nested Form_For Singular Resource

nested form_for singular resource

I think form_for assumes all nested resources are plural resources. Fixing form_for would be the correct thing to do (and I urge you to submit a bug), but in the mean time, you can fake it:

# in app/helpers/application_helper.rb
module ApplicationHelper

def booking_reviews_path(*args)
booking_review_path(*args)
end

end

You can't use alias_method because the method booking_review_path doesn't exist in that module, but this is essentially the same thing.

Rails nested resource on a singular resource form

Ideally I'd like to see those event & action models - I suspect that's where the problem lies. Without seeing those, a few suggestions:

  • Is 'accepts_nested_attributes_for :action' set in the event model?

  • Remove any 'attr_accessible' line from both models & see if things work. (Keep in mind you need to set accessible attributes for nested forms in the parent model)

  • 'Action' seems like an imprudent name for a model. It's possible rails is overwriting 'action' methods with things related to the actual action

Hope this helps - I'd suggest posting the models if you still can't find a solution.

form_for nested resource (singular) not using correct path helper

Found my answer in another question: Ruby on rails: singular resource and form_for

app/models/totem.rb:

class Totem < ActiveRecord::Base
model_name.instance_variable_set :@route_key, 'totem'
belongs_to :user
end

(not sure why this Q&A didn't appear in my earlier searches...)

Ruby on rails: singular resource and form_for

Unfortunately, this is a bug. You'll have to set the url like you mention.

= form_for @order, :url => order_path do |f|

Note that this will properly route to create or update depending on whether @order is persisted.


Update

There's another option now. You can add this to your routes config:

resolve("Order") { [:order] }

Then when the polymorphic_url method is given an object with class name "Order" it will use [:order] as the url component instead of calling model_name.route_key as described in jskol's answer.

This has the limitation that it cannot be used within scopes or namespaces. You can route a namespaced model at the top level of the routes config:

resolve('Shop::Order') { [:shop, :order] }

But it won't have an effect on routes with extra components, so

url_for(@order)           # resolves to shop_order_url(@order)
url_for([:admin, @order]) # resolves to admin_shop_orders_url(@order)
# note plural 'orders' ↑
# also, 'shop' is from module name, not `resolve` in the second case

Rails Nested Singular Resource Routing

After looking around, it appears that the form generating the update had an incorrect url. If anyone is seeing this issue, it's because I had my form set up as:

form_for [@user, @profile] do |f| ...

This caused the form action to have the incorrect url (of the offending form above). Instead, I used

form_for @profile, :url => user_profile_path(@user) do |f| ...

and everything seemed to work.

Dealing has_one and singular resource with nested resource

I believe you can use url parameter in this case. Try something like:

<%= form_for [@user, @employeur], url: user_employeur_path do |f| %>
...

Nested Resource Creation in Parent Form

Ajax

You wouldn't need ajax functionality for this - Ajax only allows you to pull data from the server asynchronously, which essentially means you don't have to reload the page.

--

Nested Attributes

What you're looking for, as alluded to by atomAltera sounds like accepts_nested_attributes_for - which allows you to create dependent models from the parent

It sounds to me that you'll need to create a quote before you try and populate line_items, which is actually quite simple using ActiveRecord:

#app/models/quote.rb
Class Quote < ActiveRecord::Base
has_many :line_items
accepts_nested_attributes_for :line_items
end

#app/controllers/quotes_controller.rb
Class QuotesController < ApplicationController
def new
@quote = Quote.new
@quote.line_items.build
end

def create
@quote = Quote.new(quote_params)
@quote.save
end

private

def quote_params
params.require(:quote).permit(:quote, :attributes, :new, line_items_attributes: [:line, :items, :attributes])
end
end

--

If you need any further information, please let me know!!



Related Topics



Leave a reply



Submit