/ YYYY / MM / Title-Slug URL结构,在编辑操作中出现Friendly_Id抛出错误

时间:2017-04-27 12:45:05

标签: ruby-on-rails

美好的一天,

请原谅我的noob问题。

在我的rails应用程序中,我有/ YYYY / MM / DD / Title-Slug URL结构,带有Friendly_Id。

news_controller.rb

class NewsController < ApplicationController
before_action :set_news, only: [:show, :edit, :update]
before_action :authenticate_user!, except: [:show, :index, :search ]
def index
if params[:search]

   @news = News.search(params[:search]).paginate(:page => params[:page],  :per_page => 12).order("created_at DESC")

 else
@news = News.where( state: true ).paginate(:page => params[:page],  :per_page => 12).order('id DESC')
end
end

def latest_news



if current_user.admin?
@news = News.paginate(:page => params[:page],  :per_page => 12).order('id DESC')
else
 flash[:error] = "you are not authorised to visit this section"
redirect_to root_path
end 

end 

def new
if current_user.state == true
  @news = News.new
 else
  flash[:error] = "your account has not been activated"
  redirect_to root_path
 end

end

 def show
 @news_photos = @news.news_photos

 end



  def create
    @news = News.new(news_params)
    respond_to do |format|
    if @news.save
      if params[:images]
        params[:images].each do |image|
          @news.news_photos.create(image: image)
        end
      end

      @news_photos = @news.news_photos


      format.html { redirect_to show_news_path(@news.year,@news.month,@news.date,@news ), notice: 'News was successfully created.' }
      format.json { render :show, status: :created, location: @news }

    else
      render :new
    end
  end
  end

  def edit
     @news_photos = @news.news_photos
  end

   def update
      respond_to do |format|
      if current_user.admin?
        if @news.update(admin_news_params)

         if params[:images]
            params[:images].each do |image|
            @news.news_photos.create(image: image)
            end
          end

        #redirect_to root_path, notice: "Updated..."
       # redirect_to latest_news_path, notice: "Updated..."
         format.html { redirect_to show_news_path(@news.year,@news.month,@news.date,@news ), notice: 'News was successfully edited' }
        # format.json { render :show, status: :created, location: @news }
        else
         render :edit
        end
      else  
        if @news.update(news_params)
          if params[:images]
            params[:images].each do |image|
            @news.news_photos.create(image: image)
            end
          end
        #  redirect_to news_index_path, notice: "Updated..."
          #redirect_to latest_news_path, notice: "Updated..."
           format.html { redirect_to show_news_path(@news.year,@news.month,@news.date,@news ), notice: 'News was successfully edited.' }
        #   format.json { render :show, status: :created, location: @news }
        else
          render :edit
        end
      end
    end
   end


  def destroy
    @news = News.find(params[:id])
    @news.destroy

    respond_to do |format|
      format.html { redirect_to news_index_path }
     # format.json { head :no_content }
   end
 end







private
  def set_news
    @news = News.find(params[:id])
  end 

def news_params
    params.require(:news).permit( :title, :description, :category, :keywords, :user_id, :email, :user_name)
end
def admin_news_params
    params.require(:news).permit( :title, :description, :category, :keywords, :user_id, :email, :user_name, :state)
end


end

news_helper.rb

module NewsHelper
  def show_news_path(news)
    "/news/#{news.year}/#{news.month}/#{news.date}/#{news.slug}"
  end

end

news.rb

class News < ApplicationRecord
extend FriendlyId
 include PgSearch
 has_many :news_photos
friendly_id :slug_candidates, use: [:slugged, :finders, :history]

 def slug_candidates 
  [ :title,
   [:title, :id]
   ] 
end

def year
    created_at.localtime.strftime("%Y")
  end

  def month
    created_at.localtime.strftime("%m")
  end

  def date
    created_at.localtime.strftime("%d")
  end

end

的routes.rb

Rails.application.routes.draw do

resources :news, :except => [:latest_news, :search, :show, :edit, :update]

 root :to => "news#index"

 scope 'news' do

   get     '/:year/:month/:date/:id',      to: 'news#show',   as: 'show_news'

    get     '/:year/:month/:date/:id/edit', to: 'news#edit',   as: 'edit_news'

    patch   '/:year/:month/:date/:id',      to: 'news#update'
    put     '/:year/:month/:date/:id',      to: 'news#update'
    delete  '/:year/:month/:date/:id',      to: 'news#destroy'
  end



end

_form.html.erb

<div class="row">
  <div class="col-md-8 col-md-offset-2">
    <%= form_for @news, :html => { :class => 'form-horizontal', multipart: true } do |f| %>
      <div class="row">
          <div class="col-md-12">
            <%= f.label :title, :class => 'control-label' %>
            <div class="form-group">
              <%= f.text_field :title, :class => 'form-control' %>
            </div>  
          </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <%= f.label :keywords, :class => 'control-label' %>
            <div class="form-group">
              <%= f.text_field :keywords, :class => 'form-control' %>
            </div>
          </div>  
        </div>  
         <div class="row">
          <div class="col-md-12">
            <%= f.label :description, :class => 'control-label' %>
            <div class="form-group">
              <%= f.text_area :description, :class => 'form-control', id: "input" %>
            </div>
          </div>  
        </div>

        <div class="row">
          <div class="col-md-12">
            <label>Upload Image</label>
            <div class="form-group">
              <span class="btn btn-success btn-file">
                  <i class="fa fa-cloud-upload fa-lg"></i> Upload Photos
                    <%= file_field_tag "images[]", type: :file, multiple: true %>
              </span>    
            </div>
          </div>  
        </div>

        <div class="row">
          <div class="col-md-6 col-md-offset-3">
            <%= f.submit nil, :class => 'btn btn-info' %>
          </div>
        </div>  
    <% end %>
  </div>
</div>    

这是有效的:

  • 新闻/索引

  • / news / 2017/4/27 / example

  • 创建新帖子
  • 销毁帖子

什么行不通:

编辑帖子(带有表单的编辑页面将呈现,但在提交我的更改后,我收到上述错误 - 并且更改从未进入数据库)

错误:

  

ActionController :: RoutingError(没有路由匹配[PATCH]“/ news / example”):

我已经完成了以下帖子以实现基于日期的slu :: /YYYY/MM/Title-Slug URL structure with Friendly_Id Solution Chokes on #edit

我不理解我在news_helper.rb中出错了什么

任何帮助都是高度赞赏的。谢谢你提前

2 个答案:

答案 0 :(得分:1)

这是因为您已禁用路线中的update操作

resources :news, :except => [:latest_news, :search, :show, :edit, :update]

解决方案是允许updateexcept子句中删除{<1}}

resources :news, except: [:latest_news, :search, :show, :edit]

除非他们&#34; view-source&#34;因此,对于可用性而言并不重要。

答案 1 :(得分:1)

尝试以下代码:

Rails.application.routes.draw do

resources :news, :except => [:latest_news, :search, :show, :edit]

删除:从except更新