0

我搜索了这个问题,但没有成功。

我正在尝试学习 ruby​​ on rails,来自 php。我用生成工具生成了一个 webapp,其次我用脚手架生成了一个新闻控制器。设计和专家也安装了 gems。

该程序运行良好,问题与新闻模块有关,我使用脚手架生成它。

使用以下命令创建的路由:resources :news

我的想法是创建一个 _form.html.erb 并调用它来创建新记录或更新现有记录。一些教程教创建一个 new.html.erb 和一个 update.html.erb 文件并复制代码,但我知道可以将部分作为主要表单部分。

我正在使用 simple_form_for 并且执行新的代码是:

# GET /news/new
  def new
    @news = New.new
    authorize New
  end

_form.html.erb

<%= simple_form_for(@news) do |f| %>
    <%= f.input :titulo %>
    <%= f.input :resumo %>
    <%= f.button :submit %>
<% end %>

当我进入编辑时,它可以工作,但是添加一个新的它会抛出。

ActionController::UrlGenerationError at /news/new

No route matches {:action=>"show", :controller=>"news", :locale=>:en} missing required keys: [:id]

对不起我的英语不好,我在这里没有方向,有什么办法可以解决吗?

谢谢。

====== 已更新 =======

路线.rb

Rails.application.routes.draw do

  root to: 'visitors#index'
  devise_for :users

  resources :users
  resources :news

end

新.rb(模型)

class New < ActiveRecord::Base

  belongs_to :user
end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :set_locale

  def default_url_options(options={})
    { locale: I18n.locale }
  end

  private
  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

end

news_controller.rb (完成)

class NewsController < ApplicationController

  before_filter :authenticate_user!
  after_action :verify_authorized

  #before_action :set_news, only: [:show, :edit, :update, :destroy]

  # GET /news
  # GET /news.json
  def index
    @news = New.all
    authorize New
  end

  # GET /news/1
  # GET /news/1.json
  def show
    @news = New.find(params[:id])
    authorize New
  end

  # GET /news/new
  def new
    @news = New.new
    authorize New
  end

  # GET /news/1/edit
  def edit

    @news = New.find(params[:id])
    authorize New

  end

  # POST /news
  # POST /news.json
  def create
    @news = New.new(news_params)

    respond_to do |format|
      if @news.save
        format.html { redirect_to @news, notice: 'New was successfully created.' }
        format.json { render :show, status: :created, location: @news }
      else
        format.html { render :new }
        format.json { render json: @news.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /news/1
  # PATCH/PUT /news/1.json
  def update
    respond_to do |format|
      if @news.update(news_params)
        format.html { redirect_to @news, notice: 'New was successfully updated.' }
        format.json { render :show, status: :ok, location: @news }
      else
        format.html { render :edit }
        format.json { render json: @news.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /news/1
  # DELETE /news/1.json
  def destroy
    @news.destroy
    respond_to do |format|
      format.html { redirect_to news_url, notice: 'New was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_news
    @news = New.find(params[:id])
  end

  private
  def news_params
    params.require(:news).permit(:titulo, :resumo, :texto, :published_at, :user_id)
  end
end

命令 rake 路由

news_index GET    /news(.:format)                   news#index
                     POST   /news(.:format)                   news#create
            new_news GET    /news/new(.:format)               news#new
           edit_news GET    /news/:id/edit(.:format)          news#edit
                news GET    /news/:id(.:format)               news#show
                     PATCH  /news/:id(.:format)               news#update
                     PUT    /news/:id(.:format)               news#update
                     DELETE /news/:id(.:format)               news#destroy

提前致谢。

======= 更新2 ===========

将我的新操作更改为:

def new
   @news = New.create(params[:id])
end

它解决了,但每次我输入时,它都会创建一个空记录......

4

1 回答 1

0

将 news_index_path 用于 GET /news 和 POST /news。Rails 没有正确理解“新闻”一词的复数形式。检查 的输出rake routes,这将是显而易见的。

于 2018-01-03T05:37:35.390 回答