0

我想换掉:

   <%= button_tag "Create" , :class => "round" %>

为了

  <%= link_to trips_path(@trip),  :class => "round", :method => :post do %>
    <div id="createtrip_button_newtrip">
      Create
    </div>
  <% end %>

在表格中

<%= form_for @trip, :html =>{ :multipart => true} do |f| %>


  <div class="field">
    <%= f.label :name %><br/>
    <%= f.text_field :name  %>
  </div>
  <div id="description_container_newtrip">
    <%= f.label :description %><br/>
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :location %><br/>
    <%= f.text_field :location %>
  </div>

  <div class="field">
    <%= f.label :start_date, "Start Date:" %><br/>
    <%= f.date_select :start_date, :order => [:month, :day, :year]   %>
  </div>
  <div class="field">
    <%= f.label :end_date, "End Date:" %><br/>
    <%= f.date_select :end_date, :order => [:month, :day, :year] %>
  </div>
  <div class="field">
    <%= f.hidden_field :user_id %>
  </div>
  <div class="field">
    <%= f.hidden_field :traveldeal_id %>
  </div>

  <%= #button_tag "Create", :class => "round" %>


  <%= link_to trips_path(@trip),  :class => "round", :method => :post do %>
    <div id="createtrip_button_newtrip">
      Create
    </div>
  <% end %>

<% end %>

link_to 在正确的控制器中调用正确的操作,但我的表单没有保存。这是方法:

  def create
    @trip = Trip.new(params[:trip])
    if @trip.save
      redirect_to root_path
    else
      redirect_to login_path
    end
  end

我不断被重定向到 login_path。我认为我的问题是我没有将@trip 的参数发送到控制器。

有小费吗?

编辑:

这是我的开发日志中的参数哈希:

Started POST "/trips" for 127.0.0.1 at 2012-04-07 15:23:37 -0700 Processing by TripsController#create as HTML Parameters: {"authenticity_token"=>"+D2icFw2pz6qwYVaeXoWg5MOJg+Sks5/ckN0lVn1r4o="} [1m[36m (0.1ms)[0m [1mbegin transaction[0m [1m[35m (0.0ms)[0m rollback transaction Redirected to http://localhost:3000/login Completed 302 Found in 2ms (ActiveRecord: 0.1ms)

4

1 回答 1

2

我认为问题在于您link_to不会影响表格。你可以用一些 javascript 来解决这个问题:

$(".submit_link").click(function(){
  var link = $(this);
  var form = link.closest("form");
  form.submit();
  return false;
});

然后将课程添加submit_link到您的链接中。

如果您不想要 javascript,请使用submit_tag,并使用 CSS 设置样式。有关走这条路线的一些提示,请参见此处:http ://cssbutton.com/forms/

于 2012-04-07T22:25:49.033 回答