I'm getting the following error when loading this URL: localhost:3000/groups/5/post/new
No route matches [GET] "/groups/5/post/new"
I am trying to create a new "post" for a specific group. Here is my Post controllers "new" action:
def new
#group = Group.find(params[:group_id])
#post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #post }
end
end
I have my routes orginized as so:
resources :groups do
resources :posts do
resources :comments
end
end
Does anyone see anything that may be causing this?
Thank you.
localhost:3000/groups/5/post/new
Should be
localhost:3000/groups/5/posts/new
Using that nested resource, shouldn't the path be?: /groups/5/posts/new (note the plural posts)
Related
I have got such routes:
resources :projects do
resources :chats
resources :lists do
resources :issues
end
end
Now I am trying to setup proper form to add issue to list, but I do not know how... Currently it looks like this:
Controller:
def show
#project = Project.find(params[:id])
#list = List.new
#issue = #list.issues.build
#chats = #project.chats
#lists = #project.lists.includes(:issues)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #project }
end
end
Form
form_for [#list, #issue], remote: true do |f|
And I get error like this:
undefined method `list_issues_path' for #<#<Class:0x00000003996f30>:0x000000038ad678>
How should I solve it? Thanks in advance!
I believe that is because it would need to be nested under the project. If you run rake:routes I imaging you have something like projects/:id/lists/:id/issues/? You can see what the name of the route is next to that. Otherwise you can add the shallow option to the lists route.
I have posts, and posts categories
Post belongs_to :category
Category has_many :posts
i want when I create new post rails redirect me to created post
but when i use in Posts controller
def create
#category = Category.find(params[:category_id])
#post = current_user.posts.build(post_params)
if #post.save
flash[:success] = "Поздравляем Ваше задание опубликованно"
redirect_to category_post_path(#post)
else
render 'new'
end
end
This
redirect_to category_post_path(#post)
rails give me error
No route matches {:action=>"show", :controller=>"posts", :category_id=>#, :id=>nil, :format=>nil} missing required keys: [:id]
but i want if #post.save rails redirect_to created post
Help please.
I bet your routes look like this:
resources :categories do
resources :posts
end
This create the URL helper category_post_path, but it needs a category and a post.
Try this:
category_post_path #category, #post
I am creating an app using Ruby on Rails and in the Admin panel there are blogs and posts controllers. The routes for the admin area looks like this:
constraints :subdomain => "admin" do
scope :module => "admin" do
root to: "pages#index"
resources :blogs do
resources :posts, :controller => "posts"
end
end
end
What I have is http://admin.mydomain.com/blogs showing the blogs with /blogs/2/ showing the posts on that blog.
What I want is for when creating a new post at /blogs/2/posts/new that the blog_id is attached to the post.
In the admin/posts_controller.rb I have this as the create action
def create
#post = Post.new(params[:post])
if #post.save
redirect_to posts_path, notice: 'Post was successfully created.'
else
render action: "new"
end
end
At the moment it just creates a post. I want to link that post to the current blog id which is in the URL - /blog/2.
How would I go about doing this?
There are a number of ways of doing this and does depend on how you're actually using the controller. If you just edit posts at /blogs/1/xxxx then you can do this:
The blog_id will be available as params[:blog_id]. I'd usually create a before_filter to find the blog and then do the rest in the create action:
before_filter do
#blog = Blog.find(params[:blog_id])
end
def create
#post = #blog.posts.build(params[:post])
if #post.save
redirect_to [#blog, #post], notice: 'Post created successfully'
else
render :action => 'new'
end
end
You want to use the power of ActiveRecord associations for this, something like this should work:
def create
#blog = Blog.find_by_id(params[:id])
if #blog
#post = #blog.posts.new(params[:post])
if #post.save
redirect_to posts_path, notice: 'Post was successfully created.'
end
end
render :new
end
First find the blog post, which will, according to your route will be the :id in the params hash. Next use #blog.posts.new to create a new post associated with that blog.
I am really confused about Ruby on Rails REST routing. Even though I have specified that after the success it should go to the confirm action it goes to the show action and pass the ID=confirm.
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to :action => "confirm" }
else
format.html { render :action => "new" }
end
end
end
The error I get is the following:
ActiveRecord::RecordNotFound in ArticlesController#show
Couldn't find Article with ID=confirm
Rails.root: /Projects/highoncoding
Application Trace | Framework Trace | Full Trace
app/controllers/articles_controller.rb:31:in `show'
UPDATE 1:
Here is my Route.rb file:
resources :articles
get "articles/confirm"
# config/routes.rb
MyApp::Application.routes.draw do
resources :articles do
member do
get 'confirm'
end
end
end
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
# use a named route here
format.html { redirect_to confirm_article_url(#article) }
else
format.html { render :action => "new" }
end
end
end
end
you'll need to add the route so it looks like
match 'articles/confirm/', :controller => 'article', :action => 'confirm'
resources :articles
you need to have the :id in there or it will think that confirm is an id which is why you are seeing the error ID=confirm. make sure also that this is the first route. (at least before the resources for the articles controller.
You should probably add the confirm route directly in your routes file.
match 'articles/confirm' => 'articles#confirm'
resources only work for create/update/destroy/etc.
I'm not being able to redirect the user to the page I want after he creates a new "service" resource.
Here's the routes.rb:
resources :wsps do
resources :services
end
The html form:
<%= form_for([#wsp,#service]) do |f| %>
Services_controller.rb:
def new
#wsp = current_wsp
#service = #wsp.services.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #service }
end
end
def create
#wsp = current_wsp
#service = #wsp.services.build(params[:service])
if #service.save
redirect_to wsp_service_path
end
end
The wsp_service_path goes to /wsps/1/services and the error:
No route matches {:action=>"destroy", :controller=>"services"}
What am I doing wrong? Why cant't I use "wsp_service_path"?
Thank you.
You can use wsp_service_path (while you should be use wsp_service_url since you are in a controller). All you are missing is arguments. wsp_service_path (or _url) are going to expect two arguments: a wsp and a service. Once you provide those two, it works.
redirect_to wsp_service_url(#wsp, #service)