发送到新操作而不是发布

时间:2014-02-14 03:52:59

标签: ruby-on-rails post

我正在控制器中从新方法发送一个表单,但不是通过创建操作,而是返回到新操作。关于这一点有无数的帖子,我做了一些研究但没有结束工作。我不确定是不是因为我的新网址不仅仅是'/ purchase / new',而是“/purchase/new#{trip.id}”。我不认为这应该有所作为。

应用/控制器/ purchases_controller.rb

class PurchasesController < ApplicationController


  def new
    trip = Trip.find(params[:trip_id])

    @purchase = trip.purchases.build

    if @purchase.trip_id == 1
        render 'paris'
    elsif @purchase.trip_id == 2
        render 'ny'
    else
        render 'tokyo'
    end
  end

 def create
    @purchase   = Purchase.new(purchase_params)
    if @purchase.save
        redirect_to '/', :notice => "Enjoy your trip"
    else
        redirect_to '/'
    end
 end    

def show
end



private

def purchase_params
    params.require(:purchase).permit(:email)
end
end

应用/视图/布局/ _form.html.erb

    <br>
<legend> Buy Now! </legend>
<%= form_for(@purchase, :method => :post) do |f| %>
 <% if @purchase.errors.any? %>
    <div class="error_messages">
        <ul>
            <%= @purchase.errors.full_messages.each do |error| %>
                <li> <%= error %> <li>
            <% end %>
        </ul>
    </div>
    <%end%>

    <%= f.hidden_field :trip_id %>

    <div class = "uk-form-row">
        <%= f.label :email %>
        <%= f.text_field :email %>
    </div>
        <div class = "uk-form-row">
            <%= label_tag :card_number, "Credit Card Number" %>
            <%= text_field_tag :card_number, nil, name: nil %>
        </div>
        <div class = "uk-form-row">
            <%= label_tag :card_code, "Security Code" %>
            <%= text_field_tag :card_code, nil, name: nil %>
        </div>
        <div class = "uk-form-row">
            <%= label_tag :card_month, "Credit Expiration"   %>
           <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
      <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
        <div><br>
    <div class = "uk-form-row submit">
        <%= f.submit "Purchase" %>
    </div>
<% end %>

应用/视图/购买/ paris.html.erb

  <nav class="uk-navbar">
<ul class= "uk-navbar-nav">
    <li class = "uk-actve"><a href="/">Home Page</a></li>
    <li class = "uk-actve"><a href="">Account</a></li>
    <li class = "uk-actve"><a href="">About Us</a></li>
</ul>
</nav>

 <div class = "uk-grid">

   <div class = "uk-width-4-6 greeting_div">

    <div class = "img_div"><img class="trip_page" src= <%= asset_path('top_eiffel.jpg') %>></div>
    <h1 class= "greetings"> Greetings from: </h6>


  </div>

  <div class = "uk-width-2-6 greeting_div_2">
     <img class= "stamp" src = <%= asset_path('french_stamp.jpg') %> >
     <div class= "trip_information">
        <p>To: <%=@purchase.trip.location %></p><br>

        <p>From: <%=@purchase.trip.cost%> for 10 days</p>
        <form class = "uk-form"> <%= render 'layouts/form' %></form>
     </div>

 </div>
</div>

应用/视图/ home_pages / index.html.erb

<div class = "uk-width-medium-1-3">
            <div class = 'panel'>
                <figure class = "uk-panel uk-panel-box front card">
                     <video src= <%=asset_path('paris.webm') %>  preload autoplay loop>
    Your browser does not support the video.
    </video>
                </figure>
                <figure class = "uk-panel uk-panel-box back card"> 
                        <img src =<%=asset_path('Eiffel.jpeg') %>> 
                        <div class="ribbon-wrapper-green"> <div class = "ribbon-green"><%= link_to "Click For Pricing!", new_purchase_path(trip_id: @paris.id)%></div></div>
                        <h3 class = "uk-panel-title">Paris</h3>
                </figure> <!-- close back -->
            </div> <!-- close flipper -->
</div> <!-- close width -->

<div class = "uk-width-medium-1-3">
            <div class = 'panel'>
                <figure class = "uk-panel uk-panel-box front card">
                     <video src= <%=asset_path('NYC.webm') %>  preload autoplay loop>
    Your browser does not support the video tag.
    </video>
                </figure>
                <figure class = "uk-panel uk-panel-box back card">
                        <img src =<%=asset_path('empire2.jpg') %>> 
                        <div class="ribbon-wrapper-green"> <div class = "ribbon-green"><%= link_to "Click For Pricing!", new_purchase_path(trip_id: @ny.id)%></div></div>
                        <h3 class = "uk-panel-title">New York City</h3>
                </figure> <!-- close back -->
            </div> <!-- close flipper -->
</div> <!-- close width -->

<div class = "uk-width-medium-1-3">
            <div class = 'panel'>
                <figure class = "uk-panel uk-panel-box front card">
                     <video src= <%=asset_path('Tokyo.webm') %> preload autoplay loop>
    Your browser does not support the video tag.
    </video>
                </figure>
                <figure class = "uk-panel uk-panel-box back card">
                        <img src =<%=asset_path('fuji.jpg') %>> 
                        <div class="ribbon-wrapper-green"> <div class = "ribbon-green"><%= link_to "Click For Pricing!", new_purchase_path(trip_id: @tokyo.id)%></div></div>
                        <h3 class = "uk-panel-title">Tokyo</h3>
                </figure> <!-- close back -->
            </div> <!-- close flipper -->
</div> <!-- close width -->

配置/路由         资源:购买

2 个答案:

答案 0 :(得分:0)

<%=form_for @purchase, :url=>{:action=> "create", :controller=>"purchases"}, :method=>"post" do |f| %>

#Your form html code will here

<%end%>

答案 1 :(得分:0)

事实证明,当我渲染表单时,我在页面中呈现它       <form>标记。 Rails不喜欢那样。只要我将<form>更改为<div>,提交请求便会进入后期路线。