我阅读了所有可以抓住的一半,虽然有一些非常清楚的解释我想要做什么,但我找不到最新的完整教程。我有页面和类别。 Pages has_many类别和类别has_many。我还设置了第3个模型categories_pages并创建了一个索引连接表,其中包含2列(category_id& page_id)。所以我的模特目前是:
class Categorization < ActiveRecord::Base
belongs_to :category
belongs_to :page
end
class CategoriesPages < ActiveRecord::Base
belongs_to :category
belongs_to :page
end
class Page < ActiveRecord::Base
#categories
has_many :categories_pages
has_many :categories, :through => :categories_pages
end
然后我有一个这样的表格(请忽略上面的页面模型中缺少回形针代码..我从这个例子中剪了它但可以确认上传文件应该如此)。
<%= form_for(@page) do |f| %>
<% if @page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@page.errors.count, "error") %> prohibited this page from being saved </h2>
<ul>
<% @page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<form role="form">
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, :class => 'form-control' %>
</div>
<div class="form-group">
<%= f.label :text %><br>
<%= f.text_field :text, :class => 'form-control' %>
</div>
<div class="form-group">
<%= form_for @page, :url => pages_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :image %>
<% end %>
</div>
<div class="form-group">
<%= f.fields_for :category do |cat| %>
<%= cat.label :name, 'Category' %> </br>
<%= cat.text_field :name %>
<% end %>
</div>
<div class="form-group">
<%= f.label :published %><br>
<%= f.check_box :published, :class => 'checkbox' %>
</div>
<div class="form-group">
<%= f.label :seo_keywords, 'SEO Keywords' %><br>
<%= f.text_field :seo_keywords, :class => 'form-control' %>
</div>
<div class="form-group">
<%= f.label :seo_description, 'SEO Description' %><br>
<%= f.text_field :seo_description, :class => 'form-control' %>
</div>
<div class="action">
<%= f.submit 'Save', :class => 'btn btn-default' %>
</div>
</form>
<% end %>
我的问题是我将什么放入控制器/ s(假设上面的代码检出)。我还没有找到关于此的最新信息。 Railscasts已经过时了,我读的越多(我从昨天起就一直试图解决这个问题),我越是圈起来。目前,我在编辑或新视图中进行了表单渲染,但保存按钮完全没有响应。我不确定我的控制器代码是否正确(只有我使用rails的第二周)所以我还没有发布它。任何帮助将不胜感激。
编辑1:
我忘记了这条至关重要的信息。表单呈现没有错误但是提交按钮没有响应(我点击它没有任何反应)。删除类别字段时,表单保存正常。
编辑2:记录信息
Started GET "/pages/8/edit" for 127.0.0.1 at 2014-06-23 03:27:33 +0800
Processing by PagesController#edit as HTML
Parameters: {"id"=>"8"}
[1m[36mPage Load (1.3ms)[0m [1mSELECT `pages`.* FROM `pages` WHERE `pages`.`id` = 8 LIMIT 1[0m
Rendered pages/_form.html.erb (29.9ms)
Rendered pages/edit.html.erb within layouts/application (31.7ms)
[1m[35mUser Load (2.7ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
Completed 200 OK in 184ms (Views: 67.8ms | ActiveRecord: 88.1ms)
Started GET "/assets/categories.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/menu.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/adminpanel.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/posts.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/pages.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/users.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/bootstrap.min.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/categories.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/posts.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/menu.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/adminpanel.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/bootstrap.min.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/pages.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2014-06-23 03:27:34 +0800
编辑3:pages_controller
class PagesController < ApplicationController
before_action :set_page, only: [:show, :edit, :update, :destroy]
# GET /pages
# GET /pages.json
def index
@pages = Page.all
end
# GET /pages/1
# GET /pages/1.json
def show
@page = Page.new(page_params)
end
# GET /pages/new
def new
@category = Category.new
@page = Page.new
end
# GET /pages/1/edit
def edit
end
# POST /pages
# POST /pages.json
def create
@page = Page.new(page_params)
@category = @page.build_category(category_params)
respond_to do |format|
if @page.save
format.html { redirect_to @page, notice: 'Page was successfully created.' }
format.json { render :show, status: :created, location: @page }
else
format.html { render :new }
format.json { render json: @page.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pages/1
# PATCH/PUT /pages/1.json
def update
@page = Page.new(page_params)
@category = @page.category
@category.assign_attributes(category_params)
respond_to do |format|
if @page.update_attributes(page_params)
format.html { redirect_to @page, notice: 'Page was successfully updated.' }
format.json { render :show, status: :ok, location: @page }
else
format.html { render :edit }
format.json { render json: @page.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pages/1
# DELETE /pages/1.json
def destroy
@page.destroy
respond_to do |format|
format.html { redirect_to pages_url, notice: 'Page was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_page
@page = Page.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def page_params
params.require(:page).permit(:title, :text, :image, :published, :seo_keywords, :seo_description)
end
def category_params
params.require(:category).permit(:name)
end
end
答案 0 :(得分:0)
form_for
标记为<form>
标记生成所有必需的html,包括操作,方法等。看看生成的源代码,了解“魔术”的作用。您会看到“没有任何反应”,因为您的提交按钮位于另一个<form>
块中(在form_for块之外),该块没有设置action
属性。并且 - 你明白了 - 没有action
导致'不采取行动'。
<强>更新强>:
为了纠正行为,请加入以下形式:
<%= form_for @page, html: { role: 'form' } do |f| %>
<div id="error_explanation">
<h2><%= pluralize(@page.errors.count, "error") %> prohibited this page from being saved </h2>
<ul>
<% @page.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, :class => 'form-control' %>
</div>
<div class="form-group">
<%= f.label :text %><br>
<%= f.text_field :text, :class => 'form-control' %>
</div>
...
<div class="action">
<%= f.submit 'Save', :class => 'btn btn-default' %>
</div>
<% end %>