没有路由匹配{:action =>" index",:controller =>"功能",:grounddetail_id => nil}缺少必需的密钥:[:grounddetail_id]

时间:2016-01-24 16:03:46

标签: ruby-on-rails ruby ruby-on-rails-4

我想获取功能索引页面而不通过grounddetail id。当我尝试在主页中创建链接时,我总是会收到此错误:

没有路由匹配{:action =>" index",:controller =>"功能",:grounddetail_id => nil}缺少必需的键:[:grounddetail_id]

<%= link_to "Account Setting", edit_admin_registration_path %> |
<%= link_to "Log Out", destroy_admin_session_path, method: :delete %> |
<%= link_to "Featured Ground", grounddetail_features_path(@grounddetail)  %> # Feature Link Where I get error

grounddetail控制器看起来:

class GrounddetailsController < ApplicationController

  before_action :find_ground, only: [:show, :edit, :destroy, :update]

  def index
    @grounddetails = Grounddetail.all.order('created_at DESC')
    @grounddetail = Grounddetail.first # With this code it run but with grounddetail id. 
  end

  def new
    @grounddetail = Grounddetail.new
  end

  def edit
  end

  def show
  end

  def create
    @grounddetail = Grounddetail.new(ground_params)
    if @grounddetail.save
      redirect_to @grounddetail
    else
      render 'new'
    end
  end

  def update
    if @grounddetail.update(ground_params)
      redirect_to @grounddetail
    else
      render 'edit'
    end
  end

  def destroy
    @grounddetail.destroy
    redirect_to root_path
  end


  private

  def find_ground
    @grounddetail = Grounddetail.find(params[:id])
  end

  def ground_params
    params.require(:grounddetail).permit(:name, :working_hours, :end_time, :address, :contact_no, :email, :number_of_grounds, :description, :featured_ground)
  end

end

功能控制器如下所示:

class FeaturesController < ApplicationController

  before_action :set_ground

  def index
    @grounddetails = Grounddetail.where(featured_ground: true).order("created_at DESC")
    @features = Feature.includes(:grounddetail).where(grounddetail_id: @grounddetail.id)
  end

  def new
    @feature = Feature.new
  end

  def show
    @feature = Feature.find(params[:id])
  end

  def edit
    @feature = Feature.find(params[:id])
  end

  def create
    @feature = Feature.create(feature_params)
    @feature.grounddetail_id = @grounddetail.id

    if @feature.save
      redirect_to grounddetail_features_path(@grounddetail)
    else
      render 'new'
    end
  end

  def update
    @feature = Feature.find(params[:id])
    if @feature.update(feature_params)
      redirect_to grounddetail_features_path(@grounddetail)
    else
      render 'edit'
    end
  end

  def destroy
    @feature = Feature.find(params[:id])
    @feature.destroy
    redirect_to grounddetail_features_path(@grounddetail)
  end

  private

  def set_ground
    @grounddetail =  Grounddetail.find(params[:grounddetail_id])
  end

  def feature_params
    params.require(:feature).permit(:featuring_start_time, :featuring_end_at)
  end

end

型号:

grounddetail模型:

has_many :features

特征模型:

belongs_to :grounddetail

index.html.erb #feature索引页

<h2>Featured Ground</h2>

<% @grounddetails.each do |grounddetail| %>

  Name: <%= link_to grounddetail.name, grounddetail %><br>
  Address: <%= grounddetail.address %><br>
  Opening Hours: From<%= grounddetail.working_hours.strftime("%l:%M %P") %> To <%= grounddetail.end_time.strftime("%l:%M %P") %><br>
  Featured : <%= check_box "Grounddetail", "Featured Ground", {checked: grounddetail.featured_ground, disabled: true} %><br>

  <% if (grounddetail.featured_ground = true) && (grounddetail_id = @grounddetail.id) %>
    <% @features.each do |feature| %>
      Featuring Start Time : <%= feature.featuring_start_time.strftime('%e %b %Y %l:%M %P') %><br>

      <%# feature.start_date_cannot_be_in_the_past %>
      Featuring End At : <%= feature.featuring_end_at.strftime('%e %b %Y %l:%M %P') %><br>
    <% end %>
  <% end %>
  <%= link_to "Add Featuring Time", new_grounddetail_feature_path(@grounddetail) %><br><br>
<% end %>

的routes.rb

resources :grounddetails do 
    resources :features
end

抱歉搞砸了..

0 个答案:

没有答案