I have many users that have one status. For some reason I cannot get statuses to save using the create action in the status controller. I assume the problem is involving the has one association because I am new to it but I may easily be wrong.
This is now working code.
User Model:
has_one :status, dependent: :destroy
Status Model:
attr_accessible :content
belongs_to :user
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 250 }
Status Controller:
def create
@new_status = current_user.build_status(param[:status])
if @new_status.save
flash[:success] = "Status posted!"
redirect_to :back
else
flash[:error] = "Status couldn't save"
redirect_to :back
end
end
User Controller:
def show
@user = User.find(params[:id])
@status = @user.status
@new_status = current_user.build_status
end
User/Show:
<%= render 'shared/status_form' if @user == current_user %>
Shared/Status_form:
<%= form_for[@new_status], :url => user_status_path(current_user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, id:"status_box", placeholder: "Status?" %>
</div>
<%= f.submit "Update", id:"status_btn", class: "btn btn-small btn-primary" %>
<% end %>
Routes:
resources :users do
resource :status, only: [:create, :destroy]
end
I know this is a lot to look at but I really appreciate the help, thanks.