I'm writing a web app using sinatra and activerecord but I can't figure it out what's wrong into my code...if you could just check it and guide me through it would be awesome !!All the models are linked but I can't update information about some books into the database..
RUBY SINATRA CODE :
get '/info/:isbn/edit' do
#book = Book.find_by(isbn: params[:isbn])
erb :edit
end
patch '/info/:isbn' do
book = Book.find_by(isbn: params[:isbn])
book.title = params[:title]
book.page_count = params[:number_pages]
book.category = params[:category]
book.save
redirect to "/info/#{ params[:isbn] }"
end
ERB PAGE
<form action="/info/<%= #book_isbn%>/edit" method="post">
<input type="hidden" value="patch" name="_method">
<label for="">Title</label>
<input type="text" name="title" value="<%= #book_title %>">
</form>
and this is the error page...
Check to make sure that your controller sets :method_override to true.
set :method_override, true
Without it Sinatra won't know what do with input type="hidden" value="patch" name="_method"
You are sending your form to the wrong URL:
<form action="/info/<%= #book_isbn%>/edit" method="post">
URL should be /info/<%= #book_isbn%>.
Related
I am facing issues with the connection of a form with the database, when I am trying to update a record using Sinatra.
I created a route and a form in a view.
At first the form connects to the get route and brings the relevant data. However, when I modify the fields and press the submit button to update the database, the record does not updated.
get'/users/:id/edit'do
# see the User we want to edit
#user = User.find_by_id(params[:id])
#Assign the values to all properties
#user.username = #user.username
#user.password = #user.password
#user.description =#user.description
#user.city = #user.city
#user.save
erb:edit
end
put 'users/:id/edit' do
#user = User.find_by_id(params[:id])
#Assign the values to all properties
#user.username = #user.username
#user.password = #user.password
#user.description =#user.description
#user.city = #user.city
#user.save
end
My form looks like this:
<form action="users/:id/edit" method="post" id="edit">
<input type="hidden" name="_method" value="put">
User ID : :<%=#user.id%> <br>
Username:<input type="text" name="username" value=
<%=#user.username%>"><br>
Password:<input type="password" name="password" value="
<%=#user.password%>"><br>
City:<input type="text" name="city" value="<%=#user.city%>">
<br>
Tell us more about you:<input type="text" name="description"
value="<%=#user.description%>"><br>
<input type="submit" name="Update" class="btn btn-primary " >
</form>
Can you please assist?
The form was passing the record id to the route, thus I modified the form to:
<form action="/users/edit/<%=#user.id%>" method="post"
id="edit">
and the route to:
put '/users/edit/:id' do
#user = User.find_by_id(params[:id])
#user.city= params[:city]
# rest of the fields need to be modified
end
The new version of watir-webdriver (0.8.0) has been released. I have installed it and have been running my suites to check for changes. During this process I have noticed the text_field methods I am using to set text in <input id="some_id" type="text"> tags has been deprecated from the message Locating textareas with '#text_field' is deprecated. Please, use '#textarea' method instead. I changed the text_field method to textarea, but once I do this the element can no longer be located.
Am I using the method wrong? I tried to use it as browser.textarea(:id=> 'some_id').when_present.set "Text". Once the method times out, I get the message saying the element with the tagname textarea could not be located. Was wondering if there is something else I need the do to use the textarea method to set text in a <input id="some_id" type="text"> tag.
EDITED => html I am trying to automate on:
<form action="some_href" method="post">
<p class="align">
<label for="user">Username</label>
<input id="login" type="text" value="">
</p>
<p class="align">
<label for="pass">Password</label>
<input id="password" type="password" value="">
</p>
</form>
EDITED Watir code used to automate page
#RUBY CODE
class Login
def open
#browser = Watir::Browser.new :firefox
#browser.goto some_website_url #URL leading to page with form HTML above
end
def login_as
user_field.when_present(10).set username
password_field.when_present(10).set password
login_link.when_present(10).click
end
def user_field
#browser.text_field(:id=> "login")
end
def password_field
#browser.text_field(:id=> 'password')
end
def username
"user123"
end
def password
"p#ssw0rd"
end
def login_link
#browser.link(:text=> 'Sign-in')
end
end#Login
#STEP DEFINITION
Given(/^login to website$/) do
login = Login.new
login.open
login.login_as
end
The previous versions I have been using without the deprecation message is:
watir-webdriver (0.6.11)
selenium-webdriver (2.44.0)
Any answers would be appreciated if someone has some insight on the problem.
I am new to Ruby, and am using Sinatra and Sequel. I'm trying to implement a form to search through the title of my posts.
I'm doing this in my controller:
post '/search' do
#post = Post.all(:Title.like => "%#{params[:query]}%")
erb :layout
end
And I'm doing this in my layout.erb:
<form action="/search" method="get">
<input type="text" name="query"/><br />
<input type="submit" />
</form>
<% if #results %>
<table>
<%#results.each do |r|%>
<tr valign="top">
<td><%=r.Title%></td>
</tr>
<%end%>
</table>
<% end %>
When I submit, this is the URL I get directed to:
http://localhost:4567/search?query=post
but it displays the "Sinatra doesn't know this ditty." screen.
What am I missing here?
Your form is doing a HTTP GET
<form action="/search" method="get">
but your Sinatra action is defined to receive HTTP POST requests.
post '/search' do
I think what is confusing you is that you have a class named Post. The get and post in the actions are not class names, but REST actions. Review routing.
I'm writing a simple Sinatra app but having issues having <input type="file" multiple /> not making Rack throw a NoMethodError: undefined method 'bytesize' for (Hash) while reading the files.
The form is written like so:
<form action="/upload" enctype="multipart/form-data" method="post">
<input type="file" name="images[]" multiple />
</form>
But the receiving end throws the mentioned error, before any of my code executes, that is, Rack is not parsing correctly the input[name=images]. Am I sending the form incorrectly? If I drop the brackets [], then only the last file (of many) is sent, but I feel like I might be missing something...
Just to clarify: this is Sinatra v1.4.3 and Rack v1.5.2, the latter being the one throwing the exception. Full backtrace here.
The only thing that puts me off here is that you don't use the POST method – maybe your issue has to do with that. Anyway, the following code works perfectly for me. I hope this will give you a hint how to fix your code.
require 'sinatra'
get '/' do
<<-HTML
<html>
<head><title>Multi file upload</title></head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>
</body>
</html>
HTML
end
post '/upload' do
content_type :text
res = "I received the following files:\n"
res << params['images'].map{|f| f[:filename] }.join("\n")
res
end
Something's wrong with the PUT action here, the form gets processed but the updated field is not being saved.
I've did what Sinatra users are doing, by adding in "_method" for Sinatra to recognise that's its a HTTP PUT action. Could anyone spot any mistake here?
# edit
get '/entries/*/:id/edit' do
#entry = Entries.get(params[:id])
#title = "edit"
erb :edit, :layout => :edit_layout
end
# update
put '/entries/:id' do
#entry = Entries.get(params[:id])
if #entry.save
redirect "/entries/id=#{#entry.id}"
else
redirect "/enewsletters"
end
end
<!-- Edit form -->
<form action="/enewsletters/edit/<%= #entry.id %>" method="post">
<input name="_method" value="put" type="hidden"/>
<p>
<label>Content</label><br/>
<input type="text" name="entry[title]" value="<%= #enew.title %>">
</p>
<p>
<input type="submit" name="commit" value="update">
</p>
</form>
You don't seem to be doing any update to the #entry, you're just fetching the specific entry with the id from params. Are you using ActiveRecord? If so, instead of #entry.save, try #entry.update_attributes(params[:entry]).
Edit: I'm guessing you're not using AR since I just noticed the .get call. Whatever ORM you are using must have an easy way to update the attributes and then save the record.