Rails: No route matches {:action=>"edit", :controller=>"discussions" - ruby-on-rails

My discussions are nested within my projects. Projects is a basic CRUD and I'm trying to make discussions the same. They originally worked, but now I'm trying to add "edit" and "destroy" and I'm getting this error: No route matches {:action=>"edit", :controller=>"discussions", :format=>nil, :id=>nil, :project_id=>#<Discussion id: 24, title: "hello", description: "hello", created_at: "2015-02-02 20:58:53", updated_at: "2015-02-02 20:58:53", project_id: 12>} missing required keys: [:id]
for this line of code here <%= link_to "Edit", edit_project_discussion_path(item) %>.
discussions_controller.rb
class DiscussionsController < ApplicationController
def new
#project = Project.find(params[:project_id])
#discussion = Discussion.new
end
def create
#project = Project.find(params[:project_id])
#discussion = #project.discussions.build(discussion_params)
if #discussion.save
redirect_to new_project_discussion_path(#project)
end
end
def edit
#discussions = Discussion.find(params[:project_id])
end
def update
#discussions = Discussion.find(params[:project_id])
if #discussions.update_attributes(discussion_params)
redirect_to new_project_discussion_path
else
render "edit"
end
end
def destroy
#discussions = Discussion.find(params[:project_id])
#discussions.destroy
redirect_to new_project_discussion_path
end
def discussion_params
params.require(:discussion).permit(:project_id, :title, :description)
end
end
_form.html.erb
<%= form_for [#project, #discussion] do |f| %>
<div class="container">
Project: <%= #project.title %> <%= link_to "Go back?", projects_path %>
<hr>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "Submit discussion", class: "btn btn-primary" %>
</div>
</div>
<% end %>
edit.html.erb
<%= render "form" %>
new.html.erb
<div class="container">
<div class="page-header">
<h1>Discussions<small> Discuss the project.</small></h1>
</div>
</div>
<%= render "form" %>
<% if !#project.discussions.blank? %>
<% for item in #project.discussions %>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<%= item.title %>
</div>
</div>
<div class="panel-body">
<p>
<%= item.description %> <br>
<%= link_to "Comment", new_discussion_comment_path(item) %>
<%= link_to "Delete", item, :method => :delete %>
<%= link_to "Edit", edit_project_discussion_path(item) %> |
</p>
</div>
</div>
<% end %>
<% end %>
Routes:
Controller#Action
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
homes GET /homes(.:format) homes#index
POST /homes(.:format) homes#create
new_home GET /homes/new(.:format) homes#new
edit_home GET /homes/:id/edit(.:format) homes#edit
home GET /homes/:id(.:format) homes#show
PATCH /homes/:id(.:format) homes#update
PUT /homes/:id(.:format) homes#update
DELETE /homes/:id(.:format) homes#destroy
project_discussions GET /projects/:project_id/discussions(.:format) discussions#index
POST /projects/:project_id/discussions(.:format) discussions#create
new_project_discussion GET /projects/:project_id/discussions/new(.:format) discussions#new
edit_project_discussion GET /projects/:project_id/discussions/:id/edit(.:format) discussions#edit
project_discussion GET /projects/:project_id/discussions/:id(.:format) discussions#show
PATCH /projects/:project_id/discussions/:id(.:format) discussions#update
PUT /projects/:project_id/discussions/:id(.:format) discussions#update
DELETE /projects/:project_id/discussions/:id(.:format) discussions#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
discussion_comments GET /discussions/:discussion_id/comments(.:format) comments#index
POST /discussions/:discussion_id/comments(.:format) comments#create
new_discussion_comment GET /discussions/:discussion_id/comments/new(.:format) comments#new
edit_discussion_comment GET /discussions/:discussion_id/comments/:id/edit(.:format) comments#edit
discussion_comment GET /discussions/:discussion_id/comments/:id(.:format) comments#show
PATCH /discussions/:discussion_id/comments/:id(.:format) comments#update
PUT /discussions/:discussion_id/comments/:id(.:format) comments#update
DELETE /discussions/:discussion_id/comments/:id(.:format) comments#destroy
discussions GET /discussions(.:format) discussions#index
POST /discussions(.:format) discussions#create
new_discussion GET /discussions/new(.:format) discussions#new
edit_discussion GET /discussions/:id/edit(.:format) discussions#edit
discussion GET /discussions/:id(.:format) discussions#show
PATCH /discussions/:id(.:format) discussions#update
PUT /discussions/:id(.:format) discussions#update
DELETE /discussions/:id(.:format) discussions#destroy
root GET / homes#index
I'm unsure if any of my projects_controller.rb info is needed but if so I will update.

edit_project_discussion_path requires a Project and a Discussion.
/projects/:project_id/discussions/:id/edit(.:format)
You are only sending the Discussion item as the project parameter.
<%= link_to "Edit", edit_project_discussion_path(item) %>
You need to change that to include the project:
<%= link_to "Edit", edit_project_discussion_path(#project, item) %>

Related

Rails 4: ActiveRecord::RecordNotFound in PostsController#edit

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)

Rails: Conditional path in form partial

I have two forms for new and edit that I want to extract to a partial, but, to my begginer knowledge in Rails, they need different paths in order to work.
This is my edit form:
<%= simple_form_for [#user, #wiki], url: user_wiki_path do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>
This is my new form:
<%= simple_form_for [#user, #wiki], url: user_wikis_path, method: :post do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>
Can I, and if so, how, combine them into one partial _form.html.erb conditionally specifing the paths?
When I try joining them now I get the error below when trying to edit it:
No route matches [PATCH] "/users/32/wikis"
These are my routes:
Prefix Verb URI Pattern Controller#Action
users_index GET /users/index(.:format) users#index
users_show GET /users/show(.:format) users#show
root GET / pages#index
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_wikis POST /users/:user_id/wikis(.:format) wikis#create
new_user_wiki GET /users/:user_id/wikis/new(.:format) wikis#new
edit_user_wiki GET /users/:user_id/wikis/:id/edit(.:format) wikis#edit
user_wiki PATCH /users/:user_id/wikis/:id(.:format) wikis#update
PUT /users/:user_id/wikis/:id(.:format) wikis#update
DELETE /users/:user_id/wikis/:id(.:format) wikis#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
wikis GET /wikis(.:format) wikis#index
wiki GET /wikis/:id(.:format) wikis#show
charges POST /charges(.:format) charges#create
new_charge GET /charges/new(.:format) charges#new
charge PATCH /charges/:id(.:format) charges#update
PUT /charges/:id(.:format) charges#update
You can post local variables into the partial. Something like the following would work:
new.html.erb
<%= render partial: '_form', locals: {url: user_wikis_path, method: :post} %>
edit.html.erb
<%= render partial: '_form', locals: {url: user_wiki_path(#user, #wiki), method: :post} %>
_form.html.erb
<%= simple_form_for [#user, #wiki], url: url, method: method do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>
Or you could set #url and #method as instance variables and access them that way.
Controller
def new
#method = :get
#url = user_wikis_path
...
end
def edit
#method = :post
#url = user_wiki_path(#user, #wiki)
...
end
_form.html.erb
<%= simple_form_for [#user, #wiki], url: url, method: method do |f| %>
...
Or you could have a conditional in the form that accesses the current action, if you use action_name in the view it'll return the name of the current action.
# _form.html.erb
<%= simple_form_for #wiki do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.submit class: "btn btn-success" %>
<% end %>
and on your wikis#edit and wikis#new, add
...
#user = User.find params[:user_id]
...

Comments to my reddit clone ruby pulls a NameError

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.

No route matches [POST]

on my app i have this
i'm trying to create a car of a previously registered user
but i got the error (tittle post)
this is my carcontroller
class CarController < ApplicationController
def new
#car = Car.new
end
def create
#user = User.find(params[:user_id])
#car = #user.car.create(params[:car])
redirect_to user_path(#user)
end
end
this is my route.rb
Estaciones::Application.routes.draw do
devise_for :users
root :to => "user#index"
resources :user do
resources :cars
end
get "user/new"
post "user/create"
get "user/:id" => "User#show"
get "user/:user_id/car/new"
and this is part of my html.erb
<div class="container">
<h1>new user registered</h1>
<p>
<strong>name:</strong>
<%= #user.name %>
</p>
<p>
<strong>email:</strong>
<%= #user.email %>
</p>
<h2>new car registration</h2>
<%= form_for([#user, #user.car.build]) do |f| %>
<p>
<%= f.label :brand %><br />
<%= f.text_field :brand %>
</p>
<p>
<%= f.label :color %><br />
<%= f.text_field :color %>
</p>
<p>
<%= f.label :model %><br />
<%= f.text_field :model %>
</p>
<p>
<%= f.label :year %><br />
<%= f.text_field :year %>
</p>
<p>
<%= f.submit "Create new car"%>
</p>
<% end %>
</div>
when i submit the creation of the new car i got the next error
No route matches [POST] "/user/1/cars"
any idea??
also my routes:
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
root / user#index
user_cars GET /user/:user_id/cars(.:format) cars#index
POST /user/:user_id/cars(.:format) cars#create
new_user_car GET /user/:user_id/cars/new(.:format) cars#new
edit_user_car GET /user/:user_id/cars/:id/edit(.:format) cars#edit
user_car GET /user/:user_id/cars/:id(.:format) cars#show
PUT /user/:user_id/cars/:id(.:format) cars#update
DELETE /user/:user_id/cars/:id(.:format) cars#destroy
user_index GET /user(.:format) user#index
POST /user(.:format) user#create
new_user GET /user/new(.:format) user#new
edit_user GET /user/:id/edit(.:format) user#edit
user GET /user/:id(.:format) user#show
PUT /user/:id(.:format) user#update
DELETE /user/:id(.:format) user#destroy
user_new GET /user/new(.:format) user#new
user_create POST /user/create(.:format) user#create
GET /user/:id(.:format) User#show
GET /user/:user_id/cars/new(.:format) car#new
You need CarsController not CarController

No route matches [POST] "/user/1/cars"

on my app i have this
i'm trying to create a car of a previously registered user
but i got the error (tittle post)
this is my carcontroller
class CarController < ApplicationController
def new
#car = Car.new
end
def create
#user = User.find(params[:user_id])
#car = #user.car.create(params[:car])
redirect_to user_path(#user)
end
end
this is my route.rb
Estaciones::Application.routes.draw do
devise_for :users
root :to => "user#index"
resources :user do
resources :cars
end
get "user/new"
post "user/create"
get "user/:id" => "User#show"
get "user/:user_id/car/new"
and this is part of my html.erb
<div class="container">
<h1>new user registered</h1>
<p>
<strong>name:</strong>
<%= #user.name %>
</p>
<p>
<strong>email:</strong>
<%= #user.email %>
</p>
<h2>new car registration</h2>
<%= form_for([#user, #user.car.build]) do |f| %>
<p>
<%= f.label :brand %><br />
<%= f.text_field :brand %>
</p>
<p>
<%= f.label :color %><br />
<%= f.text_field :color %>
</p>
<p>
<%= f.label :model %><br />
<%= f.text_field :model %>
</p>
<p>
<%= f.label :year %><br />
<%= f.text_field :year %>
</p>
<p>
<%= f.submit "Create new car"%>
</p>
<% end %>
</div>
when i submit the creation of the new car i got the next error
No route matches [POST] "/user/1/cars"
any idea??
also my routes:
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
root / user#index
user_cars GET /user/:user_id/cars(.:format) cars#index
POST /user/:user_id/cars(.:format) cars#create
new_user_car GET /user/:user_id/cars/new(.:format) cars#new
edit_user_car GET /user/:user_id/cars/:id/edit(.:format) cars#edit
user_car GET /user/:user_id/cars/:id(.:format) cars#show
PUT /user/:user_id/cars/:id(.:format) cars#update
DELETE /user/:user_id/cars/:id(.:format) cars#destroy
user_index GET /user(.:format) user#index
POST /user(.:format) user#create
new_user GET /user/new(.:format) user#new
edit_user GET /user/:id/edit(.:format) user#edit
user GET /user/:id(.:format) user#show
PUT /user/:id(.:format) user#update
DELETE /user/:id(.:format) user#destroy
user_new GET /user/new(.:format) user#new
user_create POST /user/create(.:format) user#create
GET /user/:id(.:format) User#show
GET /user/:user_id/cars/new(.:format) car#new
I am not a ruby on rails person. But....
your post url /users/1/cars does not match any of the routes you mentioned in your route.rb file.
Try to do this :
root :to => "user#index"
resources :users do
resources :cars
end
Instead this :
root :to => "user#index"
resources :user do
resources :cars
end
(add a s to user). And try to go here : /users/1/cars
The problem is because resources :user is singular, but the route wants a plural. The routes should be:
resources :users do
resources :cars
end

Resources