0

I am learning to use CRUD, and setup a page to add a record, however it only generated blank record? Can you take a look my code?

thanks!

here is the Page controller:

class PagesController < ApplicationController
   def index
    @test = Page.all
  end

  def new
    @test = Page.new
  end

  def create
    @test = Page.new(params.permit(:subject_id,:name,:position,:visible,:permalink))
    if @test.save
      flash[:notice] = "Page created successfully!"
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

end

here is the new.html.erb

<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>

<div class="pages new">
  <h2>Create Subject</h2>

  <%= form_for(:Page, :url => {:action => 'create'}) do |f| %>

    <table summary="Page form fields">
     <tr>
        <th>Subject_ID</th>
        <td><%= f.text_field(:subject_id) %></td>
      </tr>
      <tr>
        <th>Name</th>
        <td><%= f.text_field(:name) %></td>
      </tr>
      <tr>
        <th>Position</th>
        <td><%= f.text_field(:position) %></td>
      </tr>
      <tr>
        <th>Visible</th>
        <td><%= f.text_field(:visible) %></td>
      </tr>
       <tr>
        <th>Permalink</th>
        <td><%= f.text_field(:permalink) %></td>
      </tr>
    </table>

    <div class="form-buttons">
      <%= submit_tag("Create Subject") %>
    </div>

  <% end %>
</div>

Here is index.html.erb

<div class="pages index">
  <h2>Pages</h2>

 # <%= link_to("Add New Page", {:action => 'new'}, :class => 'action new') %>

  <table class="listing" summary="Page list">
    <tr class="header">
      <th>Position</th>
      <th>Page Name</th>
      <th>Visible</th>
      <th>Pages</th>
      <th>Actions</th>
    </tr>
    <% @test.each do |f| %>
    <tr>
      <td><%= f.position %></td>
      <td><%= f.name %></td>
      <td class="center"><%= f.visible %></td>
      <td class="center"><%= f.permalink %></td>
      <td class="actions">
        <%= link_to("Show", {:action => 'show', :id => f.id}, :class => 'action show') %>
        <%= link_to("Edit", {:action => 'edit', :id => f.id}, :class => 'action edit') %>
        <%= link_to("Delete", {:action => 'delete', :id => f.id}, :class => 'action delete') %>
      </td>
    </tr>
    <% end %>
  </table>
</div>

Thanks so much!

1
  • 1
    params.require(:page).permit(:subject_id,:name,:position,:visible,:permalink) Commented Jun 22, 2015 at 23:06

1 Answer 1

1

Strong parameters

Rails sends form parameters in nested hashes.

{ page: { subject_id: 1, name: 'Hello World.' } }

So to whitelist the parameters you would do.

class PagesController < ApplicationController
   def index
    @test = Page.all
  end

  def new
    @test = Page.new
  end

  def create
    @test = Page.new(page_params)
    if @test.save
      flash[:notice] = "Page created successfully!"
      redirect_to(:action => 'index')
    else
      render('new')
    end
  end

  private 

  def page_parameters
    params.require(:page)
          .permit(:subject_id,:name,:position,:visible,:permalink)
  end
end

which is like doing:

params[:page].slice(:subject_id,:name,:position,:visible,:permalink)

form_for and models

Also your form should read:

 <%= form_for(:page, :url => {:action => 'create'}) do |f| %>

Or:

 <%= form_for(@page, :url => {:action => 'create'}) do |f| %>

:Page will give you the wrong key in your parameters and prevent rails from binding the form to your @page object. (The values posted will disappear from the form when it's invalid).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks so much, maxcal! why I should use "page" instead of "Page"?
Because form_for(:page) looks for the instance variable @page, form_for(:Page) would look for @Page which is not defined. Ruby like most sensible programming languages is case sensitive.
thanks agin, maxcal! now I understood the difference :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.