2

当我发帖时,它需要我

example.com/shop.:community_name

代替

example.com/shop/:community_name

我的路线.rb

resources :communities, :path => "shop", do
    resources :community_topics, :path => "topic", :as => :'topic'
end

_form.html.erb

<%= form_for @community, :html => { :class => 'form-horizontal' } do |f| %>
...
    <%= f.submit nil, :class => 'btn btn-primary' %>
    <%= f.submit 'Destroy', 
    :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
    :class => 'btn btn-danger', :name => 'destroy' unless @community.new_record? %>
<% end %>

在 community_controller.rb 中更新操作我使用 to_param 这就是为什么使用 community_name

def update

        @community = Community.find_by_community_name(params[:id])      

        respond_to do |format|
            if @community.update_attributes(params[:community])
                format.html { redirect_to communities_path(@community.community_name), notice: 'Community was successfully updated.' }
                format.json { head :no_content }
            else
                format.html { render action: "edit" }
                format.json { render json: @community.errors, status: :unprocessable_entity }
            end
        end
end
4

2 回答 2

1

它应该是

... redirect_to community_path(@community.community_name) ...

update你的行动中CommunitiesController

于 2012-12-24T01:30:36.887 回答
1

讨厌命名路线...

redirect_to @community
于 2012-12-24T01:35:12.400 回答