Not saving, no error message

2019-08-29 13:00发布

On form submission, it's telling me that it was successfully created but it's not showing any data that was submitted. The database is empty. It's showing "null" values and the same on the actual screen where I should be able to edit the data. Here's a screenshot

enter image description here

Update: I think the problem is that it's making a GET request but I don't know how to fix it. Here's a screen shot of my server doing a get when I clicked the submit server doing a get


Here's the set up

In the index action of results_controller.rb, I have

    def index
    @results = Result.all
    @blob = Sex.new            //==@blob = Sex.new is the one I'm focussing on...
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @results }


    end
  end

In views/results/index, I have the form

<`%= form_for(@blob) do |f| %>`

<div class="field">
    <b>1. solicitor exam was fixed?:</b><br/>
    <%= f.label(:solicitorcurve, "it was cooked") %>
    <%= f.radio_button(:solicitorcurve, "t") %> </br>
  </div>  
  <div class="field">

   <%= f.label(:solicitorcurve, "no it was ok") %>
    <%= f.radio_button(:solicitorcurve, "f") %>
   </div>  

    <div class="field">
    <%= f.label(:draftingteach, "i give the teaching a grade of _ on a scale of 1 to 6") %>
    <%= f.select:draftingteach, 1..6 %> </br>
    </div>

In the create action of sexes_controller.rb i have

def create

    @sex = Sex.new(params[:blob])
    respond_to do |format|
      if @sex.save
        format.html { redirect_to(@sex, :notice => 'Sex was successfully created.') }
        format.xml  { render :xml => @sex, :status => :created, :location => @sex }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @sex.errors, :status => :unprocessable_entity }
      end
    end
  end

In models/sex.rb, there is nothing...

class Sex < ActiveRecord::Base
end

And this is the set up of the database

and this is my database structure

1条回答
神经病院院长
2楼-- · 2019-08-29 13:33

It looks like the issue is that you're retrieving params[:blob] when you should be looking at params[:sex]. form_for will create fields named after the class of the object. The instance variable name @blob you're using is arbitrary.

...
@sex = Sex.new(params[:sex])
...

This is a good argument for why you probably want to name instance variables for what they are. Less confusion.

查看更多
登录 后发表回答