Rails input data not working between pages - ruby-on-rails

I want to made a page that shows you what you introduced in the text box after you pressed the button this is my code. What is wrong? Thanks.
routes.rb
Rails.application.routes.draw do
get '/page' => 'pages#page'
post '/act/' => 'pages#act'
pages_controller.rb
class PagesController < ApplicationController
def page
#input = params[:field1]
end
def act
#input = params[:field1]
redirect_to '/page'
end
end
page.html.erb
<%= form_tag( {:controller => :pages, :action => 'act' }, :method => "post") do %>
<%= text_field_tag :field1 %>
<%= submit_tag "Show text-box contents" %>
<% end %>
<br>
<p> Your input is </p>
<br>
<%= #input %>

def act
render :page
end
instead of redirect_to '/page' will work.
but this is not the rails way of doing.
Please see Rails guide for better controllers .
You can remove #input = params[:field1]
in routes.rb you can just use
resources :pages
run rake routes in console
You can see all the inbuild routes rails give you.
In your case def page should be def new
and def act should be def create
Your page.html.erb should be changed to new.html.erb
<%= form_for #input, url: {action: "create"} do %>
<%= text_field_tag :field1 %>
<div><%= submit_tag 'Save' %></div>
<% end %>

Related

Ruby On Rails: To Do App- Can't connect my form with to-do index page

I'm trying to create a "to-do" app on Ruby on Rails that uses a simple "name" form to generate a to-do list.
I was successfully able to create the form as well as the main index page, but I am not able to connect the two. This means that I can type fill out the form but when I submit it, it just brings me back to the index page without the form item listed. I'm stumped!
Here are a view of my code:
welcome controller:
class WelcomeController < ApplicationController
def index
if user_signed_in?
redirect_to :controller => "todoodoo" , :action => "index"
end
end
def home
#todos = Todoodoo.where(done: false)
#completes = Todoodoo.where(done: true)
end
end
todo controller (todoodoo):
class TodoodooController < ApplicationController
def index
#todos = Todoodoo.where(done: false)
#completes = Todoodoo.where(done: true)
end
def new
#todo = Todoodoo.new
end
def todo_params
params.require(:todoodoo).permit(:name, :done)
end
def create
#todo = Todoodoo.new(todo_params)
if #todo.save
redirect_to todoodoo_index_path, :notice => "Your To-Do item was created!"
else
render "new"
end
end
def update
#todo = Todoodoo.find(todo_params[:id])
if #todo.update_attribute(:done, true)
redirect_to todoodoo_index_path, :notice => "Your To-Do item was marked as done!"
else
redirect_to todoodoo_index_path, :notice => "Your To-Do item was unable to be marked as done!"
end
end
def destroy
#todo = Todoodoo.find(todo_params[:id])
#todo.destroy
redirect_to todoodoo_index_path, :notice => "Your To-Do item was deleted!"
end
end
This is my view file for the todo controller (todoodooo):
<h1> To-Do </h1>
<h2> To Do </h2>
<% #todos.each do |t| %>
<p>
<strong><%= t.name %></strong>
<small><%= link_to "Mark as Done", todoodoo_path(t), :method => :put %></small>
</p>
<% end %>
<h2> Completed </h2>
<% #completes.each do |t| %>
<p>
<%= t.name %>
<small><%= link_to "Remove", t, :confirm => "Is it okay to remove this from the list?", :method => :delete %></small>
</p>
<% end %>
This is my view file for the form:
<h1> Add new item to your To-Do list </h1>
<%= form_for #todo, :url => todoodoo_index_path(#todo) do |f| %>
<%= f.label :Name %>: <%= f.text_field :name %>
<%= f.hidden_field :done, :value => false %>
<%= f.submit "Add to To-Do list", class: "btn btn-primary" %>
<% end %>
And my routes.rb file:
Rails.application.routes.draw do
get 'welcome/home'
get 'todoodoo/index'
match "todoodoo/index" => "todoodoo#index", :via => :post
get 'people/new'
devise_for :users
root 'welcome#home'
resources :todoodoo do
member do
put :complete
end
end
I'm really new to Rails development so I apologize if I missed something out or if this doesn't make sense. I've tried my best to include every dependent file. Thanks for any input you can add. Thanks! (also, forgive me of the formatting, please know that there isn't any errors in the formatting in my code)
Since you're new, here's what you need to do
By the way, I'd highly recommend upgrading to Rails 4. If you're learning the system, there's no point using legacy code to do it!
--
#Gemfile
gem 'rails', '4.1.5'
#config/routes.rb
root to: "todoodoos#index"
resources :todoodoo
#app/controllers/todoodoo_controller.rb
Class TodoodooController < ApplicationController
def index
#todoodoos = Tododoo.all
end
def new
#todoodoo = Todoodoo.new
end
def create
#todoodoo = Todoodoo.new(todo_params)
#todoodoo.save
end
private
def todo_params
params.require(:todoodoo).permit(:name)
end
end
This will give you the ability to add the following views:
#app/views/todoodoo/index.html.erb
<% #todoodoo.all.each do |todo| %>
<%= link_to todo.name, todo %> #-> goes to show action
<% end %>
#app/views/todoodoo/new.html.erb
<%= form_for #todoodoo do |f| %> #-> goes to create action
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
#app/views/todoodoo/create.html.erb
Succesfully created!!
I can explain how this works if you try it & find that it does work
Your current setup is so conflicting that I felt it best to provide you with the required code you need!
Try to make your form simple by removing the :url. Right now your form is confused between to doing the New action and the Index action of your to do list.
<h1> Add new item to your To-Do list </h1>
<%= form_for #todo do |f| %>
<%= f.label :Name %>: <%= f.text_field :name %>
<%= f.hidden_field :done, :value => false %>
<%= f.submit "Add to To-Do list", class: "btn btn-primary" %>
<% end %>

find no results using search form in Rails 4

I'm building a simple search function in my Rails 4 app but it shows no result using the search function.
I've tried several times and find out the use of "%#{params[:search]}%" in my controller always returns nothing. If I use plain string like "example" then it works fine. Is there anything wrong with my code? Thanks!
View
<div class="row">
<%= link_to "new post", new_post_path, :class => "tiny button" %>
<%= form_tag posts_path, method: :get do %>
<%= text_field_tag :search, params[:search], placeholder: "search" %>
<% end %>
</div>
<% #post.each do |post| %>
<h3 class="post_title">
<% end %>
Controller
class PostsController < ApplicationController
def index
if params[:search]
#post = Post.where('title LIKE ?', '%#{params[:search]}%')
else
#post = Post.all
end
end
routes.rb
Rails.application.routes.draw do
resources :posts
root to: "posts#index"
end
Model
class Post < ActiveRecord::Base
validates :title, presence: true
end
You cannot use interpolation on String within single quotes '%#{params[:search]}%'
Change it to
"%#{params[:search]}%"

Rails Copy Button Error

I have an object "device" that contains a good deal of attributes. When editing one of the devices, I want to to have a copy button that when pressed will create a new device and load its "edit page" with the old devices parameters filled into the text_field. But I am very confused on how to called action control methods with buttons and I am currently getting an error saying
"No route matches {:action=>"clone", :controller=>"device", :id=>"1"}"
If I could just get the call to the "clone" method to work then I think I would be in good shape. Any help would be greatly appreciated! My current code is as followed:
edit.html.erb
<div class="row">
<%= form_for(#device) do |f| %>
<div class="span3 offset0">
<%= f.label "Unit Name" %>
<%= f.text_field :unitName %>
.
.
.
<%= f.label "Router Terminal Server IP" %>
<%= f.text_field :routerTerminalServerIp %>
</div>
<div class="span3 offset0">
<%= f.label "N2x Server" %>
<%= f.text_field :n2xServer %>
.
.
.
<%= f.label "Last Changed On" %>
<%= f.text_field :updated_at %>
<%= f.label "Update, Copy, or Delete Device" %>
<%= f.submit "Update", class: "btn btn-medium btn-info" %>
<%= link_to "Clone", :controller => "device", :action => "clone" %>
<%= link_to "Delete", device_path, class: "btn btn-medium btn-danger" %>
</div>
<% end %>
</div>
routes.rb
App::Application.routes.draw do
resources :devices
root 'static_pages#home'
match 'devices/clone', to: 'devices#clone', via: 'get'
end
devices_controller.rb
class DevicesController < ApplicationController
def new
#device = Device.new
end
def clone
oldDevice = Device.find(params[:id])
#device = Device.new
#device = #oldDevice.dup
#device.save
redirect_to edit_device_path(#device.id)
end
def create
#device = Device.new(device_params)
#device.lastChangedBy = request.remote_ip
if #device.save
redirect_to edit_device_path(#device.id)
else
render 'new'
end
end
def show
#device = Device.find(params[:id])
end
def edit
#device = Device.find(params[:id])
end
def update
#device = Device.find(params[:id])
#device.lastChangedBy = request.remote_ip
if #device.update_attributes(device_params)
redirect_to edit_device_path(#device)
else
render "edit"
end
def destroy
Device.find(params[:id]).destroy
redirect_to root_url
end
end
private
def device_params
params.require(:device).permit(:unitName, ...., :owner)
end
end
You should specify the route like this:
resources :devices do
member do
get :clone
end
end
Then you can use the clone_device_path(id) helper.
Also, you should consider using post method for this action and use button instead of link.
You're going to have no end of obscure problems if you name your controller method 'clone', as that is already defined in ruby on Object: http://ruby-doc.org/core-2.0.0/Object.html#method-i-clone
It used to be overridden in ActiveRecord, but lately ActiveRecord uses #dup for this functionality. You can still use 'clone' in your route and in the link label, but use something else for the actual method in your controller; you might want to use an ' :as => 'my_clone' ' option in your routes, if you call your method 'my_clone" for instance.

problem with request.post

I have a controller that has the following index action :
def index
if request.post?
flash[:notice] = "Searching.."
redirect_to songs_path # where songs_path is this index page that i am on
end
end
in my application layout i have defined the flash section as such
<% if flash[:notice] %>
<div id='notice'><%= flash[:notice] %></div>
<% end %>
and on my pages_path i've got
<% form_for :search do |f| %>
<%= f.label :search_text %>
<%= f.text_field :search_text %>
<p>
<%= f.submit "Search" %>
</p>
<% end %>
The final result should be a search through a youtube api ( youtube-g) but now i would only wish to make that flash notification appear when i click on the "Search" button , and it doesn't .. any ideas why ?
index action ordinary is GET request. So, if in your routes.rb there is something like resources :searches then your code won't work.
try this:
def create
render :text => "Searching.."
end
because POST /searches will refer to create action
you should update your RESTful route to like this
resources :songs do
collection do
get 'search'
end
end
Then in your Controller
def search
# Perform Your search here like Youtube ..
flash[:notice] = "Searching.."
redirect_to songs_path # This should work now.
end

how to use youtube-g ruby gem?

new to rails and trying to get youtube-g to work... I am having a difficult time passing a users search from the form_tag to the controller.. for example:
user lands on page, enters - tiger videos - in text_field_tag
controller takes - tiger videos - and searches
user lands on another page displaying the results...
user lands on page, enters query
index.html.erb
<%= form_tag(youtube_path) do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:wth) %>
<%= submit_tag("Search") %>
<% end %>
--
controller takes query and searches
youtubecontroller.rb
class YoutubeController < ApplicationController
def search
require 'youtube_g'
client = YouTubeG::Client.new
client.videos_by(:query => params[:wth])
end
end
--
my routes file:
ActionController::Routing::Routes.draw do |map|
map.search “search”, :controller => “youtube”
I havn´t got to step 3 yet..
what I want is a page with just a text box and a submit button, and when a user enters text and submits, they are brought to a new page that renders 10 youtube results. any help is much appreciated!!!
http://youtube-g.rubyforge.org/
search.html.erb
<%= form_tag(:action => "search") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:wth) %>
<%= submit_tag("Search") %>
<% end %>
YoutubeController
require 'youtube_g'
class YoutubeController < ApplicationController
def index
end
def search
if request.post?
client = YouTubeG::Client.new
#youtube = client.videos_by(:query => params[:wth],:per_page => 10)
render :action => :index
end
end
end
index.html.erb
<% #youtube.videos.each do |video| %>
<%= video.title %>
<% end %>
routes.rb
map.resources :youtube, :collection => { :search => :get }

Resources