我有許多用戶有一個狀態。出於某種原因,我無法使用狀態控制器中的創建操作來保存狀態。我認爲問題涉及到有一個協會,因爲我是新手,但我可能很容易犯錯。有一個關聯 - 控制器創建方法
這是現在的工作代碼。
用戶模型:
has_one :status, dependent: :destroy
狀態型號:
attr_accessible :content
belongs_to :user
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 250 }
狀態控制器:
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
用戶控制器:
def show
@user = User.find(params[:id])
@status = @user.status
@new_status = current_user.build_status
end
用戶/顯示:
<%= render 'shared/status_form' if @user == current_user %>
共享/ 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 %>
路線:
resources :users do
resource :status, only: [:create, :destroy]
end
我知道這是很多來看待,但我很欣賞的幫助,謝謝。
當我這樣做,我奇怪地得到了一個未定義的方法 「拯救」,從行,如果@ status.save因爲在這種情況下沒有什麼可以保存的? current_user.status將是空的不是嗎? – Jaqx