ruby on rails - No route matches {:action=>"index", :controller=>"features", :grounddetail_id=>nil} missing required keys: [:grounddetail_id] -
i wanna fetch feature index page without going through grounddetail id. when try create link in homepage error:
no route matches {:action=>"index", :controller=>"features", :grounddetail_id=>nil} missing required keys: [: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 error
grounddetail controller looks :
class grounddetailscontroller < applicationcontroller before_action :find_ground, only: [:show, :edit, :destroy, :update] def index @grounddetails = grounddetail.all.order('created_at desc') @grounddetail = grounddetail.first # code run 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
feature controller looks :
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
models:
grounddetail model:
has_many :features
feature model:
belongs_to :grounddetail
index.html.erb #feature index page
<h2>featured ground</h2> <% @grounddetails.each |grounddetail| %> name: <%= link_to grounddetail.name, grounddetail %><br> address: <%= grounddetail.address %><br> opening hours: from<%= grounddetail.working_hours.strftime("%l:%m %p") %> <%= 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 |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 @ : <%= 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 resources :features end
sorry mess up..
Comments
Post a Comment