Joy_TK
Joy_TK

Reputation: 11

Ruby/Rails app not saving, also not erroring out

A bit of a Ruby returner trying to make an app after not having touched it for a while. So I think I remember the error driven development process pretty well. I have my app working for the most part, using a form partial to house my edit and new logic.

I have gone back and forth of using the conventional resources :facts in my controller, as well as spelling out all the routes verbatim. The behavior persists, so I am thinking it isn't that.

I've trawled Stack overflow, and also returned to the syntax from my two other WORKING ruby apps hosted on Heroku, and I'm not seeing the syntax disparity. That has gotta be it though, I'm guessing, since the routes were the issue.

Sorry if I am doing something so obvious, but its been a while. I've been working on this thing every weekend for the past few weekends and I'm getting to a point of frustration wanting to have this bug done and taken care of.

jtkaufman737/raleigh_newcomer

<DOCTYPE html>
  <head>
    <link rel='stylesheet' type="text/css" href='/application.scss'/>
    <link href="https://fonts.googleapis.com/css?family=Space+Mono" rel="stylesheet">
  </head>
<body id="formMain" style="width:50%; margin-top:5%; margin-bottom:5%;">
<form class="defaultForm">
<%= form_for @fact do |f|%>
<% if @fact.errors.any? %>
   <div id="error_explanation">
     <h2>
       <%= pluralize(@fact.errors.count, "error") %> prohibited
       this fact from being saved:
     </h2>
     <ul>
       <% @fact.errors.full_messages.each do |msg| %>
         <li><%= msg %></li>
       <% end %>
     </ul>
   </div>
 <% end %>
 <div class="container"><br/>
      <div class="column-right">
        <%= f.label :category %>:<br/><br/>
        <%= f.label :text %>:<br/><br/>
        <%= f.label :source %>:<br/><br/>
      </div>
      <div class="column-left">
        <%= f.text_field :category %><br/><br/>
        <%= f.text_field :body%><br/><br/>
        <%= f.text_field :source %><br/><br/>
      </div>
    </div><br/>
</form>
<button class="buttonStyle"><center>
  <%= f.submit %><% end %>
<%= link_to 'Back', facts_path, :class => 'buttonStyle'%></button></center>
</body>
</html>

enter image description here

Upvotes: 1

Views: 43

Answers (2)

widjajayd
widjajayd

Reputation: 6253

As additional info from @Sebastián, your form structure has also a problem (some unmatched div, button and center tags).

Here is some correction from your form:

<%= form_for @fact do |f|%>
  <% if @fact.errors.any? %>
     <div id="error_explanation">
       <h2>
         <%= pluralize(@fact.errors.count, "error") %> prohibited
         this fact from being saved:
       </h2>
       <ul>
         <% @fact.errors.full_messages.each do |msg| %>
           <li><%= msg %></li>
         <% end %>
       </ul>
     </div>
  <% end %>
  <div class="container">
    <div class="column-right">
      <%= f.label :category %>:<br/><br/>
      <%= f.label :text %>:<br/><br/>
      <%= f.label :source %>:<br/><br/>
    </div>
    <div class="column-left">
      <%= f.text_field :category %><br/><br/>
      <%= f.text_field :body%><br/><br/>
      <%= f.text_field :source %><br/><br/>
    </div>
  </div>
  <button class="buttonStyle">
    <center>
    <%= f.submit "Save", :class => 'btn btn-lg btn-primary', data: { disable_with: "Please wait..." } %>
    <%= link_to 'Back', facts_path, :class => 'buttonStyle'%>
    </center>
  </button>
<% end %>

Upvotes: 1

Sebasti&#225;n Palma
Sebasti&#225;n Palma

Reputation: 33470

You don't need to wrap all of your views between html and body tags, that's what application.html.erb is used for, you can define there the js and or css files you need to use.

In order to make your form_for work when the f.submit is pressed, then remove the "plain" html form you're using to wrap it.

You haven't added the category to your strong params in the facts_controller, and as you've added the presence: true validation on the model, then it'll always throw an error asking for this attribute.

In the create method after the @fact is saved you redirect to @Fact, but that variable isn't defined, @fact is different to @Fact, so it'll throw an error saying such variable doesn't exist.

In your strong_params you've defined title and text, but the attributes for fact are body, category and source, that's why it doesn't matter if you fill the inputs, you won't be able to save that data entered, you'll see there's a:

Unpermitted parameters: body, source

message in the Rails server, that method might look like:

def fact_params
  params.require(:fact).permit(:category, :body, :source)
end

Upvotes: 1

Related Questions