0

我试图将参数从一个站点传递到下一个站点并使用它,但我遇到了问题!

商店>索引

<h1>All Priorities</h1>


<% @buildings.each do |building| %>
<div class="entry">
    <div class="img">
    <%= image_tag (building.photo.url)%></div>
    <div class=" disc">
    <h3>Name of the Bulding:  <%= building.title %></h3>
     <h4>Status: <%= building.status %></h4>
    Info: <%=sanitize(building.description)%>
    <div class="price_line">
        <span class="price">Price: <%= sprintf("€ %0.02f",building.price)%></span><br/>
        <div class="button">
        <%= button_to "I Want to See it", new_seeit_path(building.id), :method => :get%>

</div>
    </div>
    </div>


    <%end%>
    <div class="pages">
    <%= will_paginate @buildings %></p>
    </div>

在下一个站点中,当我按下我想查看的按钮时,它说:

http://localhost:3000/seeits/new.4?

并且不打开页面

如果我删除(building.id)它说:

http://localhost:3000/seeits/new?

它带我到下一个站点,但没有参数

意见>wish_receive.text.erb <= 这通过电子邮件向我发送这些信息

You got a wish from   <%= @seeit.name %> 
<%= @seeit.telephone_number %>
<%= @seeit.email %>
<%= @seeit.comments %>

路线

Projectproperties::Application.routes.draw do
  resources :seeits

  get "about/index"

  get "contact/index"

  get "store/index"

  resources :buildings
 root :to => 'store#index', :as => 'store'
end

seeits_controller

def new
    @seeit = Seeit.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @seeit }
    end
  end

  # GET /seeits/1/edit
  def edit
    @seeit = Seeit.find(params[:id])
  end

  # POST /seeits
  # POST /seeits.json
  def create
    @seeit = Seeit.new(params[:seeit])

    respond_to do |format|
      if @seeit.save
        Notifier.wish_received(@seeit).deliver
        format.html { redirect_to(store_url, notice: 'Thank you for your wish will contac you very soon.') }
        format.json { render json: @seeit, status: :created, location: @seeit }
      else
        format.html { render action: "new" }
        format.json { render json: @seeit.errors, status: :unprocessable_entity }
      end
    end
  end

耙路线命令

      seeits GET    /seeits(.:format)             seeits#index
              POST   /seeits(.:format)             seeits#create
    new_seeit GET    /seeits/new(.:format)         seeits#new
   edit_seeit GET    /seeits/:id/edit(.:format)    seeits#edit
        seeit GET    /seeits/:id(.:format)         seeits#show
              PUT    /seeits/:id(.:format)         seeits#update
              DELETE /seeits/:id(.:format)         seeits#destroy
  about_index GET    /about/index(.:format)        about#index
contact_index GET    /contact/index(.:format)      contact#index
  store_index GET    /store/index(.:format)        store#index
    buildings GET    /buildings(.:format)          buildings#index
              POST   /buildings(.:format)          buildings#create
 new_building GET    /buildings/new(.:format)      buildings#new
edit_building GET    /buildings/:id/edit(.:format) buildings#edit
     building GET    /buildings/:id(.:format)      buildings#show
              PUT    /buildings/:id(.:format)      buildings#update
              DELETE /buildings/:id(.:format)      buildings#destroy
        store        /                             store#index
4

1 回答 1

2

你能告诉我们你的 routes.rb 文件和 rake routes 输出吗?


开始了。

在您的 routes.rb 文件中,添加以下内容:

  resources :seeits do
    member do 
      post 'new'
    end
  end

在您的 seeits_controller.rb 文件中,您的新方法应如下所示:

  def new
      # raise params.inspect
      @seeit = Seeit.new
      @building = Building.find(params[:building_id])

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @seeit }
      end
    end

最后,在您的视图文件中,将您的 button_to 代码替换为

<%= button_to("I want to see it", {:controller => "seeits", :action => "new", :building_id => building.id})%>

请注意,现在,您的按钮将变量 building_id 作为参数传递,允许您访问 @building 对象。控制器中的新操作现在是一个 post 函数,因为我们正在向服务器发送信息。现在您可以在新模板中使用@building。希望这可以帮助。

于 2012-09-26T16:19:48.807 回答