Carrying on with getting “new link” working.

Yesterday, i got it so that you can make a new link, which is saved as an article with a score of 0, but had two problems.

a) it’s not making an entry in the join table

b) i can’t work out how to redirect to the original page, as opposed to the new one.

I think that if i solved a i could solve b, as i know how to redirect to a page with a given id, and i get that out of the join table entry.

Waiting for forum help again…In the meantime let’s alter the list view to only show pages with a score > 0 (ie to not show links). OK, did that by simply wrapping the article display code in an if test:

<% if article.points > 0 %>
<br />
<span><strong><%= link_to article.title, :action => “show”, :id => article %></strong>
<span align=”right”> Score = <%= article.points %> </span>
</span>
<% end %>

At first glance this is not as efficient as it could be: we get all the articles and then show only the ones we want. It would perhaps be better to only get the ones we want and then show everything we got. But, a test before getting the array of items could create a delay before we see any of them, which would be bad. So this is the best way to do it after all i think.

OK – no help from the forums. (I’m also STILL trying to enable aleks to import her big data text file into mysql).

Looks like i need to work this one out by myself. Let’s break down some other processes and see what’s going on there.

Comments:

It starts on the view page, where we have this view code (stripped of markup):

form_tag :action => “comment”, :id => @article
text_area “comment”, “body”
submit_tag “Submit comment”

This makes a form, and sets up a call to the comment action, and adds a new entry into the params hash, called :id. There’s nothing special about this name, it could be called :foo – in fact, let’s change the form and action so it is called :foo. But it points at the current article, which we’ll need later to put in the comment’s ‘article_id’ field. The contents of the “comment” text area get passed through as well, with the name ‘body’.

So, the above has generated a form. When the user hits the submit button the ‘comment’ action (in newspipe_controller) is called:

def comment

#find the article using :foo (which by default tries to match against id), get its comments
#then get it’s comments list and create a new one using the params from the table.

@comment = Article.find(params[:foo]).comments.create(params[:comment])
#put in values for the stuff we didn’t get from the form
@comment.added_at = DateTime.now.to_s
@comment.user_id = session[:user].id
#then try to save the new details to the db
if @comment.save
flash[:notice] = “Added your comment.”
else
flash[:notice] = “Sorry, your comment was not able to be saved.”
end
#finally, go back to the article, whose id we stored in :foo
redirect_to :action => “show”, :id => params[:foo]
end

Next, lets look at the “new” and “create” methods that make a new article, and see what’s going on there.

TBC tomorrow by the looks of it – spent all day on aleks’ db problems. Looks like they’re sorted though! That is, the data is going in fine. Conclusion – heidi sql is not very good for importing data, and mysqlimport is the way to go.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s