开发者

Does Rails 3 Have a new way to get the primary key?

开发者 https://www.devze.com 2023-03-08 07:38 出处:网络
I am trying to go through a learning rails book, but I keep running into road bumps as it is meant for rails 2.something.I have been able to get around them until now, while the script still runs almo

I am trying to go through a learning rails book, but I keep running into road bumps as it is meant for rails 2.something. I have been able to get around them until now, while the script still runs almost perfectly fine, it lacks one function... I have the following code in ads_controller.rb:

 class AdsController < ApplicationController
  def create
    @ad = Ad.new(params[:ad])
    @ad.save
  end
    def new
    @ad = Ad.new
  end


  def show
    @ad = Ad.find(params[:id])
  end


  def index
    @ads 开发者_开发百科= Ad.find(:all)
  end

end

Thefollowing Code In routes.rb:

    MeBay::Application.routes.draw do |map|

  map.connect '/ads/new', :controller=>'ads', :action=>'new'
  map.connect '/ads/create', :controller=>'ads', :action=>'create'
  map.connect '/ads/', :controller=>'ads', :action=>'index'
  map.connect '/ads/:id', :controller=>'ads', :action=>'show'
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action;:id.:format' 

The following in new.html.erb:

<h1>New Ad</h1>
<%= form_for @ad, :url=>{:action=>'create'} do |f| %>
    <p><b>Name:</b><br /><%= f.text_field:name %></p>
    <p><b>Description</b><br /><%= f.text_area:description %></p>
    <p><b>Price</b><br /><%= f.text_field:price%></p>
    <p><b>Seller</b><br /><%= f.text_field:seller_id%></p>
    <p><b>Email</b><br /><%= f.text_field:email %></p>
    <p><b>Image URL</b><br /><%= f.text_field:img_url %></p>
    <p> <%= f.submit "Create" %> </p>
<% end %>

and the Following in create.html.erb:

<h3>NEW AD CREATED FOR <%= @ad.name %>!</h3>
<a href="/ads/<% @ad.id %>">Click To View</a>

What happens, when I click on the submit button when on the new page I get transfered to the create template, which displays the name, however, it can not get the id from the variable and thus returns to the list page, but it is suppose to return to the ads newly created page..(ie.. /ads/7, but returns /ads/)

Is there a syntax problem or a new way to get it in rails 3?


Change:

<a href="/ads/<% @ad.id %>">Click To View</a>

With:

<a href="/ads/<%= @ad.id %>">Click To View</a>

By the way, why don't you use url helpers?

0

精彩评论

暂无评论...
验证码 换一张
取 消