Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert First post, requesting sage pe...

Replytotopic

First post, requesting sage perspective

Posted in Forums : Ask a Rails expert

 
Wwr-jess

Authority 12
Posting Rating 0
Sign in to rate this post

Hello, I’m working on a basic app for learning purposes. Nothing critical, but I’m not meeting much success asking for help on railsforum.com.

It’s a very simple juncture (for an expert). I’ve got a /post page with shows a list of entries, and a /post/create page for people to create entries. Mainly I’m trying to figure out how to redirect from /post/create (post created) to /post—with the correct flash notice. Also how to customize the flash notice color scheme for success / error.

I do need help with this problem, but more than that, I need good, clear direction on how to proceed into the more complex functionalities. Thank you :-)

==================
post_controller.rb
==================
class PostController < ApplicationController def index @title = “Gravity” list end

def list
  @posts = Post.find(:all)
end
def show
end
def new
  @post = Post.new(params[:post]).save
end
def create
  @title = "Gravity" 
  if request.post? and params[:post]
    if new
      flash[:notice] = "Comment created!" 
      redirect_to posts_path
    else
      flash[:notice] = "Please complete all fields." 
      render :action => "create" 
    end    
  end
end
def preview
end

end

==================
create.haml
==================
%br
%fieldset %legend New Post – form_for(:post, :url => { :action => “create” }) do |f| = f.label :title, “Title” %br = f.text_field :title %br %br = f.label :content, “Comment” %br = f.text_area :content, { :rows => 8 } %br %br = f.label :author, “Your Name” %br = f.text_field :author %br %br = f.submit “Create Post” – end

==================
routes.rb
.
.
. # Sample resource route (maps HTTP verbs to controller actions automatically): # map.resources :products map.resources :posts
.
.
.
==================

 
Wwr-jess

Authority 12
Posting Rating 0
Sign in to rate this post

Goof. I changed post_path to ’/post’ and the successful message works. I guess I’m trucking when I get help with validating each param. I put ‘validates_presence_of :author … etc. before the method listings? Is there a better way than using a string literal for ‘posts_path’ ? Thanks.

 
Wwr-jess

Authority 12
Posting Rating 0
Sign in to rate this post

the following does not seem to work, but I am able to browse to the /post page and see the “please comlete all fields” message. The error, each time I enter a comment without one of the attributes follows the Post model.

class Post < ActiveRecord::Base

validates_presence_of :title
validates_presence_of :content
validates_presence_of :author

end

NoMethodError in Post#create

Showing post/create.haml where line #7 raised:

undefined method `title’ for false:FalseClass

Extracted source (around line #7):

4: – form_for(:post, :url => { :action => “create” }) do |f|
5: = f.label :title, “Title”
6: %br
7: = f.text_field :title
8: %br
9: %br
10: = f.label :content, “Comment”

 
Avatar

Authority 12
Posting Rating 0
Sign in to rate this post

if you deal with RESTfull controllers, the code should looks like the following

  1. GET /posts/new
    def new @post = Post.new
    end
  1. POST /posts
    def create @post = Post.new(params[:post]) if @post.save flash[:notice] = “Comment created!” redirect_to posts_path else flash[:notice] = “Please complete all fields.” render :action => “new” end end
    end

this way you will get the @post object of the Post class on the form.

Replytotopic

Other Recent Topics

Ask a Rails expert : How to work with ror 2.1.1 using Netbeans IDE 6.1

Ask a Rails expert : json gem error

Ask a Rails expert : Problem with break

Ask a Rails expert : activesupport string first method error

Ask a Rails expert : url_for broken ?

Ask a Rails expert : map.routes.rb pls help

Ask a Rails expert : how to h tag in controller

Ask a Rails expert : Problem with Restful routing and partial form

Ask a Rails expert : will_paginate, search and ajax

Ask a Rails expert : captcha

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel