Ruby on rails Rails创建方法来发布对象不工作
我不熟悉rails。我创建了一个文章发布表单,当我单击“发布”按钮时,它会将我带到文章页面,但它不会发布我键入的内容Ruby on rails Rails创建方法来发布对象不工作,ruby-on-rails,ruby,ruby-on-rails-3,rubymine,Ruby On Rails,Ruby,Ruby On Rails 3,Rubymine,我不熟悉rails。我创建了一个文章发布表单,当我单击“发布”按钮时,它会将我带到文章页面,但它不会发布我键入的内容 routes.rb: get 'articles/new' => 'articles#new' post 'articles' => 'articles#create' class ArticlesController < ApplicationController def index @articles = Article.al
routes.rb
:
get 'articles/new' => 'articles#new'
post 'articles' => 'articles#create'
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to '/articles'
else
render 'new'
end
end
private
def article_params
params.required(:article).permit(:content)
end
end
`articles/new.html.erb`:
<div class="create">
<div class="container-fluid">
<%= form_for @article do |t| %>
<%= t.text_field :title, :placeholder => 'Title', :class => 'article_title' %>
<%= t.text_area :body, :placeholder => 'Article', :class => 'article_body' %>
<%= t.submit 'Post', :class => 'btn-btn-article' %>
<% end %>
</div>
</div>
articles\u controller.rb
:
get 'articles/new' => 'articles#new'
post 'articles' => 'articles#create'
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to '/articles'
else
render 'new'
end
end
private
def article_params
params.required(:article).permit(:content)
end
end
`articles/new.html.erb`:
<div class="create">
<div class="container-fluid">
<%= form_for @article do |t| %>
<%= t.text_field :title, :placeholder => 'Title', :class => 'article_title' %>
<%= t.text_area :body, :placeholder => 'Article', :class => 'article_body' %>
<%= t.submit 'Post', :class => 'btn-btn-article' %>
<% end %>
</div>
</div>
不允许的参数:标题、正文
您应该将您的文章参数
更改为以下内容
def article_params
params.require(:article).permit(:title, :body)
end
因此,您的服务器日志会告诉您问题:
Unpermitted parameters: title, body
您必须将标题
和主体
属性列为白名单,因为在活动模型体量指定中禁止使用动作控制器参数,直到它们被列为白名单
更改:
def article_params
params.required(:article).permit(:content)
end
致:
另外,请看一下的官方Rails文档
更新
我建议您在路由
文件中使用:
resources :articles
然后,您将有以下路线:
articles GET /articles(.:format) articles#index
然后,在控制器的创建操作中,可以使用articles\u path
:
def create
@article = Article.new(article_params)
if @article.save
redirect_to articles_path
else
render 'new'
end
end
你的日志上写着-
Unpermitted parameters: title, body
将您的文章参数
方法更改为
def article_params
params.require(:article).permit(:title, :body, :content)
end
很抱歉,文章控制器代码的格式不正确。请将您的表单显示在“发布”按钮的位置,好吗?能否使用表单代码和服务器中生成的日志更新问题?您确实需要开始发布更多可接受的问题,这是连续两个问题。在这种情况下,检查日志是您的最佳选择。在您的服务器日志中,您应该能够看到您的请求发生了什么。删除
必需的:)中的d。我更改了它,但它仍然没有发布到索引页?它不应该发布到索引页,而是应该发布到创建操作。您的模型也有内容列吗?那也允许吧。另外,将required
更改为require
params.require(:article).permit(:title,:body,:content)
看看这是否解决了问题。否则,请在问题部分再次发布服务器日志作为更新。如果您不想使用RESTful资源,它应该是:get'articles'=>'article#index'
。我更改了它,但它仍然没有发布到索引页?