Rails 4控制器由脚手架生成的新闻无法正常工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails 4控制器由脚手架生成的新闻无法正常工作相关的知识,希望对你有一定的参考价值。
我搜索了这个问题,但没有成功。
我试图学习ruby on rails,来自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(型号)
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
它解决了,但每次我进入,都会创建一个空记录......
使用news_index_path获取GET / news和POST / news。 Rails没有正确地弄清楚“新闻”一词的复数。检查rake routes
的输出,这将是显而易见的。
以上是关于Rails 4控制器由脚手架生成的新闻无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章