RoR - 脚手架 - 未定义的方法“to_sym” for nil:NilClass 仅在编辑方法中出现错误

发布于 2024-08-21 09:48:25 字数 2798 浏览 1 评论 0原文

undefined method `to_sym' for nil:NilClass

我仅在 nifty_scaffold 的编辑页面中出现此错误。

这是 _form.html.erb

<% form_for @progress do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :date %><br />
    <%= f.date_select :date %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :weight %><br />
    <%= f.text_field :weight %>
  </p>
  <p>
    <%= f.label :fatpercentage %><br />
    <%= f.text_field :fatpercentage %>
  </p>
  <p>
    <%= f.label :height %><br />
    <%= f.text_field :height %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

这是 edit.html.erb

 <% title "Edit Progress" %>
 <%= render :partial => 'form' %>

这是我的控制器:

class ProgressesController < ApplicationController
  def new
    @progress = Progress.new
  end

  def create
    @progress = Progress.new(params[:progress])
    if @progress.save
      flash[:notice] = "Successfully created progress."
      redirect_to progresses_url
    else
      render :action => 'new'
    end
  end

  def edit
    @progress = Progress.find(params[:id])
  end

  def update
    @progress = Progress.find(params[:id])
    if @progress.update_attributes(params[:progress])
      flash[:notice] = "Successfully updated progress."
      redirect_to progresses_url
    else
      render :action => 'edit'
    end
  end

  def index
    @progresses = Progress.all
  end
end

可能出了什么问题?我似乎找不到我的错误:-S。 看来它: - 正确获取数据 - 无法将数据库值插入“编辑视图”字段。

我使用 :float、:string 和 :date 作为脚手架中的数据类型。

只是为了完成帖子,这是我的错误: NoMethodError in Progresses#edit

Showing app/views/progresses/edit.html.erb where line #3 raised:

undefined method `to_sym' for nil:NilClass
Extracted source (around line #3):

1: <% title "Edit Progress" %>
2: 
3: <% form_for @progress do |f| %>
4:   <%= f.error_messages %>
5:   <p>
6:     <%= f.label :date %><br />

在第 6 行,代码日志结束...

编辑: 我的routes.rb 似乎有一个错误。 目前已对此进行评论:

 map.edit_progress "edit_progress", :controller => "progresses", :action => "edit"

当我取消注释时,它也会在我的索引视图中给出错误。

由于某种原因,这被称为: 'http://127.0.0.1:3001/progresses/1/edit',不应该是: 'http://127.0.0.1:3001/progresses/edit/1 '? 尽管似乎调用了“编辑视图”...也许这就是未填写值的原因,但在我看来...

我的解决方案是什么?

undefined method `to_sym' for nil:NilClass

I have this error only in my edit page of my nifty_scaffold.

This is _form.html.erb

<% form_for @progress do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :date %><br />
    <%= f.date_select :date %>
  </p>
  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>
  <p>
    <%= f.label :weight %><br />
    <%= f.text_field :weight %>
  </p>
  <p>
    <%= f.label :fatpercentage %><br />
    <%= f.text_field :fatpercentage %>
  </p>
  <p>
    <%= f.label :height %><br />
    <%= f.text_field :height %>
  </p>
  <p><%= f.submit "Submit" %></p>
<% end %>

This is edit.html.erb

 <% title "Edit Progress" %>
 <%= render :partial => 'form' %>

And this is my controller:

class ProgressesController < ApplicationController
  def new
    @progress = Progress.new
  end

  def create
    @progress = Progress.new(params[:progress])
    if @progress.save
      flash[:notice] = "Successfully created progress."
      redirect_to progresses_url
    else
      render :action => 'new'
    end
  end

  def edit
    @progress = Progress.find(params[:id])
  end

  def update
    @progress = Progress.find(params[:id])
    if @progress.update_attributes(params[:progress])
      flash[:notice] = "Successfully updated progress."
      redirect_to progresses_url
    else
      render :action => 'edit'
    end
  end

  def index
    @progresses = Progress.all
  end
end

What could be wrong? I can't seem to find my error :-S.
It seems that it:
- fetches the data correctly
- can't insert the db-values into the "edit view" fields.

I'm using :float, :string and :date as data types in the scaffold.

Just for the completed post, this is my error:
NoMethodError in Progresses#edit

Showing app/views/progresses/edit.html.erb where line #3 raised:

undefined method `to_sym' for nil:NilClass
Extracted source (around line #3):

1: <% title "Edit Progress" %>
2: 
3: <% form_for @progress do |f| %>
4:   <%= f.error_messages %>
5:   <p>
6:     <%= f.label :date %><br />

At line 6 the log of the code ends...

Edit:
It seems to be an error in my routes.rb.
This is currently commented:

 map.edit_progress "edit_progress", :controller => "progresses", :action => "edit"

when i uncomment it, it gives an error also in my index view.

For some reason, this is called:
'http://127.0.0.1:3001/progresses/1/edit', shouldn't it be: 'http://127.0.0.1:3001/progresses/edit/1' ?
Even though it seem's that the "edit view" is called... Perhaps this is the reason why the values aren't filled in, in my view...

What could be my solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-08-28 09:48:25

我建议在这里进行两步调试:

  1. 从编辑视图中删除所有代码并在其中添加一些纯文本,然后在浏览器中访问您的页面,看看是否出现任何错误或新错误或没有错误

  2. 如果收到任何新错误,则它可能会帮助您解决问题或在控制器编辑操作中引发@progress以查看是否已设置

    定义编辑
      @progress = Progress.find(params[:id])
      提出@progress.inspect
    结尾
    

这两个步骤可能会帮助您解决问题。

I will suggest two step debugging here:

  1. Remove all your code from the edit view and a add some plain text in it, then access your page in the browser and see if you get any error or new error or no error

  2. If you get any new error then it might help you in solving the issue or in your controller edit action raise the @progress to see whether it is being set

    def edit
      @progress = Progress.find(params[:id])
      raise @progress.inspect
    end
    

These two steps might help you in resolving the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文