I am dealing with a nested resource "farm" in Rails, and my form for making new farm looks like this:
<%= form_for([#user, #farm], :url=> new_user_farm_path(#user)) do |f| %>
<% if #farm.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#farm.errors.count, "error") %> prohibited this farm from being saved:</h2>
<ul>
<% #farm.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :contact %><br />
<%= f.text_field :contact %>
</div>
<div class="field">
<%= f.label :adress %><br />
<%= f.text_field :adress %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My corresponding "new" function in the farms controller is:
def new
#farm = Farm.new
#user = User.find(current_user)
respond_to do |format|
format.html # new.html.erb
format.json { render json: #farm }
end
end
It renders the form just fine, but after I click submit, and it actually tries to create new Farm, I get this error:
No route matches [POST] "/users/2/farm/new"
In my rake routes, I clearly have thie route showing:
user_farm POST /users/:user_id/farm(.:format) {:action=
create", :controller=>"farms"}
I am only guessing that the problem is in my create function:
def create
#farm = Farm.new(params[:farm])
#user = User.find(current_user)
respond_to do |format|
if #farm.save
format.html { redirect_to user_farm(#user, #farm), notice: 'Farm was successfully created.' }
format.json { render json: #farm, status: :created, location: #farm }
else
format.html { render action: "new" }
format.json { render json: #farm.errors, status: :unprocessable_entity }
end
end
end
My routes.rb file:
resources :users do
resource :farm
end
devise_for :users, :path => 'accounts'
I am accessing my new farm form via this link:
<%= link_to "New Farm", new_user_farm_path(current_user) %>
My entire rake routes:
user_farm POST /users/:user_id/farm(.:format) {:action=>"
create", :controller=>"farms"}
new_user_farm GET /users/:user_id/farm/new(.:format) {:action=>"
new", :controller=>"farms"}
edit_user_farm GET /users/:user_id/farm/edit(.:format) {:action=>"
edit", :controller=>"farms"}
GET /users/:user_id/farm(.:format) {:action=>"
show", :controller=>"farms"}
PUT /users/:user_id/farm(.:format) {:action=>"
update", :controller=>"farms"}
DELETE /users/:user_id/farm(.:format) {:action=>"
destroy", :controller=>"farms"}
users GET /users(.:format) {:action=>"
index", :controller=>"users"}
POST /users(.:format) {:action=>"
create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"
new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"
edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"
show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"
update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"
destroy", :controller=>"users"}
new_user_session GET /accounts/sign_in(.:format) {:action=>"
new", :controller=>"devise/sessions"}
user_session POST /accounts/sign_in(.:format) {:action=>"
create", :controller=>"devise/sessions"}
destroy_user_session GET /accounts/sign_out(.:format) {:action=>"
destroy", :controller=>"devise/sessions"}
user_password POST /accounts/password(.:format) {:action=>"
create", :controller=>"devise/passwords"}
new_user_password GET /accounts/password/new(.:format) {:action=>"
new", :controller=>"devise/passwords"}
edit_user_password GET /accounts/password/edit(.:format) {:action=>"
edit", :controller=>"devise/passwords"}
PUT /accounts/password(.:format) {:action=>"
update", :controller=>"devise/passwords"}
cancel_user_registration GET /accounts/cancel(.:format) {:action=>"
cancel", :controller=>"devise/registrations"}
user_registration POST /accounts(.:format) {:action=>"
create", :controller=>"devise/registrations"}
new_user_registration GET /accounts/sign_up(.:format) {:action=>"
new", :controller=>"devise/registrations"}
edit_user_registration GET /accounts/edit(.:format) {:action=>"
edit", :controller=>"devise/registrations"}
PUT /accounts(.:format) {:action=>"
update", :controller=>"devise/registrations"}
DELETE /accounts(.:format) {:action=>"
destroy", :controller=>"devise/registrations"}
user_confirmation POST /accounts/confirmation(.:format) {:action=>"
create", :controller=>"devise/confirmations"}
new_user_confirmation GET /accounts/confirmation/new(.:format) {:action=>"
new", :controller=>"devise/confirmations"}
GET /accounts/confirmation(.:format) {:action=>"
show", :controller=>"devise/confirmations"}
user_unlock POST /accounts/unlock(.:format) {:action=>"
create", :controller=>"devise/unlocks"}
new_user_unlock GET /accounts/unlock/new(.:format) {:action=>"
new", :controller=>"devise/unlocks"}
GET /accounts/unlock(.:format) {:action=>"
show", :controller=>"devise/unlocks"}
home_index GET /home/index(.:format) {:controlle
r=>"home", :action=>"index"}
root / {:controlle
r=>"home", :action=>"index"}
The problem is that your form is attempting to make a POST request to a url that only exists for GET requests. So it's telling you that
[POST] "/users/2/farm/new"
doesn't exist -- which it doesn't. And your rake routes output confirms this -- the cloesst thing is
new_user_farm GET /users/:user_id/farm/new(.:format)
which is a GET request.
Forms default to using POST for new record creation, so you need to supply a url that can be POSTed to. Or you can let Rails figure it out from the state of your objects. So either
<%= form_for([#user, #farm], :url=> user_farm_path(#user)) do |f| %>
OR
<%= form_for([#user, #farm]) do |f| %>
should work. In the former case we're using a named route that matches a POST route from your rake routes output. In the latter case we're letting rails figure it out based on the state of your #farm object. That is, if #farm is a new_record? it'll submit a POST request to /users/:user_id/farm, or if #farm is persisted? then it'll submit a PUT request to /users/:user_id/farm. (Same path, just different request type.)
Some issues:
--
Params
When you create a new ActiveRecord object in Rails, you'll want to use strong params:
#app/controllers/farms_controller.rb
Class FarmsController < ApplicationController
def new
#farm = Farm.new
end
def create
#farm = Farm.new(farm_params)
end
private
def farm_params
params.require(:farm).permit(:params, :for, :farm)
end
end
--
Routes
You're using nested routing, which can be tricky if you're not used to it.
If you haven't already, you should do this with your routes:
#config/routes.rb
resources :users do
resources :farm #-> /users/3/farm/new
end
This will give you the ability to reach that route either from your controller or views.
If you give me some more info (Routes file), I'll be able to help further!
--
POST
Having looked over the issue again, it seems I made an oversight before!!!
As pointed out by pdbobb, the error certainly says you're trying to reach /new with a POST verb. This is not correct, as according to the Rails resourceful routing conventions, you need to
You'll be able to use pdobb's answer, but more importantly, we need to establish why your form is trying to submit to the new post.
The problem is likely with your nested resource
Related
This is a basic error and lots of people have ask similar questions, such as:
ActiveRecord::RecordNotFound in PostsController#show
ActiveRecord::RecordNotFound in PostsController#show clicking a link
However, none of them actually helped me solve the issue I am facing now.
In my Rails 4 app, I have four models:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
has_many: :posts
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Post < ActiveRecord::Base
belongs_to :calendar
end
Here are my routes:
Rails.application.routes.draw do
root to: 'pages#home'
devise_for :users, :path => 'account'
resources :calendars do
resources :posts, shallow: true
end
end
Here is the problem: when I am on a calendar, for instance http://localhost:3000/calendars/11, I have all the posts that belong to this calendar displayed, with Show, Edit and Destroy links alongside each post.
When I click the Edit link, I am taken to http://localhost:3000/posts/11/edit.5* and I get the following error:
ActiveRecord::RecordNotFound in PostsController#edit
Couldn't find Post with 'id'=11
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
Note: I don't know why I keep getting this weird url, with .5 at the end.
Here is the content of my PostsController:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
#calendar = Calendar.find(params[:calendar_id])
end
# POST /posts
# POST /posts.json
def create
#calendar = Calendar.find(params[:calendar_id])
#post = #calendar.posts.create(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to calendar_path(#calendar), notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: #post }
else
format.html { render :new }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
#calendar = Calendar.find(params[:calendar_id])
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to calendar_path(#calendar), notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: #post }
else
format.html { render :edit }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#calendar = Calendar.find(params[:calendar_id])
#post.destroy
respond_to do |format|
format.html { redirect_to calendar_path(#calendar), notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:date, :time, :subject, :format, :copy, :media)
end
end
UPDATE: as per #Nathan's comment, here is the edit.html.erb view:
<h1>Editing Post</h1>
<%= render 'form' %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
and here is the _form.html.erb partial used in it:
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<p>
<%= f.label :date %><br>
<%= f.date_select :date %>
</p>
<p>
<%= f.label :time %><br>
<%= f.time_select :time %>
</p>
<p>
<%= f.label :subject %><br>
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :format %><br>
<%= f.text_field :format %>
</p>
<p>
<%= f.label :copy %><br>
<%= f.text_area :copy %>
</p>
<p>
<%= f.label :media %><br>
<%= f.text_field :media %>
</p>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
UPDATE 2: as per #danielricecodes' answer, here is the result of running rake routes in Terminal:
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / pages#home
new_user_session GET /account/sign_in(.:format) devise/sessions#new
user_session POST /account/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /account/sign_out(.:format) devise/sessions#destroy
user_password POST /account/password(.:format) devise/passwords#create
new_user_password GET /account/password/new(.:format) devise/passwords#new
edit_user_password GET /account/password/edit(.:format) devise/passwords#edit
PATCH /account/password(.:format) devise/passwords#update
PUT /account/password(.:format) devise/passwords#update
cancel_user_registration GET /account/cancel(.:format) devise/registrations#cancel
user_registration POST /account(.:format) devise/registrations#create
new_user_registration GET /account/sign_up(.:format) devise/registrations#new
edit_user_registration GET /account/edit(.:format) devise/registrations#edit
PATCH /account(.:format) devise/registrations#update
PUT /account(.:format) devise/registrations#update
DELETE /account(.:format) devise/registrations#destroy
user_confirmation POST /account/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /account/confirmation/new(.:format) devise/confirmations#new
GET /account/confirmation(.:format) devise/confirmations#show
user_unlock POST /account/unlock(.:format) devise/unlocks#create
new_user_unlock GET /account/unlock/new(.:format) devise/unlocks#new
GET /account/unlock(.:format) devise/unlocks#show
calendar_posts GET /calendars/:calendar_id/posts(.:format) posts#index
POST /calendars/:calendar_id/posts(.:format) posts#create
new_calendar_post GET /calendars/:calendar_id/posts/new(.:format) posts#new
GET /posts/:id/edit(.:format) posts#edit
GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
calendars GET /calendars(.:format) calendars#index
POST /calendars(.:format) calendars#create
new_calendar GET /calendars/new(.:format) calendars#new
edit_calendar GET /calendars/:id/edit(.:format) calendars#edit
calendar GET /calendars/:id(.:format) calendars#show
PATCH /calendars/:id(.:format) calendars#update
PUT /calendars/:id(.:format) calendars#update
DELETE /calendars/:id(.:format) calendars#destroy
UPDATE 3: as per #Nathan's second comment, here is the content of my show.html.erb calendar view:
<h2><%= #calendar.name %> Calendar</h2>
<h3>Posts</h3>
<% if #calendar.posts.any? %>
<table>
<tr>
<th>Date</th>
<th>Time</th>
<th>Subject</th>
<th>Format</th>
<th>Copy</th>
<th>Media</th>
</tr>
<% #calendar.posts.each do |post| %>
<tr>
<td><%= post.date %></td>
<td><%= post.time %></td>
<td><%= post.subject %></td>
<td><%= post.format %></td>
<td><%= post.copy %></td>
<td><%= post.media %></td>
<td><%= link_to 'View', post %></td>
<td><%= link_to 'Update', edit_post_path(#calendar, post) %></td>
<td><%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</table>
<% end %>
<% else %>
<p>This calendar does not contain any post yet: just create one with the form below.</p>
<% end %>
<h3>Add a post to <%= #calendar.name %> Calendar:</h3>
<%= form_for([#calendar, #calendar.posts.build]) do |f| %>
<p>
<%= f.label :date %><br>
<%= f.date_select :date %>
</p>
<p>
<%= f.label :time %><br>
<%= f.time_select :time %>
</p>
<p>
<%= f.label :subject %><br>
<%= f.text_field :subject %>
</p>
<p>
<%= f.label :format %><br>
<%= f.text_field :format %>
</p>
<p>
<%= f.label :copy %><br>
<%= f.text_area :copy %>
</p>
<p>
<%= f.label :media %><br>
<%= f.text_field :media %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Edit', edit_calendar_path %> |
<%= link_to 'Back', calendars_path %>
UPDATE 4: here are the routes when I remove the shallow option from the post resource:
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root GET / pages#home
new_user_session GET /account/sign_in(.:format) devise/sessions#new
user_session POST /account/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /account/sign_out(.:format) devise/sessions#destroy
user_password POST /account/password(.:format) devise/passwords#create
new_user_password GET /account/password/new(.:format) devise/passwords#new
edit_user_password GET /account/password/edit(.:format) devise/passwords#edit
PATCH /account/password(.:format) devise/passwords#update
PUT /account/password(.:format) devise/passwords#update
cancel_user_registration GET /account/cancel(.:format) devise/registrations#cancel
user_registration POST /account(.:format) devise/registrations#create
new_user_registration GET /account/sign_up(.:format) devise/registrations#new
edit_user_registration GET /account/edit(.:format) devise/registrations#edit
PATCH /account(.:format) devise/registrations#update
PUT /account(.:format) devise/registrations#update
DELETE /account(.:format) devise/registrations#destroy
user_confirmation POST /account/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /account/confirmation/new(.:format) devise/confirmations#new
GET /account/confirmation(.:format) devise/confirmations#show
user_unlock POST /account/unlock(.:format) devise/unlocks#create
new_user_unlock GET /account/unlock/new(.:format) devise/unlocks#new
GET /account/unlock(.:format) devise/unlocks#show
calendar_posts GET /calendars/:calendar_id/posts(.:format) posts#index
POST /calendars/:calendar_id/posts(.:format) posts#create
new_calendar_post GET /calendars/:calendar_id/posts/new(.:format) posts#new
GET /posts/:id/edit(.:format) posts#edit
GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
calendars GET /calendars(.:format) calendars#index
POST /calendars(.:format) calendars#create
new_calendar GET /calendars/new(.:format) calendars#new
edit_calendar GET /calendars/:id/edit(.:format) calendars#edit
calendar GET /calendars/:id(.:format) calendars#show
PATCH /calendars/:id(.:format) calendars#update
PUT /calendars/:id(.:format) calendars#update
DELETE /calendars/:id(.:format) calendars#destroy
MacBook-Pro-de-Thibaud:calendy TXC$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
new_user_session GET /account/sign_in(.:format) devise/sessions#new
user_session POST /account/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /account/sign_out(.:format) devise/sessions#destroy
user_password POST /account/password(.:format) devise/passwords#create
new_user_password GET /account/password/new(.:format) devise/passwords#new
edit_user_password GET /account/password/edit(.:format) devise/passwords#edit
PATCH /account/password(.:format) devise/passwords#update
PUT /account/password(.:format) devise/passwords#update
cancel_user_registration GET /account/cancel(.:format) devise/registrations#cancel
user_registration POST /account(.:format) devise/registrations#create
new_user_registration GET /account/sign_up(.:format) devise/registrations#new
edit_user_registration GET /account/edit(.:format) devise/registrations#edit
PATCH /account(.:format) devise/registrations#update
PUT /account(.:format) devise/registrations#update
DELETE /account(.:format) devise/registrations#destroy
user_confirmation POST /account/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /account/confirmation/new(.:format) devise/confirmations#new
GET /account/confirmation(.:format) devise/confirmations#show
user_unlock POST /account/unlock(.:format) devise/unlocks#create
new_user_unlock GET /account/unlock/new(.:format) devise/unlocks#new
GET /account/unlock(.:format) devise/unlocks#show
calendar_posts GET /calendars/:calendar_id/posts(.:format) posts#index
POST /calendars/:calendar_id/posts(.:format) posts#create
new_calendar_post GET /calendars/:calendar_id/posts/new(.:format) posts#new
edit_calendar_post GET /calendars/:calendar_id/posts/:id/edit(.:format) posts#edit
calendar_post GET /calendars/:calendar_id/posts/:id(.:format) posts#show
PATCH /calendars/:calendar_id/posts/:id(.:format) posts#update
PUT /calendars/:calendar_id/posts/:id(.:format) posts#update
DELETE /calendars/:calendar_id/posts/:id(.:format) posts#destroy
calendars GET /calendars(.:format) calendars#index
POST /calendars(.:format) calendars#create
new_calendar GET /calendars/new(.:format) calendars#new
edit_calendar GET /calendars/:id/edit(.:format) calendars#edit
calendar GET /calendars/:id(.:format) calendars#show
PATCH /calendars/:id(.:format) calendars#update
PUT /calendars/:id(.:format) calendars#update
DELETE /calendars/:id(.:format) calendars#destroy
Any idea how I can fix this?
Two arguments are being passed in for a named path (edit_post) which expects one argument. This is causing problems when the controller action set_post tries to determine which :id to use when looking up the record to set as #post.
It's also why you have that .5 mysteriously appended to the end of your URL — I believe 5 is the :id of post (in that context), and 11 is the :id of #calendar.
edit_post_path only needs to know the :id of the post you want to edit, so you can fix this by changing <%= link_to 'Update', edit_post_path(#calendar, post) %> to <%= link_to 'Update', edit_post_path(post) %> (note the removal of #calendar).
If you really do want to edit this post as a resource nested under calendar (at the path /calendars/<calendar_id>/posts/<post_id>/edit), take a look at your config/routes.rb. The shallow: true option (which you used when nesting :posts under :calendars) is what's keeping the nested :edit path from being created.
You'll need to have that nested :edit path available, and then you can pass two arguments just as you are now (something like edit_calendar_post_path(#calendar, post)).
The error makes me think there's a problem with the link you're building in your view template. I'd help more but I do not see the View file or the output from rake routes. Without those two things, its hard for me to tell you why your app is generating a funny url - but it usually means the view template is not calling the correct url_helper.
The problem his related to the edit_post_path. With shallow nesting, the edit route is outside of the parent scope. The .5 is the Post ID being added to the url, but not as a valid url param because there's already an ID param from the calendar. Based on the way your routes are setup, it looks like you would need to set the id as a param in the link, like this:
link_to "edit post", edit_post_path(post)
I'm trying to add comment functionality of my Reddit clone. This is the comments controller that creates a comments and adds it to a post.
class CommentsController < ApplicationController
def new
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
#comment = Comment.new
#authorize #comment # from include Pundit in the application controller, authorize is an inherited method
end
def create
#topic = Topic.find(params[:topic_id])
#post = Post.find(params[:id])
#comment = current_user.comments.build(comment_params)
end
private
def comment_params
params.require(:comment).permit(:text)
end
end
I'm trying to add a comments field for every post page by using a form partial that looks like this:
<%= form_for [topic, post] do |f| %>
<%= form_group_tag(comment[:text]) do %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<% end %>
<div class = "form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
This form partial should appear at the post.show.html.erb so I put it there
<h1><%= markdown #post.title %></h1>
<div class="row"> <!-- what others are there besides row? -->
<div class="col-md-8">
<p><%= markdown #post.body %></p>
</div>
<div class="col-md-4">
<% if policy(#post).edit? %>
<%= link_to "Edit", edit_topic_post_path(#topic, #post), class: 'btn btn-success' %>
<% end %>
</div>
<div class="col-md-8">
<%= render partial: 'comments/form', locals: { topic: #topic, post: #post, text: #post.comments.new } %>
</div>
</div>
but I'm getting a NameError for my 'comment' on the form_group_tag line. Most of what I defined here comes from my code for adding new posts, which seemed to work. Is there something missing here?
I fixed my name error by adding comments to the form_for line, but I'm getting NoMethodError for my topic,post,comment path, so I thought it'd be helpful to add what rake routes is pulling up.
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
topic_posts POST /topics/:topic_id/posts(.:format) posts#create
new_topic_post GET /topics/:topic_id/posts/new(.:format) posts#new
edit_topic_post GET /topics/:topic_id/posts/:id/edit(.:format) posts#edit
topic_post GET /topics/:topic_id/posts/:id(.:format) posts#show
PATCH /topics/:topic_id/posts/:id(.:format) posts#update
PUT /topics/:topic_id/posts/:id(.:format) posts#update
DELETE /topics/:topic_id/posts/:id(.:format) posts#destroy
topics GET /topics(.:format) topics#index
POST /topics(.:format) topics#create
new_topic GET /topics/new(.:format) topics#new
edit_topic GET /topics/:id/edit(.:format) topics#edit
topic GET /topics/:id(.:format) topics#show
PATCH /topics/:id(.:format) topics#update
PUT /topics/:id(.:format) topics#update
DELETE /topics/:id(.:format) topics#destroy
post_comments POST /posts/:post_id/comments(.:format) comments#create
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
about GET /about(.:format) welcome#about
root GET / welcome#index
BTW: how does this routes.rb file look?
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts do
resources :comments, only: [:create]
end
end
Try:
<%= render partial: 'comments/form', locals: { topic: #topic, post: #post, comment: #post.comments.new } %>
...
<%= form_for [post, comment] do |f| %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>
...
If it does not help, try to temporary comment form_group_tag and send error here
It seems like you're sending the comment down to the partial as text.
... locals: { topic: #topic, post: #post, text: #post.comments.new } %>
^^^^
And by the way you're not saving the comment in the create action.
When I attempt to render this view:
Reply:
<%= form_for :message, url: [:reply, conversation] do |f| %>
<%= f.text_area :body %>
<%= f.submit "Send Message", class: 'btn btn-primary' %>
<%= submit_tag 'Clear Reply Box', type: :reset, class: 'btn btn-danger' %>
<% end %>
I get the error: undefined method 'reply_mailboxer_conversation_path'
Routes:
resources :messages do
member do
post :new
end
end
resources :conversations do
member do
post :reply
post :trash
post :untrash
end
collection do
get :trashbin
post :empty_trash
end
end
rake routes output:
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / profiles#index
message POST /messages/:id(.:format) messages#new
messages GET /messages(.:format) messages#index
POST /messages(.:format) messages#create
new_message GET /messages/new(.:format) messages#new
edit_message GET /messages/:id/edit(.:format) messages#edit
GET /messages/:id(.:format) messages#show
PATCH /messages/:id(.:format) messages#update
PUT /messages/:id(.:format) messages#update
DELETE /messages/:id(.:format) messages#destroy
reply_conversation POST /conversations/:id/reply(.:format) conversations#reply
trash_conversation POST /conversations/:id/trash(.:format) conversations#trash
untrash_conversation POST /conversations/:id/untrash(.:format) conversations#untrash
trashbin_conversations GET /conversations/trashbin(.:format) conversations#trashbin
empty_trash_conversations POST /conversations/empty_trash(.:format) conversations#empty_trash
conversations GET /conversations(.:format) conversations#index
POST /conversations(.:format) conversations#create
new_conversation GET /conversations/new(.:format) conversations#new
edit_conversation GET /conversations/:id/edit(.:format) conversations#edit
conversation GET /conversations/:id(.:format) conversations#show
PATCH /conversations/:id(.:format) conversations#update
PUT /conversations/:id(.:format) conversations#update
DELETE /conversations/:id(.:format) conversations#destroy
Really not sure what I'm doing wrong. If I've left out any important code, please just let me know.
https://github.com/portOdin/gfi2/tree/june6/app/views
The error you're receiving is because of how you're defining your form_for block:
<%= form_for :message, url: [:reply, conversation] do |f| %>
From the docs:
Resource-oriented style
In the examples just shown, although not indicated explicitly, we
still need to use the :url option in order to specify where the form
is going to be sent. However, further simplification is possible if
the record passed to form_for is a resource, i.e. it corresponds to a
set of RESTful routes, e.g. defined using the resources method in
config/routes.rb. In this case Rails will simply infer the appropriate
URL from the record itself. For example,
<%= form_for #post do |f| %>
...
<% end %>
is then equivalent to something like:
<%= form_for #post, as: :post, url: post_path(#post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
...
<% end %>
--
Nested
When you use an array for the resource part of form_for, you're basically telling rails that conversation will inherit from reply.
We use it like this:
<%= form_for [:admin, #object] do |f| %>
This basically treats the url attribute like this:
url: reply_conversation_path
I don't know where it got mailboxer from, but either way, the problem will be caused by the declaration of [:reply, conversation].
--
Fix
There are two ways to fix this.
The first is to use the url: attribute inside the form_for, like this:
<%= form_for [:reply, #conversation], url: reply_conversation_path(conversation) do |f| %>
The second is to use the correct formatting:
<%= form_for #reply, url: reply_conversation_path(#conversation) do |f| %>
...
<% end %>
There are two things that you have to keep in mind whenever you make a form
a. You need to initialise your resource in your controller, in your case reply. I'm assuming you are in show action of conversations controller so you can initialise it like this:
#reply = Message.new # assuming you dont have a reply model and you are using message model as reply
b. The path where you are creating your resource. If you look at your rake routes
reply_conversation POST /conversations/:id/reply(.:format) conversations#reply. You can clearly see that you need to have a conversation to make a reply in that conversation so again you'll have to find that conversation in your show action of conversations controller like this:
#conversation = Conversation.find(params[:id])
Now to make a form is relatively simple
<% form_for #reply, url: reply_conversation_path(#conversation.id) do |f| %>
<%= #your fields %>
<% end %>
I only have 2 weeks learning ruby on rails, in my app the users can register their cars, from their profile (code below) the app sent to the registration cars page,
<div class="container">
<fieldset>
<h1><%= #user.email %></h1>
<br>
<h2>now you are able to...</h2>
<br>
<ul>
<li>
<strong>new car registration: </strong>
<%= link_to "new car", new_user_car_path(current_user)%>
</li>
</ul>
</fieldset>
</div>
it works before but i don't know what i did that now it show this:
Routing Error
No route matches {:action=>"show", :user_id=>#<User id: 27, email: "armando.santoya#hotmail.com", encrypted_password: "$2a$10$EZtvPWiXgMfUlAqvuvGAzODMaas/y4rGkJPKJtg4PnC6...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-07-24 19:07:54", last_sign_in_at: "2012-07-24 19:07:54", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: nil, created_at: "2012-07-24 19:07:54", updated_at: "2012-07-24 19:07:54">, :controller=>"cars"}
Try running rake routes for more information on available routes.
also I put my carsController
class CarsController < ApplicationController
def new
#car = Car.new
end
def create
#car = current_user.Car.new(params[:car])
if #car.save
flash[:notice] = "new car created success"
#redirect_to current_user, :flash => { :success => "car created!" }
else
#redirect_to new_user_car_path,
flash[:notice] = "sorry try again"
end
end
def index
#car=Car.all
end
def show
#car = current_user.car.find(params[:id])
##car = Car.find(params[:id])
#redirect_to #user
end
end
and my routes.rb
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users do
resources :cars
end
my rake routes:
root / static_pages#home
contact /contact(.:format) static_pages#contact
about /about(.:format) static_pages#about
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_cars GET /users/:user_id/cars(.:format) cars#index
POST /users/:user_id/cars(.:format) cars#create
new_user_car GET /users/:user_id/cars/new(.:format) cars#new
edit_user_car GET /users/:user_id/cars/:id/edit(.:format) cars#edit
user_car GET /users/:user_id/cars/:id(.:format) cars#show
PUT /users/:user_id/cars/:id(.:format) cars#update
DELETE /users/:user_id/cars/:id(.:format) cars#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
this is the new.html.erb for new cars
<div class="container">
<h2>new car registration</h2>
<%= form_for(:users, :url => user_car_path(current_user)) do |f| %>
<div><%= f.label :brand %><br />
<%= f.text_field :brand %></div>
<div><%= f.label :color %><br />
<%= f.text_field :color %></div>
<div><%= f.label :model %><br />
<%= f.text_field :model %></div>
<div><%= f.label :year %><br />
<%= f.text_field :year %></div>
<div><%= f.submit "new car",:class => "btn btn-primary" %></div>
<% end %>
<%= link_to "new car", new_user_car_path(current_user)%>
should be ok given your route
new_user_car GET /users/:user_id/cars/new(.:format) cars#new
but the error says that a car#show (not new!) is missing, so maybe look for that.
When is the error exactly thrown?
ADDITION: now that you've posted the form,
I think the line producing the error is
<%= form_for(:users, :url => user_car_path(current_user)) do |f| %>
because user_car_path needs both a user and a car - so you would need
user_car_path(current_user,#car)
I've used just this in my form:
<%= form_for ([#user,#car]) do |f| %>
but the bottom line is, every time you're referencing a car, you need also the enclosing user reference.
I don't know if it is generating the problem, but why are you passin the current_user here:
<%= link_to "new car", new_user_car_path(current_user)%>
In your creation action, you are already getting the current user:
#car = current_user.Car.new(params[:car])
Also in your show action you have car and not Car
def show
#car = current_user.car.find(params[:id])
EDIT - based in your routes, I can see you have a nested resource:
your cars controller should be:
class CarsController < ApplicationController
def new
#user = User.find(params[:user_id])
#car = #user.cars.build
end
def create
#user = User.find(params[:user_id])
#car = #user.cars.build(params[:car])
if #car.save
flash[:notice] = "new car created success"
#redirect_to current_user, :flash => { :success => "car created!" }
else
#redirect_to new_user_car_path,
flash[:notice] = "sorry try again"
end
end
def index
#car=Car.all
end
...
And the link to create the new car is new_user_car_path
For what it's worth, I had a problem very similar to the one described by OP: the "index" action for my controller worked, but the "new" action threw the same exception ("show" route missing, which was not the case). It turned out Rails had some problem with the pluralization of my model name (ending in a 'y').
Rather than fighting the convention, I ended up choosing another model name with simpler pluralization and things were fine.
I'm trying to render a _new.html.erb partial from a comments controller inside a posts controller.
My comments resources is nested in my posts resource
routes.rb
resources :users, :only => [:show, :create, :new]
resources :posts do
resources :comments
resources :memorybooks
end
root to: 'static_pages#home'
match '/channel', to: 'static_pages#channel'
match 'login', to: 'static_pages#login'
match '/posts', to: 'posts#new'
match '/users', to: 'users#new'
My _new.html.erb partial in my comments controller:
<%= form_for([#post, #comment]) do |f| %>
<%= f.label :comment %>
<%= f.text_field :comment %>
<p>
<center>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
</center>
<% end %>
My comments controller method:
def create
#post = Post.find_by_id(params[:id])
#comment = #post.comments.build(params[:comment])
if #comment.save
redirect_to #current_post
else
render '/'
end
end
In my show.html.erb file, when I use the following:
<%= render 'comments/new' %>
The _new partial form appears, but when I post, I get the error:
No route matches [POST] "/comments"
And if I use the following line in show.html.erb
<%= render new_post_comment %>
I get the error:
undefined local variable or method `new_post_comment'
Below is my rake routes
users POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
user GET /users/:id(.:format) users#show
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
post_memorybooks GET /posts/:post_id/memorybooks(.:format) memorybooks#index
POST /posts/:post_id/memorybooks(.:format) memorybooks#create
new_post_memorybook GET /posts/:post_id/memorybooks/new(.:format) memorybooks#new
edit_post_memorybook GET /posts/:post_id/memorybooks/:id/edit(.:format) memorybooks#edit
post_memorybook GET /posts/:post_id/memorybooks/:id(.:format) memorybooks#show
PUT /posts/:post_id/memorybooks/:id(.:format) memorybooks#update
DELETE /posts/:post_id/memorybooks/:id(.:format) memorybooks#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root / static_pages#home
channel /channel(.:format) static_pages#channel
login /login(.:format) static_pages#login
/posts(.:format) posts#new
/users(.:format) users#new
Any help would be appreciated! Thank you!
try this
<%= render new_post_comment(#post, #comment) %>
I hope that can help you.