Ask A Question

Notifications

You’re not receiving notifications from this thread.

Having issue with Nested routes and form

Mike Chilson asked in Rails

I know this is probably simple for most of you folks, but I am trying to work through an area I am weak on in Rails which is forms with nested routes. First here is what I have...

Models
Topic - has_many :blogs
Blog - belongs_to :topic

Nested Routes
resources :topics do
resources :blog
end

Blog controller edit and update methods

def edit
@page_title = " | Edit Post "
@blog = Blog.find(params[:id])
end

def update
respond_to do |format|
if @blog.update(blog_params)
format.html { redirect_to @blog, notice: 'Post was successfully updated.' }
else
format.html { render :edit }
end
end
end

I can get the form to load with the correct record I want by using the following though it feels clunky and most likely wrong.
edit_topic_blog_url(:id => blog.id, :topic_id => blog.topic_id)
which gives the URL
http://localhost:3000/topics/1/blog/25/edit

And my form I simply have
<%= form_for(@blog) do |f| %>
blah, blah, blah
<%end %>

However, after trying this I get the following error: "No route matches [PATCH] "/blog.dummy-article-title".

Can someone point me to a resource that explains the relationship between the form and nested route?

Thanks in advance!
Mike

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.