I'm new in Ruby on Rails and I'm creating my own Ruby on Rails - Blog... For my tweets I'm creating a selfmade Scaffold...
Even i press the submit button.. for my new post.. i will get this error msg:
uninitialized constant PostsController
Here are my files:
new.html.erb
<h1>Add a new Tweet</h1>
<%= form_for(#post) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
blog_controller.rb
class BlogController < ApplicationController
def new
#post = Post.new
end
def create
#post = Post.new(params[:post])
redirect_to #post
end
end
routes.rb
Me::Application.routes.draw do
resources :posts
resources :post
match '/' => 'blog#home'
match '/archiv' => 'blog#archiv'
match '/tweets' => 'blog#new'
root :to => 'blog#home'
end
I made a model with these syntax:
rails g model post title:string content:text
And at least rake db:migrate
solved by myself: made a new controller
Related
I am building a Rails from from scratch (without the use of Scaffold for the first time) to do a simple thing: submit to a database the username and a post.
To do that, in my Stories Controller, I have set up a create method:
class StoriesController < ApplicationController
def index
#posts = Post.where(slug: params[:id]).all
end
def create
#comments = Comment.new(params[:comments])
#comments.save
redirect_to(:back)
end
end
Then, in my view, I have this form in my Stories view:
<%= form_for :comments do |f| %>
<div class="form-group">
<div class="field">
<%= f.label :username %><br>
<%= f.text_field :username, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="field">
<%= f.label :post %><br>
<%= f.text_area :post, class: "form-control" %>
</div>
</div>
<p>
<%= f.submit %>
</p>
<% end %>
What's weird is that when I click save, it fails to save to the table and has a routing error:
No route matches [POST] "/stories"
Troubleshooting
First off, I want to redirect back to the same page I was on, which has a URL like http://localhost:3000/stories?id=example-post-slug. I thought the routing error was weird, because shouldn't it just go back based on my controller code?
I also thought my routes.rb file could be an issue, but I have added it as a resource as other answers have suggested:
routes.rb
Rails.application.routes.draw do
resources :posts
resources :comments
get "stories" => "stories#index", as: :stories
....
end
Thus, I am confused. Why am I having a routing issue when I have defined the create method, added the Comments table as a resource, and I have chosen a redirect in my controller?
Ive been going through this for hours and i cant seem to find the problem. Im doing a simple contacts form in rails , this is a tutorial im following so bear with me. I get the following error message :
undefined method `name' for #
I have this in my DB/migrate file:
class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|
t.string :name
t.string :email
t.text :comments
t.timestamps
end
end
end
this on my contacts_controller.rb file:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
end
end
and this is on my pages_controller.rb:
class PagesController < ApplicationController
def homepage
end
def about
end
end
This is my html :
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
Did you run your migration?
If not, do so:
bundle exec rake db:migrate
This will create the contacts table in your database (which has the name column along with other columns email and comments that you're using while building your form). Also, re-start your rails server and then you should be able to use your form code without any problem.
You have probably not migrated your table.
In your console run:
rake db:migrate
Then restart your server and try again.
The other answers summed it up well - you need to migrate your contacts table:
$ rake db:migrate
You'll want to read up on Rails migrations here.
As an aside,
Pages
I don't know which tutorial everyone seems to be following, it's telling you to use specific actions for static pages, which IMO is pretty bad practice (especially in the long run).
A much more versatile (and "Railsy*) solution is to put the content for the data in the database, which you can then call with a model - allowing you to have a single show view for all your pages:
#config/routes.rb
resources :pages, only: [:show]
#app/controllers/pages_controller.rb
class PagesController < ApplicationController
def show
#page = Page.find params[:id]
end
end
#app/views/pages/show.html.erb
<%= #page.title %>
<%= #page.body %>
You'd have to create another migration for the pages datatable, as follows:
$ rails g migration CreatePages
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
$ rake db:migrate
HTML
Your current HTML can be a lot more efficient:
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
<%= form_for #contact do |f| %>
<% attributes = {name: "text_field", email: "email_field", comments: "text_area"} %>
<% attributes.each do |attr,val| %>
<div class="form-group">
<%= f.label attr.to_sym %>
<%= f.send(val, attr.to_sym, class: 'form-control') %>
</div>
<% end %>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
</div>
</div>
</div>
I'm trying to add a custom create action for my Book model, but i keep ending up with a "Couldn't find Book without an ID".
routes.rb:
Books::Application.routes.draw do
resources :books
resources :books do
collection do
post 'create_new_record', :action => :create_new_record
end
end
match 'create_new_record' => 'books#create_new_record', via: [:post]
The relevant controller action:
def create_new_record
#book = Book.new(book_params)
respond_to do |format|
if #book.save
format.html { redirect_to #book, notice: 'New book record created.' }
end
end
end
And my form (in new.html.erb). I'm looping through results that i get from goodreads.com.
<% #book_search.results.work.each do |stuff| %>
<%= form_for(#book, :url => create_new_record_books_path) do |f| %>
<div class="field">
<%= f.label :author %><br>
<%= f.text_field :author, :value => stuff.best_book.author.name %>
</div>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title, :value => stuff.best_book.title %>
</div>
<div class="field">
<%= f.label :isbn %><br>
<%= f.text_field :isbn, :value => stuff.best_book.isbn %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.text_field :image, :value => stuff.best_book.image_url %>
</div>
<div class="field">
<%= f.label :bookid %><br>
<%= f.text_field :bookid, :value => stuff.best_book.id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<hr>
<% end %>
The error i get when submitting the form is:
ActiveRecord::RecordNotFound in BooksController#create_new_record
on the callback
def set_book
#book = Book.find(params[:id])
end
I'm pretty much stumped now, my understanding is that it doesn't even reach the action, but instead looks for a book id that doesn't exist?
Thank you!
If you use before_filter so you don't pass an id to create action. Call your before filter the following way:
before_filter :set_book, except: [:index, :new, :create]
If you use model callback, params is unavailable in the model so pass the id some other way, for example via attr_accessor.
use #book = Book.where(id: params[:id]).first
Hi I'm playing with Rails at the moment and building a basic app. When I try to run the app I get this error:
"undefined method `products_path' for #<#:0x45c19f8>"
My code is as follows...
Config:
Depot::Application.routes.draw do
resources :product
resources :test
end
Controller:
class ProductController < ApplicationController
def new
#product = Product.new
end
def show
#product = Product.find(params[:id])
end
end
View:
<h1>Page to add new products</h1>
<%= form_for(#product) do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :price %>
<%= f.text_field :price %>
<%= f.submit "Create new product" %>
<% end %>
I don't understand why the form won't render and I receive the error message. Am I missing something?
Thanks any help appreciated.
Edited to add config file.
Just add the following line in config/routes.rb
resources :products
I am not sure if I'm structuring my application corretly (I've been learning Rails for 2 months now) but I am building a pretty deeply nested application that looks like this:
user has_many accounts > accounts has_many characters > characters has_many items
So it's 4 levels deep (that's the plan at least).
I'm currently at characters and I'm having trouble creating the form which is throwing up this error: undefined method 'characters' for nil:NilClass (screenshot).
Here's the project on github: https://github.com/imjp/d2shed
characters_controller.rb
class CharactersController < ApplicationController
def create
#user = User.find(params[:user_id])
#account = Account.find(params[:account_id])
#character = #account.characters.create(params[:character])
redirect_to root_url
end
end
character.rb
class Character < ActiveRecord::Base
attr_accessible :name, :type
belongs_to :account
end
_form.html.erb
<%= form_for([#account, #account.characters.build]) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.radio_button(:type, "SC") %>
<%= f.label(:type, "SC") %>
<%= f.radio_button(:type, "HC") %>
<%= f.label(:type, "HC") %>
<%= f.radio_button(:type, "SCL") %>
<%= f.label(:type, "SCL") %>
<%= f.radio_button(:type, "HCL") %>
<%= f.label(:type, "HCL") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You get this error because you don't define #account in the users controller in the show action,
class UsersController < ApplicationController
...
def show
#user = User.find(params[:id])
#account = #user.accounts.first # Otherwise #account == nil
...
end
...
end
Also, the route in your form don't look right.
The create action for the Character resource is like this in the routes:
POST /:user_id/accounts/:account_id/characters
So you need to provide, :user_id, :account_id, and character
like this:
<%= form_for [#user, #account, #account.characters.build] %>