3

开始通过 Lynda 学习 Ruby on Rails - 非常兴奋,并且正在尽我最大的努力练习。我正在关注练习,但培训是基于 Rails 3 - 到目前为止,有些用途不被接受。

情况如下:

我到达主题/新的创建表单填写表单得到以下错误作为回报

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

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

我花了最后 2 个小时在 stackoverflow、rail guide 和所有其他资源中徘徊 - 尝试了许多变体,但无法超越这个阶段。

对你的帮助表示感谢。

路线.rb

SimpleCms::Application.routes.draw do

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

end

subject_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

新的.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

4 回答 4

3

naomik 的回答肯定会帮助表单更简洁,但听起来你只需要在config/routes.rb文件中为主题添加一条路线:

SimpleCms::Application.routes.draw do

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

end

Rails 路由指南中的更多信息。

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

于 2013-08-27T17:47:15.857 回答
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 create 控制器如下:

    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

希望有帮助!

于 2014-01-12T23:09:38.223 回答
2

如果你只是这样做,你不应该有任何问题

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

助手将根据模型的状态form_for自动选择正确的(惯用action的) method

如果@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' %>
于 2013-08-27T17:42:28.037 回答
0

此时我在 Lynda.com 课程中遇到了同样的问题。它是通过添加resources :subjects上面解决的get ':controller(/:action(/:id(.:format)))',然后将 subject_controllercreate操作更改为

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 的主题”。为了解决这个问题,我get 'subjects/list' => 'subjects#list'在路线上方添加了resources :subjects(我不知道这是否是“正确”的事情,但它现在有效)。

于 2013-10-05T12:02:46.690 回答