jeudi 4 avril 2019

Create and Update no longer working. Any suggestions? Or reasons why?

I'm attempting to build a simple blog type app to learn ruby on rails. Create used to work, but after adding the edit/update function, both create and edit doesn't work. I'm not sure what I did wrong. Edit doesn't update with the new data and Create no longer accepts new data. Ex: creating a new post will result in a blank post no matter what is written and when adding new text to an old post will not change the post after hitting submit.

My controller:

def create
    @post = Post.create(post_params)

    if @post.save(post_params)
      redirect_to @post
    else 
      render 'new'
    end
end

def update
    @post = Post.find(post_params[:id])
    if @post.update(post_params)
      redirect_to @post
    else
      render 'edit'
    end
end

def edit
    @post = Post.find(post_params[:id])
end

Both edit and create html files render a form page:

<div class="section">
  <%= simple_form_for @post do |f| %>
    <div class="field">
      <div class="control">
        <%= f.input :title, input_html: {class: 'input'}, wrapper: false, label_html: {class: 'label'} %>
      </div>
    </div>
    <div class="field">
      <div class="detail">
        <%= f.input :detail, input_html: {class: 'textarea'}, wrapper: false, label_html: {class: 'label'} %>
      </div>
    </div>
    <%= f.button :submit %>
   <% end %>
</div>




Aucun commentaire:

Enregistrer un commentaire