Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources Routes to Create Actions View for New Pages

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

my form_for isn't working.

My form_for code isn't working. The code I have is:

<%= form_for (@mediums) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

<% end %>

The error message I keep getting is:

SyntaxError at /profile
/Users/maryanneleanya/site2/views/new.erb:20: syntax error, unexpected ')' ...<(( form_for(@mediums) do |f| ).to_s) ... ^ 
/Users/maryanneleanya/site2/views/new.erb:24: syntax error, unexpected keyword_end, expecting ')' ; end ^~~ 
/Users/maryanneleanya/site2/views/new.erb:41: syntax error, unexpected keyword_ensure, expecting ')' ensure ^~~~~~ 

Also I am using sinatra.

10 Answers

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

https://teamtreehouse.com/library/linking-to-pages I think the problem that I'm having is that the method "link_to" doesn't work with sinatra. And I can't use the video you recommended because I didn't save to a file. I saved to a database.

Jay McGavren
Jay McGavren
Treehouse Teacher

That's right. You should not use the link to method. Instead, you should set up the form and the Sinatra route as you see in the video. But then, instead of writing to the text file, you should use the destroy method on the model object. That will delete the object in the database.

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

form_for only works in Rails, not Sinatra. For Sinatra, you'll need to write the form HTML yourself. Start with this video to learn how.

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

Thanks! If I wanted to send the post path to a database instead of to a file, could I embed php in the HTML forms. Would that work with Sinatra.

Jay McGavren
Jay McGavren
Treehouse Teacher

MaryAnn Eleanya Integrating PHP with any Ruby program would probably be technically difficult, and the result wouldn't perform very well. You'd succeed much more quickly (and get better results) if you research integrating ActiveRecord with Sinatra. But really, if you're at the point where you're thinking about the need for a database, it might be time to consider switching your entire app from Sinatra to Rails.

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

(SORRY I POSTED IN THE WRONG AREA PREVIOUSLY)

Hi Jay, I already have sinatra working with active record. I just need to make sure that my http post request in sinatra is going to the database. This is what I have:

new.html.erb

<form method="post" action="/send"> 
<label for = "medium"> Name</label> 
<input type="text" name=""> 
<input type="submit" name="">
</form>

test.rb

class Test < Sinatra::Base

post ("/send") do 

'The post route is correctly working' 

end

end
Jay McGavren
Jay McGavren
Treehouse Teacher

I assume that when you click the submit button, you get the response "the post route is correctly working"...

Next you'll need to fix the name and id attributes on your form input tags. See this video for info on how to do that. You can check the parameters the form is sending like this:

post("/send") do
  puts params.inspect
end

When you submit the form, the value of params will appear among the other output in your terminal (not in your browser). You'll need to keep tweaking the form HTML until the value of params is correct.

You'll also need to ensure that ActiveRecord is saving objects to the database. I haven't tested this code, but it will be something like:

post("/send") do
  @medium = Medium.new(name: "Test Name")
  @medium.save
  # Again, this output will appear in your terminal
  puts "Object created in Sinatra route:"
  puts @medium
  puts "Same record, retrieved from database:"
  puts Medium.find(@medium.id)
end

Finally, once your form is set up correctly and you're sure ActiveRecord is working, you'll need to use the params from the form to set up the model object attributes:

post("/send") do
  @medium = Medium.new(name: params[:name])
  @medium.save
  redirect "/some/other/path"
end
MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

The delete method posted on this video isn't working for me since I am still using sinatra. How can I delete a post without using the "_method" method.

Jay McGavren
Jay McGavren
Treehouse Teacher

For anyone else reading, please note that this reply is applicable only to Sinatra, not Rails...

MaryAnn Eleanya Which video are you referring to? Can you post a link to it?

You'll need to set up a form and Sinatra route similar to what you see in this video. Within the route block, you'll need to look up the ActiveRecord model object you want to delete, and then call the destroy method on it.

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

I am getting the following error msg.NoMethodError at /id

undefined method `link_to' for #<Test:0x00007fb1e1bc4bf8>

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

But how can I crate a path to the show method when the link_to method isn't working.

Jay McGavren
Jay McGavren
Treehouse Teacher

You wouldn't use link_to, because that method only works with Rails, and you're using Sinatra. However, you are also using ActiveRecord. ActiveRecord is part of Rails, but it can also be used separately from Rails. So you need to take the code for the delete form and delete route from the Sinatra videos, and the code to delete the database model object from the Rails videos.

As shown in this video, you will need to create an HTML form with a method attribute of post, containing an <input type="hidden"...> tag with a name of _method and a value of delete. You will also need to create a delete Sinatra route.

But then, within that delete Sinatra route, you would NOT call a delete_content method. Instead, you would use the ActiveRecord find method on your model class to find a particular instance of your model class that you want to delete. Then you would call the destroy method on that model object.

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

Hi, Im having a hard time figuring out what the show view should look like:

This is what I have so far.

class Test < Sinatra::Base
get ("/:name") do
@mediums = params[:name]
erb :show
end

end

show.erb

<h1> <%= @mediums %></h1>
MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

I've figured it out, but it is now treating the delete request as a post request.

Try this:

# in test.rb
class Test
  post '/@mediums.name' do
    "Hello World"
  end
end
Jay McGavren
Jay McGavren
Treehouse Teacher

I'm guessing the form HTML code in your .erb file isn't set up correctly. Can you post that ERB code here?

MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

I fixed it. I just needed to include rack::MethodOverride in my config.ru. But now it seems that the delete block isn't responding. This is what I have.

test.rb

class Test < Sinatra::Base
delete ("/@mediums.name/:id") do 
Mediums.get(params[:id]).destroy
redirect "/names"
end

end

show.erb

<form method="post" action= "/<%=@mediums.name%>/<%=@mediums.id%>">
    <input name="_method" type="hidden" value="delete">
    <input type="submit"  value="Delete name">
</form>
Jay McGavren
Jay McGavren
Treehouse Teacher

Your delete action has a path of "/@mediums.name/:id". That's not valid. You probably want a path of "/mediums/:id".

class Test < Sinatra::Base
delete ("/mediums/:id") do 
Mediums.get(params[:id]).destroy
redirect "/names"
end

end
<form method="post" action= "/mediums/<%=@mediums.id%>">
    <input name="_method" type="hidden" value="delete">
    <input type="submit"  value="Delete name">
</form>
MaryAnn Eleanya
MaryAnn Eleanya
8,626 Points

I am getting the following messages:

"NoMethodError at /mediums/4 undefined method `get' for #<Class:0x00007f829b2f8030> Did you mean? gem file: dynamic_matchers.rb location: method_missing line: 22"

Jay McGavren
Jay McGavren
Treehouse Teacher

I'm just now noticing that you have Mediums.get(params[:id]).destroy. You probably mean Mediums.find(params[:id]).destroy.