Handling Has_One Nested Resource in Rails 3

Rails 3.1 has_one nested resource: routing not generating all paths

I dont really know why it's not displayed when you do rake routes but did you try in your code to do supplier_presentation_path(@supplier)? It should work based on your routes.

Nested Routes with has_one in rails

for this, I would nest inside of a block:

map.resources :customers do |customer|
customer.resource :quote do |quote|
quote.resources :first_resources
quote.resources :second_resources
end
end

alternative syntax:

map.resources :customers do |customer|
customer.resource :quote, :has_many => [:first_resources, :second_resources]
end

This would give you urls of

customers/:customer_id/quote/first_resources/:id
customers/:customer_id/quote
customers/:id

Or the way you provided I believe you would need to map plural quotes in order to be able to get to a specific quote if you don't want to nest

map.resources :customers, :has_one => :quote
map.resources :quotes, :has_many => [:first_resources, :second_resources]

that would give you urls of

customers/:customer_id/quote
customers/:id
quotes/:quote_id/first_resources/:id

I think the first one is what you are after. Hope this helps.

Resources: http://api.rubyonrails.org/classes/ActionController/Resources.html

Rails 3 - Nested Resources Routing

It sounds like mailbox should be a singleton resource.

resources :users do
resource :mailbox
end

Otherwise it would expect that a user has multiple mailboxes and you'd have to provide the mailbox_id to user_mailbox_path as well.

user_mailbox_path(current_user, @mailbox)

How do I make an inherited_resource optionally nested as a singleton?

In case anyone is trying to do something similar, I ended up just abandoning inherited_resources. I'm finding I'm happier with less "magic" going on in my controllers.

undefined method in nested resources has_one

If your Location model really has a latitude attribute, the most likely cause for this error is that you have some City records in your database that have nil values for location. You can check whether this is the case by looking at the full exception message, is it something like?:

NoMethodError: undefined method `latitude' for nil:NilClass

In that case you have two choices depending on whether you consider latitude to be a required attribute for your City model or not. If its required then enforce that rule by adding a validation rule to your City model:

validates :location, presence: true

and perhaps a not null constraint to the foreign key column in the database schema.

Alternatively if missing latitude values are permitted in your application you can just handle the missing values in your view:

= city.location.try(:latitude)

Nested objects has_one relation

In your create action, you need to do as follows:

def create
@project = Project.new project_params
# Don't need to create dependent objects

respond_to do |format|
if @project.save
...

You don't need to separately create album; all the other code looks like it should work. If it doesn't, you will need to post the submitted params from your form (to which I'll be able to write an update).

--

Update

To show the albums in the view, you just need to call the associative method:

#app/controllers/projects_controller.rb
def show
@project = Project.find params[:id]
end

#app/views/projects/show.html.erb
<% @project.albums.each do |album| %>
<%= album.title %>
<% end %>

Rails 4 API: Creating a nested resource

You'll want accepts_nested_attributes_for to do this.

It'll allow you to create related models simply by passing the proper attributes to your API endpoint.

rails render an action with nested route

Look at the way your form is set up in your view. It should be something like the following. Your LeadsController#edit method will need to load both @contact and @lead.

<%= form_for [@contact, @lead] do |f| %>
...
<% end %>

The fact that you're getting the non-nested-resource url from the form suggests that you're not using this pattern, and you should.

Also, I assume this was a typo, but in your question, you're declaring the nested route as

resource :leads

instead of

resources :leads

The singular resource is valid, but it means something different than what you're intending here, i.e., that each contact has one associated lead.

Unpermitted Parameters in a two level deep nested form with has_one relationship

I Think I've solved it. The issue was, I had to add edit my exam.rb model to look like this:

belongs_to :unit

has_many :questions, :dependent => :destroy

has_many :answers, :through => :questions

accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:question].blank? }, :allow_destroy => true

Note: I created a new model called response with exactly the same fields as answer as well as edited my exam_controller.br as advised by Rich Peck

It appears that adding the line has_many :answers, :through => :questions to my parent model solved the issue.
Now the parameter responsechanged to response_attributes as it should be.

Thanks, Rich Peck was really helpful.



Related Topics



Leave a reply



Submit