rails 4:使用form_for,没有路由,POST

时间:2013-08-27 17:40:33

标签: ruby-on-rails routes

开始通过Lynda学习Ruby on Rails - 非常兴奋,并尽我所能尽力练习。我正在进行练习,但是训练是基于Rails 3 - 到现在为止一些用途都没有被接受。

情况如下:

我到达subject / new的创建表单 填写表格 收到以下错误

  

没有路线匹配[POST]“/ subjects / create”

     

Rails.root:/ Users / alpozenalp / Sites / simple_cms

我花了最近2个小时在stackoverflow,铁路指南和所有其他来源周围徘徊 - 尝试了很多变化,但无法通过这个阶段。

非常感谢您的帮助。

的routes.rb

SimpleCms::Application.routes.draw do

root :to => "demo#index"
get ':controller(/:action(/:id(.:format)))'

end

subjects_controller.rb

class SubjectsController < ApplicationController

def index
list
render('list')
end

def list
@subjects = Subject.order("subjects.position ASC")
end

def show
@subject = Subject.find(params[:id])
end

def new
@subject = Subject.new
end

def create
# Instantiate a new object using form parameters
@subject = Subject.new(params[:subject])
# Save the object
if @subject.save
# If save succeeds, redirect to the list action
redirect_to(:action => 'list')
else
# If save fails, redisplay the form so user can fix problems
render('new')
end
end
end

new.html.erb

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

<div class="subject new">
  <h2>Create Subject</h2>

  <%= form_for(:subject, :url => {:action => 'create'}, :method => :post) do |f| %>

    <table summary="Subject form fields">
      <tr>
        <th>Name</th>
        <td><%= f.text_field(:name) %></td>
      </tr>
      <tr>
        <th>Position</th>
        <td><%= f.text_field(:position) %></td>
      </tr>
      <tr>
        <th>Visible</th>
        <td><%= f.text_field(:visible) %></td>
      </tr>
    </table>

    <div class="form-buttons">
      <%= submit_tag("Create Subject") %>
    </div>

  <% end %>
</div>

4 个答案:

答案 0 :(得分:3)

naomik的回答肯定会帮助表格更清晰,但听起来你只需要在config/routes.rb档案中为主题添加路线:

SimpleCms::Application.routes.draw do

  resources :subjects    
  root :to => "demo#index"

end

Rails routing guide中的更多信息。

编辑:根据naomik的建议删除默认的后备路由。

答案 1 :(得分:2)

如果你这样做,你应该没有任何问题

<%= form_for @subject do |f| %>

form_for帮助器会根据模型的状态自动选择正确的(惯用的)actionmethod

如果@subject是新记录,您将获得

<form action="/subjects" method="post">
  ...

如果@subject是现有记录(ID为1),您将获得

<form action="/subjects/1" method="post">
  <input type="hidden" name="_method" value="put">
  ...

额外:您的list行动似乎毫无意义。只需按预期使用index即可。

然后这段代码

<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>

成为这个

<%= link_to '&laquo; Back to List'.html_safe, subjects_path, class: 'back-link' %>

答案 2 :(得分:2)

我已经完成了课程,我的route.rb如下所示:

Cms2::Application.routes.draw do  

root to: "public#index"
get 'admin', :to => 'access#menu'
get 'show/:id', :to => 'sections#show'
get ':controller(/:action(/:id(.:format)))'  

post "admin_users/update"
post "subjects/update"
post "pages/update"
post "sections/update"

post "subjects/destroy"
post "subjects/create"

post "pages/destroy"
post "pages/create"

post "sections/destroy"
post "sections/create"

post "admin_users/destroy"
post "admin_users/create"

post "access/attempt_login"

get "access/logout"

end

我的def创建控制器如下:

    def create
#new_position = params[:subject].delete(:position)
# Instantiate a new object using form parameters
@subject = Subject.new(params.require(:subject).permit(:name, :position, :visible, :created_at, :updated_at))
# Save the object
if @subject.save
  #@subject.move_to_position(new_position)
  # If save succeeds, redirect to the list action
  flash[:notice] = "Subject Created."
  redirect_to(:action => 'list')
else
  # If save fails, redisplay the form so user can fix problems
  @subject_count = Subject.count +1
  render('new')
end
end

希望有所帮助!

答案 3 :(得分:0)

我在Lynda.com课程中遇到了同样的问题。通过在resources :subjects上方添加get ':controller(/:action(/:id(.:format)))',然后将subject_controller create操作更改为

来解决问题
def create
    @subject = Subject.new(params.require(:subject).permit(:name, :position, :visible))
    if @subject.save
        redirect_to(:action => 'list')
    else
        render('new')
    end
end

这规避了以前写过的动作所发生的禁止属性错误。

因为我添加了resources :subjects,这意味着上面的redirect_to(:action => 'list')会产生错误,而且无法找到ID = list&#的主题39 ;.为了解决这个问题,我在get 'subjects/list' => 'subjects#list'路线上方添加了resources :subjects(我不知道这是否是正确的事情,但它现在有效)。