soldiershin
soldiershin

Reputation: 1620

Update Attributes in rails using 'ancestry' gem

I am using ancestry gem in rails.When it is set to a model it gets attributes like parent,parent_id,children etc.Now i have a model message which contains two attributes content and content_title and we can set the parent_id but the problem is when i try to set both of them only one gets updated.Here's my code.

The "parent_id" is being set in the "create action" and the other two attributes as well.Now when i update it like this only "parent_id " gets updated but when i use

 #@page = Message.new(message_params)

The other two attributes content and content_title gets updated and the parent_id doesn't get updated.

class MessagesController < ApplicationController
    layout false
  def index
     @messages = Message.all
  end

  def show
    @message_id = Message.find(params[:id])
  end

  def new
    @message = Message.new

  end

 def create
    @page = Message.new(:parent_id => params[:parent_id],:content_title => params[:content_title],:content => params[:content])
   #@page = Message.new(message_params)

     if @page.save
        flash[:notice] = "The Mesage was created succeffully"
       redirect_to(:action => 'index')
    else
      render('new')
  end
end


private
    def message_params
       params.require(:message).permit(:parent_id,:content_title,:content)
    end

end

This is the view file which is sending the data

<div class="Pages new">
   <h2> Create Pages</h2>
     <%= form_for(:message, :url => {:action => 'create',:parent_id => @message_id}) do |f| %>
     <table summary="Page form fields">
     <tr>
          <th>ParentID</th>
          <td><%= f.text_field(:id) %></th>
        </tr>
        <tr>
        <tr>
          <th>Content Title</th>
          <td><%= f.text_field(:content_title) %></th>
        </tr>
        <tr>
          <th>Content</th>
          <td><%= f.text_field(:content) %></th>
        </tr>
        </table>

        <div class="form-buttons">
          <%= submit_tag("Submit Form") %>
          </div>

          <% end %>
        </div> 

Is there a way to set them both simultaneously.Am i doing something wrong?

Upvotes: 0

Views: 241

Answers (1)

soldiershin
soldiershin

Reputation: 1620

For anybody wondering what the answer was .

def create
    @page = Message.new(message_params)
    @page.parent_id = params[:parent_id]
   #@page = Message.new(message_params)

     if @page.save
        flash[:notice] = "The Mesage was created succeffully"
       redirect_to(:action => 'index')
    else
      render('new')
  end
end

Upvotes: 1

Related Questions