link_to期待keyword_end rails

时间:2016-10-27 19:57:57

标签: ruby ruby-on-rails-4 ruby-on-rails-3.2

我不明白为什么但是在关注ruby on rails教程时我仍然遇到问题。当您打开链接以在不同页面之间导航时...

这是我的观点:

class ArticlesController < ApplicationController
  def new
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to @article
    else
      render 'new' #on utilise render car cela permet de recharger la requête d'article
                  #qui vient d'échouer contrairement à redirect_to qui lancerais une nouvelle requete
    end
  end

  def index
    @articles = Article.all
  end

  def edit
  end

  def show
    @article = Article.find(params[:id])
  end

  def update
  end

  def destroy
  end
end

private
  def article_params
    params.require(:article).permit(:title, :text)
  end

这是我的控制器

{{1}}

如果你需要一些人告诉我。

所以如果你有任何提示,非常感谢

3 个答案:

答案 0 :(得分:1)

不是期望link关键字的end。实际错误是由于表单没有end标记。 请按以下更新您的

<h1>New Article</h1>
<%= form_for @article, url: articles_path do |f| %> <!<%= form_for :article, url: articles_path do |f| %> >

  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@article.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
        <% @article.errors.full_messages.each do |msg| %>
          <ul>
              <li><%= msg %></li>
          </ul>
        <% end %>
    </div>
  <% end %>

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

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

  <p>
    <%= f.submit %>
  </p>
<%end%>
<%= link_to 'Back', articles_path %>

答案 1 :(得分:0)

感谢您的快速回答,我做了您告诉我要做的事情。事实上,我错过了表格中的结尾。但是现在出现了一个新的错误,指向&#34;标题&#34;第2行:

错误讯息: &#34;表格中的第一个参数不能包含nil或为空&#34;

新代码

 <h1>New Article</h1>
<%= form_for @article url: articles_path  do |f| %>

  <% if @article.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@article.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
      <ul>
        <% @article.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

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

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

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

<%end%>

<%= link_to 'Back', articles_path %>

你能找到这个bug吗?我想在Ruby和Ror中提高自己。

非常感谢

答案 2 :(得分:0)

您似乎还没有在新操作中初始化您的@article变量。 在您的新操作中将其写为@article = Article.new,它应该可以正常工作。 由于你没有初始化@article,它会在表单中给你一个零值错误。