Rails 3 Маршруты выдаются в форме Править - PullRequest
0 голосов
/ 11 мая 2011

По сути, я могу создать новый элемент, который будет сохранен в моей таблице в моей БД. но когда я иду, чтобы отредактировать элемент, открывается форма, я делаю изменения, а затем, когда я иду, чтобы отправить, я перехожу на тот же URL-адрес, что и страница редактирования, и выдает мне ошибку маршрутизации. Не найдено ни одного маршрута "/ support / 14 / edit ", хотя, если вы введете это в адресную строку, он просто откроет форму редактирования, но мои изменения не будут сохранены. Так вот мой код.

routes.rb

resources :support

support_controller.rb

def new
  @support_item = Support.new

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @support_item }
  end
end

# GET /support/1/edit
def edit
  @support_item = Support.find(params[:id])
end

# POST /support
# POST /support.xml
def create
  @support_item = Support.new(params[:support_item])

  respond_to do |format|
    if @support_item.save
      format.html { redirect_to("/support", :notice => 'Question was successfully created.') }
    else
      format.html { render :action => "new" }
    end
  end
end

# PUT /support/1
# PUT /support/1.xml
def update
  @support_item = Support.find(params[:id])

  respond_to do |format|
    if @support_item.update_attributes(params[:support_item])
      format.html { redirect_to("/", :notice => 'Question was successfully updated.') }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @support_item.errors, :status => :unprocessable_entity }
    end
  end
end

support.rb

class Support < ActiveRecord::Base
  belongs_to :role

  scope :admin_available, order("role_id ASC") do
      Support.all
  end

  def self.available(user)
    questions =   where(:role_id => 1)
    questions +=  where(:role_id => user.roles)
    questions
  end
end

_form.html.erb

  <% if @support_item.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@support_item.errors.count, "error") %> prohibited this question from being saved:</h2>

      <ul>
      <% @support_item.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label "Support item for:" %><br />
    <%= f.collection_select :role_id, Role.find_by_max(5), :id, :name, {:default => 'everyone'} %>
  </div>
  <div class="field">
    <%= f.label :question %><br />
    <%= f.text_field :question, :class => 'genForm_question'%>
  </div>
  <div class="field">
    <%= f.label :answer %><br />
    <%= f.text_area :answer, :class => 'genForm_textarea' %>
  </div>
  <div class="field">
    <%= f.label :url %><br />
    <%= f.text_field :url, :class => 'genForm_question' %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>

new.html.erb

<h1>New Support Item</h1>

<% form_for @support_item, :url => { :action => "create" }, :html => { :method => :post } do |f| %>
    <%= render 'form', :f => f %>
<% end %>

edit.html.erb

<h1>Editing Support Item</h1>

<% form_for @support_item, :url => { :action => "edit" }, :html => { :method => :post } do |f| %>
        <%= render 'form', :f => f %>
<% end %>

Я полагаю, это весь соответствующий код.

1 Ответ

1 голос
/ 11 мая 2011
<h1>Editing Support Item</h1>

<% form_for @support_item do |f| %>
        <%= render 'form', :f => f %>
<% end %>

Вы переопределяете URL.Он должен быть в состоянии генерироваться автоматически, если вы делаете все со стандартным отдыхом.Если это не сработает, просто знайте, что вы не хотите отправлять в / support_items / 1 / edit, вы хотите отправить в /support_items/1.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...