0

我有一个clan.rbclan_options.rb

部落.rb

class Clan < ActiveRecord::Base
    has_one :options, :class_name => "ClanOptions", :foreign_key => "clan_id", dependent: :destroy
    accepts_nested_attributes_for :options
end

clan_options.rb

class ClanOptions < ActiveRecord::Base
  belongs_to :clan
end

要为clan.rbclan_options.rb创建一个编辑表单,我在edit.html.erb中使用以下内容:

<%= form_for @clan, :html => {:class => 'form-horizontal'} do |clan| %>
    <fieldset>
        <!-- Form stuff -->
        <%= clan.fields_for :options do |o| %>
            <!-- o.text_field -->
        <% end %>
    </fieldset>
<% end %>

我可以更新clan.rb的字段,但是当我尝试编辑值 backgroundurl 时它不会保存它。Backgroundurl 是clan_options.rb 之一

编辑:更新代码和文本。

部落控制器.rb

class ClansController < ApplicationController
    before_filter :check_login, :only => [:new, :edit]
    before_filter :check_bound, :only => [:new, :edit]
    before_filter :check_clan, :only => :new

    def update
        @clan = Clan.find(params[:id])
        if @clan.update_attributes(clan_update_params)
            flash[:status] = TRUE
            flash[:alert] = "Successfully updated your clan."
            redirect_to clan_path(params[:id])
        else
            flash[:status] = FALSE
            flash[:alert] = @clan.errors.full_messages

            redirect_to edit_clan_path(@clan.id)
        end
    end

    def edit
        clan = Clan.where(id: params[:id])
        if !clan.blank?
            @clan = Clan.find(params[:id])

            user = User.where(id: session[:user_id])
            if !user.blank?
                #De gebruiker is ingelogt en zit in de clan
                @user = User.find(session[:user_id])
                if @clan.id != @user.clan.id
                    flash[:status] = FALSE
                    flash[:alert] = 'That was not your clan, you may not edit theirs.'
                    redirect_to clans_path
                elsif @user.clanmember.group.rank != 10
                    flash[:status] = FALSE
                    flash[:alert] = "You must be the leader to edit the clan."
                    redirect_to clan_path(@clan.id)
                end
            end
        else
            flash[:status] = FALSE
            flash[:alert] = 'that clan doesn\'t exist or has been removed.'
            redirect_to clans_path
        end
    end

    def clan_params
        params.require(:clan).permit(:name, :prefix, :description, :user_id)
    end

    def clan_update_params
        params.require(:clan).permit(:name, :prefix, :description, :user_id, options: [:id, :clan_id, :backgroundurl])
    end
end
4

1 回答 1

0

我建议您阅读 Rails 指南。它会在开始时提供您需要的大部分答案。但这里是您问题的相关部分:http: //guides.rubyonrails.org/getting_started.html#updating-posts

于 2013-10-25T21:51:29.853 回答