0
似乎無法弄清楚問題在這裏。所以我有一個has_many條目的日誌模型。在我的路線中,我把它設置爲:讓我的嵌套資源在Rails 4中工作
resources :logs, shallow: true do
resources :entries
end
resources :entries
1)我是否需要單獨的條目資源在底部?這似乎與我使用的表單有關。
在我的日誌部分我有一個表,然後呈現部分在底部的鏈接條目創建新條目:
<tbody>
<%= render(log.entries) %>
</tbody>
<%= link_to 'New Entry', new_log_entry_path(@log) %>
和/ _form.html.erb看起來像這樣的條目:
<%= form_for(@entry) do |f| %>
<%= f.hidden_field :log_id %>
<div class="field">
<%= f.label :date %><br>
<%= f.date_select :date %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.select(:name, [['Bench Press', 1], ['Military Press', 2], ['Squat', 3], ['Deadlift', 4], ['Barbell Row', 5]]) %>
</div>
...
...
<div class="actions">
<%= f.submit %>
</div>
<% end %>
最後,我的控制器代碼,我有一個名爲CurrentLog關注:
module CurrentLog
extend ActiveSupport::Concern
private
def set_log
@log = Log.find(params[:log_id])
end
end
在我的表項,開始是:
class EntriesController < ApplicationController
include CurrentLog
before_action :set_log
before_action :set_entry, only: [:show, :edit, :update, :destroy]
# GET /entries
# GET /entries.json
def index
@entries = Entry.all
end
# GET /entries/1
# GET /entries/1.json
def show
@entry = Entry.new
end
# GET /entries/new
def new
@entry = Entry.new(log_id: params[:log_id])
end
# GET /entries/1/edit
def edit
end
# POST /entries
# POST /entries.json
def create
@entry = Entry.new(entry_params)
respond_to do |format|
if @entry.save
format.html { redirect_to @entry.log, notice: 'Entry was successfully created.' }
format.json { render action: 'show', status: :created, location: @entry }
else
format.html { render action: 'new' }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
end
現在的問題是:
當我創建,編輯或刪除條目,它不正確匹配的1 LOG_ID(以這種情況下)與條目。
在使用表單創建新條目之後,而不是進入log/1/entries/xx,它直接進入條目/ xx,並且不會從表單中攜帶適當的信息。我在這裏做錯了什麼?很抱歉,這篇長文章,我希望我儘可能簡潔!