ruby-on-rails - 将表单从一个 Controller 的 View 传递到完全不同的 Controller

标签 ruby-on-rails ruby-on-rails-4 controller form-for

好吧,我已经搜索了其他可用的 Stackoverflow 问题,并尽我所能来解释这个问题,但我陷入了僵局。我是一名 PHP 开发人员,但我刚刚学习 Ruby on Rails (Rails 4)。

我想尝试实现的目标: - POST 表单(使用 form_for),该表单位于“博客”系列下的 View index.html.erb 文件中(因此 Controller 也称为博客) - 但我希望该表单 POST 到一个完全不同的 Controller ,不是博客 Controller ,而是一个名为“Feature”的 Controller (具体来说,到名为“Feature”的 Controller 中的创建操作)

我知道我应该在 form_for 中使用 :url 组件,但我确信我输入的语法是错误的,或者其他地方导致了错误。

请原谅我的新手无知,我刚刚开始接触 Rails

我当前收到的错误消息:

博客中的参数错误#index 参数数量错误(1..2 为 3) 提取的源代码(第 3 行附近):

<h1>Blog main page, welcome</h1>

<% form_for :feature, @feature, :url => { :action => "create" } do |f| %>
<p>
<%= f.label :subject %><br>
<%= f.text_field :string %>

我的代码:

Index.html.erb(位于博客系列下)

<h1>Blog main page, welcome</h1>

<% form_for :feature, @feature, :url => { :action => "create" } do |f| %>
  <p>
    <%= f.label :subject %><br>
    <%= f.text_field :string %>
  </p>

  <p>
    <%= f.label :author %><br>
    <%= f.text_area :string %>
  </p>

  <p>
    <%= f.label :title %><br>
    <%= f.text_area :string %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

<br>

<%= link_to "My Blog", new_blog_path %>

我的功能 Controller 代码

class FeatureController < ApplicationController

    def index
    end

    def new

    end

    def show

    end

    def create

        @feature = Blog.new (feature_params)
        render text: params[:feature].inspect

    end

private

    def feature_params
        params.require(:feature).permit(:title, :text, :text2, :text3)
    end

end

end

我的路由配置中的代码

MyApp::Application.routes.draw do

  root 'blog#index' 

  resources :blog
  resources :admin
  resources :feature


end

我的数据库架构如下所示:

  create_table "blogs", force: true do |t|
    t.string   "subject"
    t.string   "title"
    t.string   "author"
    t.text     "text"
    t.datetime "created_at"
    t.datetime "updated_at"

我不知道我做错了什么:(如果有人能够帮助我,我将非常感激

最佳答案

修改路由使用复数格式后,解决方案是:

<%= form_for :features, :url => features_path do |f| %>

features_path'/features'

的路径辅助方法

关于ruby-on-rails - 将表单从一个 Controller 的 View 传递到完全不同的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575929/

相关文章:

ruby-on-rails - Rails 返回用户数

mysql - as_json 忽略 .where() 子句的第二部分

ruby-on-rails - Rails 找不到包含左方括号的文件

ruby-on-rails - Rails 4-belong_to 嵌套对象字段未保存

ruby-on-rails - Facebook 评论 w/Rails 4 和 Turbolinks

JAVAFX:阻止用户打开新窗口,直到他关闭第一个窗口

c# - Angular 6 HttpClient Post 未命中 DotNet Core 2.2 API Controller 中的 API Post 方法

ruby-on-rails - Rails 每天向用户发送邮件

ruby-on-rails - 使用 RSpec 测试 Redis 事务

ruby-on-rails - Rails update_attribute 问题