duhovicm
duhovicm

Reputation: 57

Rails submit button gives no response

I searched for the answer, but I couldn't find the same problem. Basically - I'm building a simple CMS, and I'm building a controller with a view, but it isn't working. I can press the submit button, but the form gives no response, it doesn't even output any errors.

I also noticed it changes the url from /pages/new to /pages, but it stays on the form.

Page Controller:

def index
 @pages = Page.sorted
end

def show 
  @page = Page.find(params[:id])
end

def new
  @page = Page.new
end

def create
  @page = Page.new(page_params)
  if @page.save
    flash[:notice] = "Page created succesfully."
    redirect_to pages_path 
  else
    render('new')
  end
end

def edit
 @page = Page.find(params[:id])
end

def update
  @page = Page.find(params[:id])
  if @page.update_attributes(page_params)
  flash[:notice] = "Subject updated succesfully."
  redirect_to page_path(@page) 
  render('edit')
 end
end

def delete
  @page = Page.find(params[:id])
end

def destroy
    @page = Page.find(params[:id])
    @page.destroy
    flash[:notice] = "Page '#{@page.name}' destoyed succesfully."
    redirect_to(pages_path)
end

private

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

new.html.erb:

<%= link_to("<< Back To List", pages_path, :class => 'back-link') %>

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

<%= form_for(@page, :url => pages_path, :method => 'post') do |f| %>

    <table summary = "Page form fields">
        <tr>
            <th> Name: </th>
            <td><%= f.text_field(:name) %></td> 
        </tr>

        <tr>
            <th> Subject ID: </th>
            <td><%= f.text_field(:subject_id) %></td> 
        </tr>

        <tr>
            <th> Permalink: </th>
            <td><%= f.text_field(:permalink) %></td> 
        </tr>

        <tr>
            <th>Position: </th>
            <td><%= f.text_field(:position) %></td>
        </tr>

        <tr>
            <th>Visible: </th>
            <td><%= f.text_field(:visible) %></td>
        </tr>
    </table>

    <div class="form-buttons">
        <%= f.submit("Create Page") %>
    </div>
    <% end %>
</div>

EDIT:

tried changing the form code to <%= form_for(@page) do |f| %>, but I still have the same problem

I added the @page.save! part of the code and it gave me a error which showed me errors which are deeper down in my code! At least I fixed this problem. Thank you very much.

Upvotes: 0

Views: 218

Answers (1)

Gerry
Gerry

Reputation: 10497

It seems that the information from your form is not valid, try using save! instead of save in your create action:

def create
  @page = Page.new(page_params)
  if @page.save!
    flash[:notice] = "Page created succesfully."
    redirect_to pages_path 
  else
    render('new')
  end
end

That will show the error on your logs.

Upvotes: 1

Related Questions