There are four models in my Rails 4 application:
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
Using this routing:
Rails.application.routes.draw do
root to: 'pages#home'
devise_for :users, :path => 'account'
resources :calendars do
resources :posts, shallow: true
end
end
What gives these routes:
Prefix Verb URI Pattern Controller
posts GET /posts(.:format) posts
POST /posts(.:format) posts
new_post GET /posts/new(.:format) posts
edit_post GET /posts/:id/edit(.:format) posts
post GET /posts/:id(.:format) posts
PATCH /posts/:id(.:format) posts
PUT /posts/:id(.:format) posts
DELETE /posts/:id(.:format) posts
root GET / pages
new_user_session GET /account/sign_in(.:format) devise/sessions
user_session POST /account/sign_in(.:format) devise/sessions
destroy_user_session DELETE /account/sign_out(.:format) devise/sessions
user_password POST /account/password(.:format) devise/passwords
new_user_password GET /account/password/new(.:format) devise/passwords
edit_user_password GET /account/password/edit(.:format) devise/passwords
PATCH /account/password(.:format) devise/passwords
PUT /account/password(.:format) devise/passwords
cancel_user_registration GET /account/cancel(.:format) devise/registrations
user_registration POST /account(.:format) devise/registrations
new_user_registration GET /account/sign_up(.:format) devise/registrations
edit_user_registration GET /account/edit(.:format) devise/registrations
PATCH /account(.:format) devise/registrations
PUT /account(.:format) devise/registrations
DELETE /account(.:format) devise/registrations
user_confirmation POST /account/confirmation(.:format) devise/confirmations
new_user_confirmation GET /account/confirmation/new(.:format) devise/confirmations
GET /account/confirmation(.:format) devise/confirmations
user_unlock POST /account/unlock(.:format) devise/unlocks
new_user_unlock GET /account/unlock/new(.:format) devise/unlocks
GET /account/unlock(.:format) devise/unlocks
calendar_posts GET /calendars/:calendar_id/posts(.:format) posts
POST /calendars/:calendar_id/posts(.:format) posts
new_calendar_post GET /calendars/:calendar_id/posts/new(.:format) posts
GET /posts/:id/edit(.:format) posts
GET /posts/:id(.:format) posts
PATCH /posts/:id(.:format) posts
PUT /posts/:id(.:format) posts
DELETE /posts/:id(.:format) posts
calendars GET /calendars(.:format) calendars
POST /calendars(.:format) calendars
new_calendar GET /calendars/new(.:format) calendars
edit_calendar GET /calendars/:id/edit(.:format) calendars
calendar GET /calendars/:id(.:format) calendars
PATCH /calendars/:id(.:format) calendars
PUT /calendars/:id(.:format) calendars
DELETE /calendars/:id(.:format) calendars
And finally, here is the content posts_controller.rb:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
end
def new
@post = Post.new
end
def edit
end
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
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to calendar_path, 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
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
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:date, :time, :subject, :format, :copy, :media)
end
end
I continue to encounter a similar error.
Problem # 1 : when trying to delete a message from the calendar show.html.erb:
<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(post) %></td>
<td><%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
</table>
<% end %>
...
I get:
ActiveRecord::RecordNotFound in PostsController
Couldn't find Calendar with 'id'=
Problem # 2 : when trying to update a message from edit.html.erbpost view :
<h1>Editing Post</h1>
<%= render 'form' %>
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
I get:
ActiveRecord::RecordNotFound in PostsController
Couldn't find Calendar with 'id'=
Problem number 3 : when I try to return to the calendar in show.html.erbfrom the show.html.erb view:
<div>
<p>Date</p>
<%= @post.date %>
</div>
<div>
<p>Time</p>
<%= @post.time %>
</div>
<div>
<p>Subject</p>
<%= @post.subject %>
</div>
<div>
<p>Format</p>
<%= @post.format %>
</div>
<div>
<p>Copy</p>
<%= @post.copy %>
</div>
<div>
<p>Media</p>
<%= @post.media %>
</div>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
I get:
ActiveRecord::RecordNotFound in PostsController
Couldn't find Calendar with 'id'=
If my interpretation of the errors is correct, each time the problem is that I canβt get one id calendarthat belongs post.
, () () .
, , , .
, .
- , , - , , .
?