【问题标题】:Forms, nested resources and routing errors Rails 4表单、嵌套资源和路由错误 Rails 4
【发布时间】:2014-04-07 03:02:32
【问题描述】:

Rails 新手在这里。我创建了一个基本网站,用于跟踪轮班期间的事件。我已经创建了模型并嵌套了它们。但是,我不完全了解这些联系。 当我从链接到班次的表单创建嵌套事件时,出现以下错误:

在 url http://localhost:3000/shifts//events 上没有路由匹配 [POST] "/shifts/events"

我认为应该是http://localhost:3000/shifts/3/events/1

_form.html.erb

<%= form_for shift_events_path(@shift) do |f| %>
<% if @event.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@event.errors.count, "error") %> prohibited </h2>

  <ul>
  <% @event.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
  <%= f.label :time_s %><br>
  <%= f.time_select :time_s %>
</div>
<div class="field">
  <%= f.label :time_f %><br>
  <%= f.time_select :time_f %>
</div>
<div class="field">
  <%= f.label :odometre %><br>
  <%= f.number_field :odometre %>
</div>
<div class="field">
  <%= f.label :location %><br>
  <%= f.text_field :location %>
</div>
<div class="field">
  <%= f.label :activity %><br>
  <%= f.text_field :activity %>
</div>
<div class="field">
  <%= f.label :description %><br>
  <%= f.text_area :description, rows: 4 %>
</div>
<div class="actions">
  <%= f.submit %>
</div>
<% end %>

roots.rb

resources :shifts do
  resources :events#, 
end

events_controller.eb

class EventsController < ApplicationController
before_filter :get_shift
before_action :set_event, only: [:show, :edit, :update, :destroy]

# GET /events
# GET /events.json
def index
  @events = @shift.events
end

# GET /events/1
# GET /events/1.json
def show
end

# GET /events/new
def new
  @event = @shift.events.build
end

# GET /events/1/edit
def edit
end

# POST /events
# POST /events.json
def create
  @event = @shift.events.build(event_params)

  respond_to do |format|
    if @event.save
      format.html { redirect_to shift_event_path, notice:
                                         'Event was successfully   created.' }
      format.json { render action: 'show', status: :created, location: @event }
    else
      format.html { render action: 'new' }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
  respond_to do |format|
    if @event.update(event_params)
      format.html { redirect_to shift_event_path, notice: 
                                          'Event was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /events/1
# DELETE /events/1.json
def destroy
  @event.destroy
  respond_to do |format|
    format.html { redirect_to shift_event_path }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_event
    @event = @shift.events.find(params[:id])
  end

  def get_shift
    @shift = Shift.find(params[:shift_id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def event_params
    params.require(:event).permit(:time_s, :time_f, :odometre,
                           :location, :activity, :shift_id)
  end
end

路线

shift_events_path    GET    /shifts/:shift_id/events(.:format)   events#index
                     POST   /shifts/:shift_id/events(.:format)   events#create
new_shift_event_path GET    /shifts/:shift_id/events/new(.:format) events#new
edit_shift_event_path GET   /shifts/:shift_id/events/:id/edit(.:format) events#edit
shift_event_path     GET    /shifts/:shift_id/events/:id(.:format) events#show
                     PATCH  /shifts/:shift_id/events/:id(.:format) events#update
                     PUT    /shifts/:shift_id/events/:id(.:format) events#update
                     DELETE /shifts/:shift_id/events/:id(.:format) events#destroy
  shifts_path        GET    /shifts(.:format)                    shifts#index
                     POST   /shifts(.:format)                    shifts#create
new_shift_path       GET    /shifts/new(.:format)                shifts#new
edit_shift_path      GET    /shifts/:id/edit(.:format)           shifts#edit
shift_path           GET    /shifts/:id(.:format)                shifts#show
                     PATCH  /shifts/:id(.:format)                shifts#update
                     PUT    /shifts/:id(.:format)                shifts#update
                     DELETE /shifts/:id(.:format)                shifts#destroy
 root_path           GET    /                                    shifts#index 

我认为表单的第一行是错误的,但我不知道要改成什么。其次,我认为事件控制器中的一些连接是错误的。任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails-4 routing nested-resources


    【解决方案1】:

    试试这个:

    <%= form_for [@shift, @event] do |f| %>
    

    现在您将为特定班次发布特定事件。

    【讨论】:

    • 好的,但正如我所说,我在事件控制器中有连接错误。没有路线匹配 {:action=>"show", :controller=>"events", :shift_id=>"3"} 缺少必需的键:[:id]
    • 最好先阅读此 Rails 指南:guides.rubyonrails.org/…。本指南提供了一个很好的嵌套模型示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多