marios
marios

Reputation: 261

pass parameters with ror

i trying to pass parameter from one site to the next site and use it but i have a problem!

store>index

<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>

in the next site when i press the button i want to see it it says :

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

and does not open the page

if i remove the (building.id) it says :

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

it takes me to the next site but without param

views>wish_receive.text.erb <= this send me those info on email

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

routes

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

rake routes command

      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

Upvotes: 0

Views: 149

Answers (1)

styliii
styliii

Reputation: 646

Can you show us your routes.rb file and what rake routes outputs?


Here we go.

In your routes.rb file, add this:

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

In your seeits_controller.rb file, your new method should look like this:

  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

Lastly, in your view file, replace your button_to code with this

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

Note that now, your button passes in the variable building_id as a parameter, allowing you to access the @building object. The new action in your controller is now a post function, because we're sending information to the server. Now you can use the @building in your new template. Hope this helps.

Upvotes: 2

Related Questions