路线:
resources :news_items, :path => 'news', :as => 'news' do
collection do
get 'all' => 'news_items#news'
end
控制器:
def new
@news = NewsItem.new
@news.file_uploads.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @news }
end
end
形式:
<%= form_for(@news, :html => {:class=>'form-stacked', :multipart => true}) do |f| %>
<% if @news.errors.any? %>
当我运行rake路线时
all_news_index GET /news/all(.:format) {:action=>"news", :controller=>"news_items"}
news_index GET /news(.:format) {:action=>"index", :controller=>"news_items"}
POST /news(.:format) {:action=>"create", :controller=>"news_items"}
new_news GET /news/new(.:format) {:action=>"new", :controller=>"news_items"}
edit_news GET /news/:id/edit(.:format) {:action=>"edit", :controller=>"news_items"}
news GET /news/:id(.:format) {:action=>"show", :controller=>"news_items"}
PUT /news/:id(.:format) {:action=>"update", :controller=>"news_items"}
DELETE /news/:id(.:format) {:action=>"destroy", :controller=>"news_items"}
当我去'/ news / new'时,我得到了:
No route matches {:path_prefix=>"news", :controller=>"news_items", :format=>nil}
当我去'/ news / 4 / edit'时,我得到:
No route matches {:path_prefix=>"news", :action=>"show", :controller=>"news_items", :format=>nil, :id=>#<NewsItem id: 4,...
答案 0 :(得分:0)
我认为问题出在form_for(@news)
行。
这将创建一个表单,其URL由@news类推断。
这似乎属于NewsItem
课程,因此它会尝试链接到news_items_path
。
您应该尝试明确说明网址。
form_for(@news, :url => news_path(@news))