为什么我的帖子会把我带到错误的网址?

时间:2012-12-24 01:24:56

标签: ruby-on-rails ruby-on-rails-3 routing

当我发帖时,它需要我

  

example.com/shop.:community_name

而不是

  

example.com/shop/:community_name

我的routes.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

2 个答案:

答案 0 :(得分:1)

应该是

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

update的{​​{1}}行动中。

答案 1 :(得分:1)

讨厌命名路线......

redirect_to @community