mercredi 15 juillet 2015

Couldn't find Project with 'id'=

ActiveRecord::RecordNotFound in PostsController#new

I am trying to nest posts in to projects, I cannot get past this when I access posts/new. any help will be greatly appreciated.

posts.rb

class Post < ActiveRecord::Base
belongs_to :user
belongs_to :project
attr_accessible :user_id, :content,:avatar, :user_id, :content, :updated_at, :avatar_updated_at, :updated_file_size, :ancestry, :parent_id, :user, :project_id, :id, :post
has_ancestry
acts_as_likeable
acts_as_followable
  #acts_as_nested_set
#acts_as_commentable
scope :sorted, lambda { order("posts.id ASC") }
has_attached_file :avatar, 
    :path => ":rails_root/public/system/:class/:attachement/:id/:basename_:style.:extension",
    :url => "/system/:class/:attachement/:id/:basename_:style.:extension",
    :styles => {
      :thumb    => ['100x100#',  :jpg, :quality => 70],
      :preview  => ['480x480#',  :jpg, :quality => 70],
      :large    => ['600>',      :jpg, :quality => 70],
      :retina   => ['1200>',     :jpg, :quality => 30]
    },
    :convert_options => {
      :thumb    => '-set colorspace sRGB -strip',
      :preview  => '-set colorspace sRGB -strip',
      :large    => '-set colorspace sRGB -strip',
      :retina   => '-set colorspace sRGB -strip -sharpen 0x0.5'
    }

  validates_attachment :avatar,
  # :presence => true,
  #  :size => { :in => 0..10.megabytes },
    :content_type => { :content_type => /^image\/(jpeg|png|gif|tiff)$/ }

 # validates :name,
  #  :presence => true,
  #  :uniqueness => true


end

posts_controller.rb

class PostsController < ApplicationController
  before_action :find_project#:set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
  @posts = @project.posts.all


  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    project = Project.find(params[:project_id])
    @post = project.posts.find(params[:id])

  end


  # GET /posts/new
  def new
   @project = Project.find(params[:project_id])
    @post = project.posts.new({:project_id => @project_id})


    @post.parent_id = params[:parent_id]
  end

  def create

    @post = Post.new(post_params)

    if @post.save!
      redirect_to([@post.project, @post])
redirect_to(:action => 'index', :project_id => @project.id)
  else
    render(:action => "new")

    end
  end

  # GET /posts/1/edit
  def edit
    @project = Project.find(params[:project_id])
    @post = project.posts.find(params[:id])



  end

  # POST /posts
  # POST /posts.json

  def update

project = Project.find(params[:project_id])
@post = Project.posts.find(params[:id])

if @post.update_attributes(params[:posts])
  redirect_to ([@post.project, @post])
else
  render :action => "edit"


  end
end


def delete 
  @post = Post.find(params[:id])
end

def destroy
   project = Project.find(params[:project_id])

   @post = project.posts.find(params[:id])
   @post.destroy





  #post = Post.find(params[:id]).destroy
  redirect_to(:action => 'index', :project_id => @project.id)

end


private

     def post_params
      params.require(:post).permit(:user_id, :content,:avatar, :user_id, :content, :updated_at, :ancestry,:parent_id ,:avatar_updated_at, :updated_file_size, :user , :project_id, :id)
    end
     def find_project
      if params[:project_id]

      @project = Project.find(params[:project_id])
      end

     end


end

Routes.rb

Rails.application.routes.draw do
  devise_for :users
  #resources :users

  resources :projects do
    resources :posts
  end

  get 'users/index'
  #root :to => 'posts#index'
  get 'users/show'
  #root :to => "projects#index"


   # root to: 'home#index'
    get 'follow' => 'users#follow'
    get 'unfollow' => 'users#unfollow'
    get 'like' => 'users#like'
    get 'unlike' => 'users#unlike'
match ':controller(/:action(/:id))', :via => [:get, :post]

end

index.html.erb

<td><%= post.content %></td>

  <% end %>

</tr>


<%= link_to("<< Back to projects", {:controller => 'projects', :action => 'index'}, :class => 'back-link') %>

<div class="posts index">
  <h2>posts</h2>


  <table class="listing" summary="post list">
    <tr class="header">
      <th>&nbsp;</th>
      <th>project</th>
      <th>post</th>
      <th>Permalink</th>
      <th>Visible</th>
      <th>Sections</th>
      <th>Actions</th>
    </tr>
    <% @posts.each do |post| %>
    <tr>
      <td><%= post.position %></td>
      <td><%= post.project.name if post.project %></td>
      <td><%= post.name %></td>
      <td><%= post.permalink %></td>
      <td class="center"><%= status_tag(post.visible) %></td>
      <td class="center"><%= post.sections.count  %></td>
      <td class="actions">
         <%= link_to("Add New post", {:action => 'new', :project_id => @project.id}, :class => 'action new') %>

        <%= link_to("View Sections", {:controller => 'sections', :post_id => post.id}, :class => 'action show') %>
        <%= link_to("Show", {:action => 'show', :id => post.id, :project_id => @project.id}, :class => 'action show') %>
        <%= link_to("Edit", {:action => 'edit', :id => post.id, :project_id => @project.id}, :class => 'action edit') %>
        <%= link_to("Delete", {:action => 'delete', :id => post.id, :project_id => @project.id}, :class => 'action delete') %>
      </td>
    </tr>
    <% end %>
  </table>
</div>

edit.html.erb

<% @Post_title = "Edit Post" %>

<%= link_to("<< Back to List", {:action => 'index', :project_id => @project.id}, :class => 'back-link') %>

<div class="Posts edit">
  <h2>Update Post</h2>

  <%= form_for(:post, :url => {:action => 'update', :id => @post.id, :project_id => @project.id}) do |f| %>

    <%= render(:partial => "form", :locals => {:f => f}) %>

    <div class="form-buttons">
      <%= submit_tag("Update Post") %>
    </div>

  <% end %>
</div>






<%- model_class = Post -%>
<div class="Post-header">
  <h1><%=t '.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<%= render :partial => 'form' %>

new.html.erb

<% @post_title = "New post" %>

<%= link_to("<< Back to List", {:action => 'index', :project_id => @project.id}, :class => 'back-link') %>

<div class="posts new">
  <h2>Create post</h2>

  <%= form_for(:post, :url => {:action => 'create', :project_id => @project.id}) do |f| %>

    <%= render(:partial => "form", :locals => {:f => f}) %>

    <div class="form-buttons">
      <%= submit_tag("Create post") %>
    </div>

  <% end %>
</div>




<%- model_class = Post -%>
<div class="page-header">
  <h1><%=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<div :class 'control-label'>
<%= @post.parent.inspect  if @post.parent %>

</div>
<%= render :partial => 'form' %>

_form.html.erb

<%= form_for(@post, :html => {:multipart => true, :class => "form-horizontal post" }) do |f| %>

    <% if @post.errors.any? %>
    <div id="error_expl" class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title"><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h3>
      </div>
      <div class="panel-body">
        <ul>
        <% @post.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    </div>
  <% end %>
<%=f.hidden_field :parent_id %>

  <div class="control-group">
    <%= f.label :user_id, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :user_id, :class => 'form-control' %>
    </div>
    <%= error_span(@post[:user_id]) %>
  </div>
  <div class="control-group">
    <%= f.label :content, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :content, :class => 'form-control' %>
    </div>
    <%= error_span(@post[:content]) %>
  </div>
  <p>
  <strong>Avatar:</strong>
  <%= image_tag @post.avatar.url(:preview) %>
</p>
  <%= f.file_field :avatar %>

  <%= f.submit nil, :class => 'btn btn-primary' %>
  <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
            posts_path, :class => 'btn btn-default' %>

<% end %>

rake routes

rake routes
                  Prefix Verb     URI Pattern                                    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
           project_posts GET      /projects/:project_id/posts(.:format)          posts#index
                         POST     /projects/:project_id/posts(.:format)          posts#create
        new_project_post GET      /projects/:project_id/posts/new(.:format)      posts#new
       edit_project_post GET      /projects/:project_id/posts/:id/edit(.:format) posts#edit
            project_post GET      /projects/:project_id/posts/:id(.:format)      posts#show
                         PATCH    /projects/:project_id/posts/:id(.:format)      posts#update
                         PUT      /projects/:project_id/posts/:id(.:format)      posts#update
                         DELETE   /projects/:project_id/posts/:id(.:format)      posts#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
             users_index GET      /users/index(.:format)                         users#index
              users_show GET      /users/show(.:format)                          users#show
                  follow GET      /follow(.:format)                              users#follow
                unfollow GET      /unfollow(.:format)                            users#unfollow
                    like GET      /like(.:format)                                users#like
                  unlike GET      /unlike(.:format)                              users#unlike
                         GET|POST /:controller(/:action(/:id))(.:format)         :controller#:action

Aucun commentaire:

Enregistrer un commentaire