方案
我的Rails项目中有四个资源,即。 Drive,Drivecomment,College和Manager
resources :drives do
resources :drivecomments
end
devise_for :colleges, controllers: { sessions: "college/sessions", registrations: "college/registrations" }
devise_for :managers, controllers: { sessions: "manager/sessions", registrations: "manager/registrations" }
它们之间的关联如下:
Manager => belongs_to :college
College => has_many :managers
College => has_many :drives
Drives => belongs_to :college
Drives => has_many :drivecomments
Drivecomments => belongs_to :drives
从上面的关联可以清楚地看出,每个云端硬盘都可以有多个Drivecomments,可以由管理员提交。
另外,我创建了一个自定义Controller #View资源" m"用于提供管理器接口(coz默认管理器是从设计生成并使用相同的资源意味着所有后期操作都被重定向到"创建"方法,包括sign_up,这最终意味着我根本无法签名)
resources :m, :only => [:index] do
member do
get :drive
end
end
所以,在我的" m"中,我创建了一个用于发布评论的表单以及一个用于显示所有评论的表。
用于显示评论:
../drive.html.erb
<h2>Drive Comments</h2>
<table>
<thead>
<tr>
<th>Jobseeker</th>
<th>Comment</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @comments.each do |comment| %>
<tr>
<td><%= comment.jobseeker.first_name %></td>
<td><%= comment.title %></td>
</tr>
<% end %>
</tbody>
</table>
../m_controller.erb
def drive
@drive = @drives.find_by_id(params[:id])
@comments = @drive.drivecomments
@addcomment = Drivecomment.new
end
添加评论:
../drive.html.erb
<%= form_for [@addcomment], as: :addcomment, url: {action: "addcomment"} do |f| %>
<div><%= f.hidden_field :drive_id, :value => @drive.id %></div>
<div><%= f.hidden_field :jobseeker_id, :value => 2 %></div>
<div><%= f.label "Comment:" %><%= f.text_field :title %></div>
<div><%= f.submit "Post Comment" %></div>
<% end %>
../m_controller.erb
def new
@addcomment = Drivecomment.new
end
def create
@drive = @drives.find_by_id(params[:id])
@addcomment = @drive.drivecomments.build
@addcomment.save
end
问题
当我提交表单时,我收到错误消息
显示/home/xxx/xxx/xxx/app/views/m/drive.html.erb第30行:
没有路线匹配{:action =&gt;&#34; addcomment&#34;,:controller =&gt;&#34; m&#34;,:id =&gt;&#34; 5&# 34;}
提取的来源(第30行):
</table>
<%= form_for [@addcomment], as: :addcomment, url: {action: "addcomment"} do |f| %>
<div><%= f.hidden_field :drive_id, :value => @drive.id %></div>
<div><%= f.hidden_field :jobseeker_id, :value => 2 %></div>
或者,如果我像这样写我的表格
<%= form_for [@drive, @addcomment] do |f| %>
<div><%= f.hidden_field :drive_id, :value => @drive.id %></div>
<div><%= f.hidden_field :jobseeker_id, :value => 2 %></div>
<div><%= f.label "Comment:" %><%= f.text_field :title %></div>
<div><%= f.submit "Post Comment" %></div>
<% end %>
它成功提交表单并添加了一个值,但我被重定向到&#34; http://localhost:3000/drives/5/drivecomments/8&#34;来自&#34; http://localhost:3000/m/5/drive&#34;这是我不想要的。 请告诉我这个问题的解决方法可能是什么。
其他信息
Drivecomments _form.html.erb
<%= form_for([@drive, @drivecomment]) do |f| %>
<% if @drivecomment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@drivecomment.errors.count, "error") %> prohibited this drivecomment from being saved:</h2>
<ul>
<% @drivecomment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :jobseeker_id %><br>
<%= f.number_field :jobseeker_id %>
</div>
<div class="field">
<%= f.label :drive_id %><br>
<%= f.number_field :drive_id %>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_area :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
从drivecoments_controller.rb中提取
before_action :set_drivecomment, only: [:show, :edit, :update, :destroy]
before_filter :load_drive
def new
@drivecomment = @drive.drivecomments.new
end
def create
@drivecomment = @drive.drivecomments.new(drivecomment_params)
respond_to do |format|
if @drivecomment.save
format.html { redirect_to [@drive, @drivecomment], notice: 'Drivecomment was successfully created.' }
format.json { render :show, status: :created, location: @drivecomment }
else
format.html { render :new }
format.json { render json: @drivecomment.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_drivecomment
@drivecomment = Drivecomment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def drivecomment_params
params.require(:drivecomment).permit(:jobseeker_id, :drive_id, :title)
end
def load_drive
@drive = Drive.find(params[:drive_id])
end
答案 0 :(得分:1)
如果您想要从控制器.cfc
使用操作create
,您应该像这样定义表单:
m
此外,您需要在路线上定义<%= form_for @addcomment, url: url_for(controller: :m, action: :create) do |f| %>
操作,更新此部分路线:
create