0

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.

Jaqx
  • 826
  • 3
  • 14
  • 39

3 Answers3

1

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.

HargrimmTheBleak
  • 2,147
  • 1
  • 19
  • 19
  • I'm looking into this but haven't gotten anywhere yet. I think I need the :url part in the form_for otherwise it tried to post to Statuses instead of status. nvm you hadn't seen the updated code yet – Jaqx Jan 25 '13 at 11:17
  • Hm, I'd think they should've fixed this. Here's a similar thread http://stackoverflow.com/questions/2261827/nested-form-for-singular-resource going 2+ years back... If not, then yes, try to explicitly give it an `user_status_path(@user, @status)` as a URL. – HargrimmTheBleak Jan 25 '13 at 11:21
  • The reason I am interchanging them is because when you are on a users profile I need to show that user using @user. However when I want to create a status only the current logged in user can create a status for themselves – Jaqx Jan 25 '13 at 11:23
  • So does that mean a user should see the form only when they're viewing their own profile? – HargrimmTheBleak Jan 25 '13 at 11:30
  • That is correct. Users can only see the form to make the status when they are looking at their own profiles. Otherwise on other users pages they will see either the status the user posted or nothing if they didn't post anything. I updated some code but still no progress, thanks for helping – Jaqx Jan 25 '13 at 11:36
  • I'm going to delete this question but the solution in case you're wondering was to add this to my Status controller: @new_status = current_user.build_status(param[:status]). I was missing param:status – Jaqx Jan 25 '13 at 11:46
  • 1
    Well that was pretty much what I've posted in the answer ;) BTW not sure if deleting is a good idea - other users might run into the same problem – HargrimmTheBleak Jan 25 '13 at 11:47
  • Wow i totally missed that. I mean I tried the code and than hit ctrl Z and didn't even realize that I was missing that piece haha. I feel like this is a real specific error resulting from my exhaustion, but I'll leave it up as you suggest – Jaqx Jan 25 '13 at 11:54
0

Shouldn't you be calling status instead of build_status?

@status = current_user.status

since this relationship names the field

has_one :status

Foo L
  • 10,977
  • 8
  • 40
  • 52
  • when I do that I strangely get undefined method "save" from the line if @status.save because there is nothing to save in that case? current_user.status would be empty wouldn't it? – Jaqx Jan 25 '13 at 11:06
0

Missing the params to be set

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

Jaqx
  • 826
  • 3
  • 14
  • 39