Rails 4控制器由脚手架生成的新闻无法正常工作

时间:2015-04-21 15:55:56

标签: ruby-on-rails ruby-on-rails-4 devise simple-form-for

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

我试图在rails上学习ruby,来自php。我使用生成工具生成了一个webapp,第二个我使用scaffold生成了一个News控制器。设计和专家也安装了宝石。

程序运行完美,问题与新闻模块有关,我用脚手架生成了它。

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

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

我正在使用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]

抱歉我的英语不好,我在这里没有指示,有什么办法可以解决吗?

感谢。

======更新=======

的routes.rb

Rails.application.routes.draw do

  root to: 'visitors#index'
  devise_for :users

  resources :users
  resources :news

end

New.rb(Model)

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

命令佣金路线

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

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

1 个答案:

答案 0 :(得分:0)

使用news_index_path获取GET / news和POST / news。 Rails没有正确地找出&#34; news&#34;术语。 检查rake routes的输出,这很明显。