Rails问题与新操作/页面

时间:2016-11-03 16:13:59

标签: ruby-on-rails ruby routes views action

我正在构建一个基本的新闻应用程序。我有一个“帖子”控制器,控制基本索引,显示,等。我正在尝试使用名为“local_news”的相应视图添加新操作。此页面将显示特别标记为出现在那里的新闻报道。

这是我的链接,指向新页面“local_new.html.erb”:

<li><%= link_to "Local", posts_local_news_path, class: 'nav_link' %></li>

当我尝试关注链接时出现此错误:

ActiveRecord::RecordNotFound

Extracted source (around line #74):

# Use callbacks to share common setup or constraints between actions.
def set_post
  @post = Post.friendly.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.

我不确定这里发生了什么。这是我的帖子控制器:

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, only: [:edit, :update, :destroy, :new, :create]

# GET /posts
# GET /posts.json
def index
  @posts = Post.most_recent
end

def local_news

end

# GET /posts/1
# GET /posts/1.json
def show
   @posts = Post.most_recent
end

# GET /posts/new
def new
  @post = Post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts
# POST /posts.json
def create
  @post = Post.new(post_params)

  respond_to do |format|
    if @post.save
      format.html { redirect_to post_path(@post), 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
respond_to do |format|
  if @post.update(post_params)
    format.html { redirect_to @post, 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
  @post.destroy
  respond_to do |format|
    format.html { redirect_to posts_url, 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.friendly.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def post_params
    params.require(:post).permit(:title, :content, :description, :photo, :ranking, :photo_file_name, :caption)
  end
end

这是路线文件:

Rails.application.routes.draw do
  get 'homes/show'

  devise_for :users
  resources :posts


  resources :homes, only: [:show]

  get 'posts/local_news', to: "posts#local_news"
  get "pages/contact" => "pages#contact"
  get "pages/about" => "pages#about"
  get "pages/election2016" => "pages#election2016"

  root "posts#index"
  # For details on the DSL available within this file, see     http://guides.rubyonrails.org/routing.html
end

当我运行rake路线时出现路线。但我无法打开页面。最后,我想让@posts进入“local_news”页面,就像他们目前使用“index”一样。但是现在,我只想要一个没有红宝石的基本html页面。它不会这样做。有人可以帮忙吗?我似乎无法找到有关此特定问题的任何文档。

0 个答案:

没有答案