Project

General

Profile

Plugin - Submitting a form returns "Couldn't find Project without an ID"

Added by Miguel Akira about 13 years ago

Hello everyone.

I'm writing a simple plugin that generates reports for a project. It's a simple form, nothing fancy.

I managed to pass the project_id to every view, but the problem is, when I try to submit a new form or submit and edited form, it always gives the "Couldn't find project without an ID" error. I don't know how to pass the project ID using the <% f.submit %> in the view.

Here is my controller code: ('relatorio' stands for 'report' in portuguese)

  def new
    @projects = Project.find(:all)
    @relatorio = Relatorio.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @relatorio }
    end
  end

  def create
    @projects = Project.find(:all)
    @relatorio = Relatorio.new(params[:relatorio])

    respond_to do |format|
      if @relatorio.save
        flash[:notice] = 'Relatorio was successfully created.'
        redirect_to :action => 'index', :project_id => Project.find(params[:project_id]) 
        #format.html { redirect_to(@relatorio) }
        #format.xml  { render :xml => @relatorio, :status => :created, :location => @relatorio }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @relatorio.errors, :status => :unprocessable_entity }
      end
    end
  end

Here's my view:

<h1>New relatorio</h1>

<% form_for (@relatorio) do |f| %>
  <%= f.error_messages %>
    <legend> Adicionar detalhes </legend>

    <label for = "title"> Titulo: </label> <br />
    <%= f.text_field :title, :size=>'35' %> <br />

    <label for = "body"> Corpo: </label> <br />
    <%= f.text_area :body, :cols=>'80', :rows=>'20' %> <br />

    <label for = "project"> Projeto: </label> <br />
    <%= f.collection_select :project_id, @projects, :id, :name %> <br />

  <p>

    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', :controller => 'relatorios', :action => 'index',:project_id => Project.find(params[:project_id]) %>

Notice that instead of the normal <%= link_to 'Back', relatorios_path %>, which would give me the "couldn't find project without an ID" as well, I changed it to the way it is above. Don't know if it's the best solution, though!

Thanks!


Replies (3)

RE: Plugin - Submitting a form returns "Couldn't find Project without an ID" - Added by Etienne Massip about 13 years ago

Just enhanced it a bit : use "@" for inline code snippet and "pre" and "code" markups for code blocks.

RE: Plugin - Submitting a form returns "Couldn't find Project without an ID" - Added by Miguel Akira about 13 years ago

Oh ok thanks a lot!

Well, I've been having this problem for a few days now, and about 1 hour after I decided to ask here in this forum, I found an answer. Don't know if it's any good - at least it's working!

The Relatorios Controller:

def create @project = Project.find(params[:project_id]) @relatorio = Relatorio.new(params[:relatorio]) @relatorio.project_id = @project.id respond_to do |format| if @relatorio.save flash[:notice] = 'Relatorio was successfully created.' format.html { redirect_to :controller => 'relatorios', :action => 'index', :project_id => Project.find(params[:project_id]) } format.xml { render :xml => @relatorio, :status => :created, :location => @relatorio } else format.html { render :action => "new" } format.xml { render :xml => @relatorio.errors, :status => :unprocessable_entity } end end end

and the view:

<h1>New relatorio</h1> <% form_for :relatorio, @relatorio, :url => { :controller => 'relatorios', :action => 'create', :project_id =>@project } do |f| %> <%= f.error_messages %> <legend> Adicionar detalhes </legend> <label for = "title"> Titulo: </label> <br /> <%= f.text_field :title, :size=>'35' %> <br /> <label for = "body"> Corpo: </label> <br /> <%= f.text_area :body, :cols=>'80', :rows=>'20' %> <br /> <p> <%= f.submit 'Create' %> </p> <% end %> <%= link_to 'Back', :controller => 'relatorios', :action => 'index',:project_id => Project.find(params[:project_id]) %>

Now it's finally working as it should be!

    (1-3/3)