Jaqx
Jaqx

Reputation: 826

Has one association - controller create method

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.

Upvotes: 0

Views: 2487

Answers (3)

Jaqx
Jaqx

Reputation: 826

Missing the params to be set

@new_status = current_user.build_status(params[:status])

Upvotes: 0

HargrimmTheBleak
HargrimmTheBleak

Reputation: 2167

I can see a few problems with your code.

  1. You're interchanging @user and current_user. Is there a reason for that? You're generating a status form for a user retrieved through params[:id], but in the create method in your StatusController you're always trying to create the current user's status. These two users are not the same.
  2. In the create method you're trying to save a newly built status instance, which is empty except for the user association. This should result in a validation error against content.

Try something like this (haven't tested but should give you a head start):

routes.rb:

resources :users do
  resource :status, only: [:create, :destroy]
end

statuses/_form.html.erb:

<%= form_for [@user, @status] 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 %>

StatusesController:

def create
  @status = Status.new(params[:status])
  if @status.save
    flash[:success] = "Status updated!"
    redirect_to :back
  else
    flash[:error] = "Status didn't save"
    redirect_to :back
  end
end

Let me know if your have any problems, so we can work out a solution. One other thing is you might consider creating a status association along with a User instance (that is, if a user is always supposed to have a status). This way you can always refer to @user.status instead of building one in the controller method.

Upvotes: 1

Foo L
Foo L

Reputation: 11137

Shouldn't you be calling status instead of build_status?

@status = current_user.status

since this relationship names the field

has_one :status

Upvotes: 0

Related Questions