0
votes

I am trying to create lists and each has 2 attributes name and description.Database does create it and save it when using console but not using the website form.When checking the log file I found that website form does not post instead uses gets and is redirected to itself, How do I make the website form POST instead of GET so it gets stored in database.

Log file:

Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-09-18 12:35:14 -0400 Served asset /jquery_ujs.js - 304 Not Modified (0ms)

Here is list controller:

 def create
    @list = Lists.new(params[:lists])
    if @list.save
      redirect_to @list
    else
      render 'new'
    end
  end

   def update
    if @list.update_attributes(params[:lists])

      flash[:success] = "lists updated"
      redirect_to @list
    else
      render 'edit'
    end
  end

def new
    @list = Lists.new
  end

This is the form for users to create list

<%= form_for @list, url: newlist_path(@list), html: { method: :put } do |f| %>

      <%= f.label :name %>
      <%= f.text_field :name %>
  </br>
      <%= f.label :description %>
      <%= f.text_field :description %>


      <%= f.submit "Create the List" %>
    <% end %>
2
are you sure your model name is Lists not ListSabyasachi Ghosh
by default the create is a POST not put. please check and confirm your create action is executing after submitting the formSabyasachi Ghosh
it creates and saves using console, it doesn't create nor saves when using website to enter data so form on website isn't saving, so isnt there something wrong with form because create works when using consoleuser2452062
Agree, I'd put a raise params.to_yaml in the first line of the update method in the controller to see if it is really hitting the update method.Miguelgraz
i am talking about the create action not create method of active record. check after submitting the form which action is being called.Sabyasachi Ghosh

2 Answers

1
votes

Your form_for helper is routing to the incorrect action. Try routing to the create action instead:

<%= form_for @list, url: {action: "create"} do |f| %>
0
votes

I don't know if your controller's code excerpt you've pasted is complete, but you might missed to initialize list object for update. In your update action you have only

if @list.update_attributes(params[:lists])

but you are not initializing @list variable before. So you probably need something like

@list = Lists.find(params[:id])

You can also inspect your log file and verify what parameters are sent to controller.